This content originally appeared on DEV Community and was authored by Maysara
Why am I writing this post?
- 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
Scenario
Let's say i have a custom function that finds the floor square root for a number
int floorSqrt(int x)
{
if (x <= 1) return x;
int i = 1, result = 1;
while (result <= x) { i++; result = i * i; }
return i - 1;
}
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 <chrono>
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 << "floorSqrt ("<< num << ") : " << time1 << std::endl;
std::cout << "floor(sqrt("<< num << ")): " << time2 << std::endl;
output
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
for (size_t i = 0; i < 10; i++)
{
/* previous code */
}
output
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 ?!!!
The Question
What is the ideal and accurate way to measure and compare execution time?
The Solution
in this case the solution is very simple !
- loop the code for
n
times - store all results in array
- find the median number in array
- 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 !
How to measure execution time for C++ function ?
#include "timeit.hpp"
std::cout << timeit(1000, floorSqrt, num).nanoseconds() << std::endl;
output
5537
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; });
output
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
How to compare execution time for two functions ?
int func1(){ return floorSqrt(num); }
int func2(){ return floor(sqrt(num)); }
repeatit(10,[]{ compareit(1000, func1, func2); });
output
[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
Tips for getting high accuracy
- Don't run the test in the
IDE
likevscode
- 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
This content originally appeared on DEV Community and was authored by Maysara
Maysara | Sciencx (2022-07-06T01:18:47+00:00) Best accurate way to measure/compare elapsed time in C++. Retrieved from https://www.scien.cx/2022/07/06/best-accurate-way-to-measure-compare-elapsed-time-in-c/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.