Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing the performance of a C++ app

I'm trying to find a way to test how long it takes a block of C++ code to run. I'm using it to compare the code with different algorithms and under different languages, so ideally I would like a time in seconds / milliseconds. In Java I'm using something like this:

long startTime = System.currentTimeMillis();

function();

long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime; 

Is there a good way to get an accurate time like that in C++ (Or should I use some other means of benchmarking)?

like image 996
Jeremy Avatar asked Apr 28 '09 00:04

Jeremy


People also ask

How do I check the performance of an app?

In Android Studio, launch the profiler by selecting View > Tool Windows > Profiler. button in the profiler to see the dropdown menu. Select your device, then select the application's entry under Other profileable processes. The profiler should attach to the application.

Which is best tool for performance testing?

Apache JMeter JMeter is a popular open-source load and performance testing tool. It can examine and benchmark a wide variety of software, including networks and servers. It is usually used to test web service applications for the load. Incorporate JMeter, a Java platform application.

How do you test the performance of a Web application manually?

Generally, manual performance testing can be done by checking the number of open connections - active sessions, database connections, amount of CPU time, amount of memory being used etc. This is the amount of performance checking that could be done by a human.


2 Answers

Use the best counter available on your platform, fall back to time() for portability. I am using QueryPerformanceCounter, but see the comments in the other reply.

General advise:

The inner loop should run at least about 20 times the resolution of your clock, to make the resolution error < 5%. (so, when using time() your inner loop should run at least 20 seconds)

Repeat these measurements, to see if they are consistent.

I use an additional outer loop, running ten times, and ignoring the fastest and the slowest measurement for calculating average and deviation. Deviation comes handy when comparing two implementations: if you have one algorithm taking 2.0ms +/-.5, and the other 2.2 +/- .5, the difference is not significant to call one of them "faster". (max and min should still be displayed). So IMHO a valid performance measurement should look something like this:

10000 x 2.0 +/- 0.2 ms (min = 1.2, , max=12.6), 10 repetitions

If you know what you are doing, purging the cache and setting thread affinity can make your measurements much more robust.

However, this is not without pifalls. The more "stable" the measurement is, the less realistic it is as well. Any implementation will vary strongly with time, depending on the state of data and instruction cache. I'm lazy here, useing the max= value to judge first run penalty, this might not be sufficient for some scenarios.

like image 146
peterchen Avatar answered Sep 29 '22 16:09

peterchen


Execute the function a few thousand times to get an accurate measurement.

A single measurement might be dominated by OS events or other random noise.

like image 39
S.Lott Avatar answered Sep 29 '22 14:09

S.Lott