Put each loop in your own method, and then compare the time spent on the methods. And use enough iterations to actually spend some time (or wrap them in another loop). 10,000 iterations should take microseconds; which is difficult for the profiler to measure. Try a billion (or 100 thousand Iterators out of 10 thousand Iterations).
In addition, if f(i)
expensive, it will take much longer than a cycle. In addition, if f(i)
actually does nothing, it can be fully optimized. Therefore, make sure that it (for example, updates the counter somewhere, calculates the amount or something else).
source share