Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously speeds up the code by almost 6x:
[...]
- Without std::sort(data, data + arraySize);, the code runs in 11.54 seconds.
- With the sorted data, the code runs in 1.93 seconds.
My first thought was that sorting brings the data into cache, but my next thought was how silly that is because the array was just generated.
What is going on? Why is a sorted array faster than an unsorted array? The code is summing up some independent terms, the order should not matter.
-----
Answer:
You are the victim of branch prediction fail.
Your new post is loading...