Best accurate way to measure/compare elapsed time in C++

xeerx
3 min readJul 6, 2022
  • Because this is a big problem that needs a careful solution
  • And since there are a lot of people who are having problems with time measurement to improve their code

So let me show you the perfect way to measure time in C++ code

Let’s say i have a custom function that finds the floor square root for a number

And i know that the functions floor(sqrt(x)) in the <cmath> library can be used !

But I care a lot about time performance, and I want to know which function is taking longer to execute?

So I searched a lot, and found a primitive solution !

which is to calc the time in each function at its begin and end and calculate the difference

#include int num = 20221; // measure time for floorSqrt(x) auto begin1 = std::chrono::steady_clock::now(); floorSqrt(num); auto end1 = std::chrono::steady_clock::now(); auto time1 = std::chrono::duration_cast<std::chrono::nanoseconds> (end1 - begin1).count(); // measure time for floor(sqrt(num)) auto begin2 = std::chrono::steady_clock::now(); floor(sqrt(num)); auto end2 = std::chrono::steady_clock::now(); auto time2 = std::chrono::duration_cast<std::chrono::nanoseconds> (end2 - begin2).count(); // print results std::cout << << num << << time1 << std::endl; std::cout << << num << << time2 << std::endl;floorSqrt (20221) : 130180 floor(sqrt(20221)): 18013

Great, now I know that floor(sqrt(x)) is faster by 112167 nanosecond!

But let’s repeat this test 10 times and see the result

floorSqrt (20221) : 131491 floor(sqrt(20221)): 130523 floorSqrt (20221) : 1952 floor(sqrt(20221)): 2078 floorSqrt (20221) : 1495 floor(sqrt(20221)): 1825 floorSqrt (20221) : 1460 floor(sqrt(20221)): 1823 floorSqrt (20221) : 1454 floor(sqrt(20221)): 1716 floorSqrt (20221) : 1464 floor(sqrt(20221)): 1720 floorSqrt (20221) : 1498 floor(sqrt(20221)): 1762 floorSqrt (20221) : 1453 floor(sqrt(20221)): 1706 floorSqrt (20221) : 1432 floor(sqrt(20221)): 1730 floorSqrt (20221) : 1461 floor(sqrt(20221)): 1727

Which is the correct test ?!!!

What is the ideal and accurate way to measure and compare execution time?

in this case the solution is very simple !

  1. loop the code for n times
  2. store all results in array
  3. find the median number in array
  4. compare between median results of the two functions

Of course it is not that easy, there are many, many details to consider!

So fortunately the C++ Timeit library was built to do these things accurately and correctly !

#include std::cout << timeit(1000, floorSqrt, num).nanoseconds() << std::endl;

Not sure about the accuracy?

Well let’s use the repeatit function to repeat the test 10 times and see the result

repeatit(10,[]{ std::cout << timeit(1000, floorSqrt, num).nanoseconds() << std::endl; });5648 5641 5667 5679 5691 5634 5695 5664 5747 5644

Of course the results won’t be the same for other reasons, but it’s much better than a normal test

[COMPARE IT] first(5827) > second(1770) x3 [COMPARE IT] first(5825) > second(1762) x3 [COMPARE IT] first(5825) > second(1764) x3 [COMPARE IT] first(5832) > second(1765) x3 [COMPARE IT] first(5830) > second(1761) x3 [COMPARE IT] first(5836) > second(1768) x3 [COMPARE IT] first(5824) > second(1767) x3 [COMPARE IT] first(5831) > second(1768) x3 [COMPARE IT] first(5822) > second(1762) x3 [COMPARE IT] first(5828) > second(1765) x3
  • Don’t run the test in the IDE like vscode
  • Run it in terminal using some thing like valgrind

In this way, we get the real difference between the two functions and compared them accurately

Originally published at http://github.com.

--

--