Search code examples
performancec++11stliteratorstdset

Composited container from std::set is faster than std::set itself?


I have made some composited container Range which accepts either a min/max range as a std::pair or a set of integers as a std::set.

internally it saves a copy of the input by as void *

This container supports iterators and I was curious how fast this container is compared to iterating just over the set std::set.

I made sure that I write every iteration loop in my test with the following policies:

  • no post-increment (slower)
  • make sure compiler is not able to fully optimize away the loops
  • compare the same things

I have got the following timings: performance test

Speed Test
RUNTIME of Range: [Pair] : 13.0522 ms
RUNTIME of Range: [Set]: 272.54 ms
RUNTIME of std::set: 438.239 ms
RUNTIME of Range: Normal For Loop: 0.000175 ms

First I was suprised, and I hope you too, because why should my Range container be faster as the iterators of Range contain bot the iterator for the std::pair (simply a integer) and for std::set. And my implementation of Range::iterator actually forwards all operators to the std::set if the Range is a std::set.

Second To you have any comments on the implementation of this to be efficient. Might there be a better approach?


Solution

  • With dyp's comment: the std::set is faster!

    Speed Test ============================
    RUNTIME of Range: [Pair] : 8.69353 ms
    RUNTIME of Range: [Set]: 281.202 ms
    RUNTIME of std::set: 220.367 ms // Recopied the std::set
    RUNTIME of Range: Normal For Loop: 0.000196 ms