|
|
|
|
|
by haskellshill
355 days ago
|
|
>we end up performing two loops instead of one, all because sticking three operations in one statement is "yucky" You seem to believe that "O(2n)" for value in range(1, n + 1):
result ^= value
for value in A:
result ^= value
is slower than "O(n2)" for value in range(1, n + 1):
result ^= value
result ^= A[value-1]
simply because the latter has one "for loop" less. Am I misunderstanding you, or if not, why would this matter for speed? |
|
Each iteration of a for loop performs one index update and one termination comparison. For a simple body that is just an XOR, that's the difference between performing 5 operations (update, exit check, read array, XOR with value, XOR with index) per N elements in the one loop case versus 7 operations (update, exit, read array, XOR with value, then update, exit, XOR with index) in the two loop case. So we're looking at a 29% savings in operations.
It gets worse if the looping structure does not optimize to a raw, most basic for loop and instead constructs some kind of lazy collection iterator generalized for all kinds of collections it could iterate over.
The smaller the loop body, the higher the gains from optimizing the looping construct itself.