Why is using a loop to iterate from start of array to end faster than iterating both start to end and end to start?

后端 未结 5 643
無奈伤痛
無奈伤痛 2021-01-03 21:19

Given an array having .length 100 containing elements having values 0 to 99 at the respective indexes, where the requirem

5条回答
  •  孤城傲影
    2021-01-03 22:26

    The answer is pretty obvious:

    More operations take more time.

    When judging the speed of code, you look at how many operations it will perform. Just step through and count them. Every instruction will take one or more CPU cycles, and the more there are the longer it will take to run. That different instructions take a different amount of cycles mostly does not matter - while an array lookup might be more costly than integer arithmetic, both of them basically take constant time and if there are too many, it dominates the cost of our algorithm.

    In your example, there are few different types of operations that you might want to count individually:

    • comparisons
    • increments/decrements
    • array lookup
    • conditional jumps

    (we could be more granular, such as counting variable fetch and store operations, but those hardly matter - everything is in registers anyway - and their number basically is linear to the others).

    Now both of your codes iterate about 50 times - they element on which they break the loop is in the middle of the array. Ignoring off-by-a-few errors, those are the counts:

                   |  forwards  | forwards and backwards
    ---------------+------------+------------------------
    >=/===/<       |       100  |                   200
    ++/--          |        50  |                   100
    a[b]           |        50  |                   100
    &&/||/if/for   |       100  |                   200
    

    Given that, it's not unexpected that doing twice the works takes considerably longer.

    I'll also answer a few questions from your comments:

    Is additional time needed for the second object lookup?

    Yes, every individual lookup counts. It's not like they could be performed at once, or optimised into a single lookup (imaginable if they had looked up the same index).

    Should there be two separate loops for each start to end and end to start?

    Doesn't matter for the number of operations, just for their order.

    Or, put differently still, what is the fastest approach to find an element in an array?

    There is no "fastest" regarding the order, if you don't know where the element is (and they are evenly distributed) you have to try every index. Any order - even random ones - would work the same. Notice however that your code is strictly worse, as it looks at each index twice when the element is not found - it does not stop in the middle.

    But still, there are a few different approaches at micro-optimising such a loop - check these benchmarks.

    • let is (still?) slower than var, see Why is using `let` inside a `for` loop so slow on Chrome? and Why is let slower than var in a for loop in nodejs?. This tear-up and tear-down (about 50 times) of the loop body scope in fact does dominate your runtime - that's why your inefficient code isn't completely twice as slow.
    • comparing against 0 is marginally faster than comparing against the length, which puts looping backwards at an advantage. See Why is iterating through an array backwards faster then forwards, JavaScript loop performance - Why is to decrement the iterator toward 0 faster than incrementing and Are loops really faster in reverse?
    • in general, see What's the fastest way to loop through an array in JavaScript?: it changes from engine update to engine update. Don't do anything weird, write idiomatic code, that's what will get optimised better.

提交回复
热议问题