3 ms·
I once found code that used vector instead of list, and the author had a benchmark exactly like this to defend it. Except that, the benchmark was storing ints
by hamstergene 4y ago
I once found code that used vector instead of list, and the author had a benchmark exactly like this to defend it.
Except that, the benchmark was storing ints but our production code stored std::function closures. Changing the benchmark to store a simple struct with two shared_ptrs invalidated it, showing that list outperforms vector on as little as 4 elements for head&middle insertions.
I think all blog articles about CPU caches could use to repeat their benchmarks on something that hides a function call (move constructor), an atomic write, and an allocation, just to demonstrate how tight the boundaries are.
What is good about sticking with fundamental computer science is that it provides pretty strong guarantee about what can and cannot happen, while hand-written optimizations are fragile. Even if optimization does work today, one year later the next maintainer may alter data types, or production data volumes may change, and the optimization will start doing the opposite.