Why am I scripting this publish?
- As a result of this can be a huge drawback that wants a cautious resolution
- And since there are lots of people who’re having issues with time measurement to enhance their code
So let me present you the right option to measure time in C++ code
Situation
For example i’ve a customized perform that finds the flooring sq. root for a quantity
int floorSqrt(int x)
{
if (x <= 1) return x;
int i = 1, consequence = 1;
whereas (consequence <= x) { i++; consequence = i * i; }
return i - 1;
}
And i do know that the features flooring(sqrt(x))
within the <cmath>
library can be utilized !
However I care quite a bit about time efficiency, and I need to know which perform is taking longer to execute?
So I searched quite a bit, and located a primitive resolution !
which is to calc the time in every perform at its start
and finish
and calculate the distinction
#embrace <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).rely();
// measure time for flooring(sqrt(num))
auto begin2 = std::chrono::steady_clock::now();
flooring(sqrt(num));
auto end2 = std::chrono::steady_clock::now();
auto time2 = std::chrono::duration_cast<std::chrono::nanoseconds> (end2 - begin2).rely();
// print outcomes
std::cout << "floorSqrt ("<< num << ") : " << time1 << std::endl;
std::cout << "flooring(sqrt("<< num << ")): " << time2 << std::endl;
output
floorSqrt (20221) : 130180
flooring(sqrt(20221)): 18013
Nice, now I do know that flooring(sqrt(x))
is quicker by 112167
nanosecond!
However let’s repeat this take a look at 10 instances and see the consequence
for (size_t i = 0; i < 10; i++)
{
/* earlier code */
}
output
floorSqrt (20221) : 131491
flooring(sqrt(20221)): 130523
floorSqrt (20221) : 1952
flooring(sqrt(20221)): 2078
floorSqrt (20221) : 1495
flooring(sqrt(20221)): 1825
floorSqrt (20221) : 1460
flooring(sqrt(20221)): 1823
floorSqrt (20221) : 1454
flooring(sqrt(20221)): 1716
floorSqrt (20221) : 1464
flooring(sqrt(20221)): 1720
floorSqrt (20221) : 1498
flooring(sqrt(20221)): 1762
floorSqrt (20221) : 1453
flooring(sqrt(20221)): 1706
floorSqrt (20221) : 1432
flooring(sqrt(20221)): 1730
floorSqrt (20221) : 1461
flooring(sqrt(20221)): 1727
Which is the proper take a look at ?!!!
The Query
What’s the best and correct option to measure and examine execution time?
The Answer
on this case the answer could be very easy !
- loop the code for
n
instances - retailer all leads to array
- discover the median quantity in array
- examine between median outcomes of the 2 features
After all it isn’t that simple, there are numerous, many particulars to contemplate!
So luckily the C++ Timeit library
was constructed to do this stuff precisely and accurately !
The way to measure execution time for C++ perform ?
#embrace "timeit.hpp"
std::cout << timeit(1000, floorSqrt, num).nanoseconds() << std::endl;
output
5537
Undecided in regards to the accuracy?
Effectively let’s use the repeatit
perform to repeat the take a look at 10 instances and see the consequence
repeatit(10,[]{ std::cout << timeit(1000, floorSqrt, num).nanoseconds() << std::endl; });
output
5648
5641
5667
5679
5691
5634
5695
5664
5747
5644
After all the outcomes will not be the identical for different causes, however it’s a lot better than a standard take a look at
The way to examine execution time for 2 features ?
int func1(){ return floorSqrt(num); }
int func2(){ return flooring(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
Suggestions for getting excessive accuracy
- Do not run the take a look at within the
IDE
likevscode
- Run it in terminal utilizing some factor like
valgrind
On this means, we get the true distinction between the 2 features and in contrast them precisely