Calling std::nth_element() function extremely frequently

后端 未结 3 1361
渐次进展
渐次进展 2021-02-13 01:42

I did not find this specific topic anywhere...

I am calling the nth_element() algorithm about 400,000 times per second on different data in a std::vector of 23 integers,

3条回答
  •  臣服心动
    2021-02-13 02:12

    I found this problem interesting, so I tried all the algorithms I could think of.
    Here are the results:

    testing 100000 repetitions
    variant 0, no-op (for overhead measure)
    5 ms
    variant 1, vector + nth_element
    205 ms
    variant 2, multiset + advance
    745 ms
    variant 2b, set (not fully conformant)
    787 ms
    variant 3, list + lower_bound
    589 ms
    variant 3b, list + block-allocator
    269 ms
    variant 4, avl-tree + insert_sorted
    645 ms
    variant 4b, avl-tree + prune
    682 ms
    variant 5, histogram
    1429 ms
    

    I think we can conclude, that you where already using the fastest algorithm. Boy was I wrong. However, if you can accept an approximate answer, there are probably faster ways, such as median of medians.
    If you are interested, the source is here.

提交回复
热议问题