On 12.08.2017 19:56, Stefan Ram wrote:
> Manfred <non...@invalid.add> writes:
>> This obviously depends on the usage of the container: for the general
>> case of random or sequential access, then vector wins.
>> When it comes to more complex operations like searching, then it is
>> understandable that other types of container work better.
>
> You are right. In my case, I used »random()« but I was not
> aware of RAND_MAX still being 32767 or so and therefore,
> the final section of the sorted_vector rarely got shifted.
>
> I now use a random-number generator that generates random
> numbers up to ::std::numerics_limits<unsigned long>::max()
> or so and get these results:
>
> filling ::sorted_vector 79'893,747.427
> retrieving ::sorted_vector 238,574.119
> filling ::std::unordered_set 360,994.757
> retrieving ::std::unordered_set 248,835.371
> filling ::std::set 741,265.933
> retrieving ::std::set 722,563.192
>
> . Here are the results with »::randomize( 0 );« added
> to find the same numbers again:
>
> filling ::sorted_vector 83'803,941.903
> retrieving ::sorted_vector 266,760.153
> filling ::std::unordered_set 362,649.427
> retrieving ::std::unordered_set 161,849.880
> filling ::std::set 754,459.535
> retrieving ::std::set 713,790.944
>
> . This means that when we actually find some numbers
> again, now ::std::unordered_set is fastest.
It appears that your definition of "sorted_vector" is useless (a poor
implementation of std::set). A proper "sorted_vector" is what you fill
by e.g. push_back() first, then sort once and keep (at least mostly)
constant afterwards. This should get rid of the apparent 100x overhead
in filling it and make the numbers more comparable.
This is not to say that it might beat std::unordered_set, the latter may
still outwin because it does not have to keep the data in a given order.
Cheers
Paavo