It will be even faster if you do a binary search on the sorted array for the first positive value, and sum all values from that index until the end, without the check in the inner loop. – Mark Jeronimus Feb 26 '14 at 15:46
On a lighter note, sorting the input data would make it faster by a factor of at least log (base2) N , where N is the input size, because you could have a boolean flag to check if the array is already sorted :) – R R Madhav Apr 12 '14 at 14:58
Jun 28 2012 – nicael Jun 6 '14 at 19:22
The amazing thing here is that the equivalent code in CPython 2.7.6, CPython 3.4.1, and Ruby 2.0.0 seems to also benefit by 10-30% from sorting the input. Not quite the 80% speedup of C++ and Java, or the 60% speedup of PyPy and v8, but a lot more than the negligible benefit I'd expect. I guess that comes down to the CPU mis-predicting a branch inside the ceval loop (which has a heavier cost, but since the whole loop is much heavier it's proportionately less)? – abarnert Sep 28 '14 at 4:30
Try switching the order of the cases. On my machine running the same benchmark above unsorted executes in 16.8X seconds, then sorted in 9.8X. When I switched the order to run sorted first, then unsorted it executes the sorted bench in 6.6X, then unsorted in a whopping 19.3X. Why is this?? – user3029413 Oct 11 '14 at 12:11
I think it is indeed misleading. Because one thing is to "optimize" the compiler/processor and an other is to optimize the code. In the "demo" code the timing is recorded "AFTER" sorting. So it looks like a compiler/optimizer/processor thing. In other words: It is simply the wrong question! As it is not about compiler optimization, but an actual code is presented. People might think to use this as optimization feature, while, indeed it is just a very bad code. Because you can seek the data well before; in advance. This situation should not happen. – mario Feb 10 at 18:50
I am sorry, because indeed each answer, is totally pertinent and fully explanatory. I did learn something too. But I think that the approach is wrong and we should discourage it. That code should not exists unless you test the compiler/optimizer/processor. This makes it a totally different question. The accepted answer should warn: do not do this!