I'll share a mathematical approach to discounting the loop overhead when looping over a piece of code in order to measure how long it takes (as compar

ates.dev · Accurate Benchmarking

submited by
Style Pass
2025-01-16 22:00:19

I'll share a mathematical approach to discounting the loop overhead when looping over a piece of code in order to measure how long it takes (as compared to some alternative piece of code).

This might be a well-known approach, and there might be better methods, but it is nevertheless a method I independently came up with and have used in the past.

Since computers are very fast, running something just once isn't a viable way to measure how long it takes to run. We therefore run it many times in a loop and measure the total amount of time it takes.

To get a quantitative "X is p% faster than Y", we should factor in the loop overhead. If method1 and method2 are both "slow" methods that take many computational cycles to run, the tiny overhead of the for loop will be insignificant. The faster the methods we're comparing, the more significant the loop overhead becomes.

Let’s denote the total time for method1 to run n times as t1, and the total loop overhead for n iterations as e. And method2 takes t2.

Leave a Comment