All three versions yield the same result.
http://coliru.stacked-crooked.com/a/84eb5d49b6a5bfe3
> is there any chance of directly using my function in your timing routine?
> I am intrigued to know how a hard-coded loop compares with the STL.
A carefully written hand coded loop would, in most cases, be as efficient as the equivalent standard algorithm.
Hint:
a. There is no need to call erase() every time a duplicate is found. We can overwrite the duplicate value, and then, at the very end, do a single bulk erase of the values at the back of the vector.
b. When a duplicate is found, we need to immediately move to the left only the values up to the next duplicate. This way, when an nth duplicate is found, we can move the values to the right of it by n positions; this would reduce the number of moves.
c. With
end_unique = std::remove( iter+1, end_unique, *iter ) ;, the number of values to be scanned for duplicates is reduced; the elements already marked for removal are excluded from subsequent scans. This too can be done if the call to
std::remove is replaced with a hand coded loop.
c. C++11: Since the values at the back are to be removed, the only requirement is that they be safely destructible. So the values can be moved to the left (instead of copied, which needs to preserve the original value). (This does not make any difference when the values are of type
int).
Anyway, here are some numbers:
version_one_kemort: 2760 millisecs.
version_two_std_remove: 50 millisecs.
version_three_lastchance: 5660 millisecs.
version_one_kemort: 2860 millisecs.
version_two_std_remove: 30 millisecs.
version_three_lastchance: 5460 millisecs. |
http://coliru.stacked-crooked.com/a/efca89f08ef4a835