How do you compute the time (in MS) of execution of a portion of code in C++ ?
-
Either use a profiler or insert calls to a timer function before/after the code section of interest. – Paul R Jun 25 '12 at 22:34
-
6-1 for showing no research effort. – Jesse Good Jun 25 '12 at 22:35
-
`
` works great if you have C++11. It might be a bit hard to understand if you've never used it, but it's not bad once you get going. – chris Jun 25 '12 at 22:37 -
similiar question: http://stackoverflow.com/questions/3400309/find-c-execution-time – Marek Kwiendacz Jun 25 '12 at 22:43
-
I think someone posted similar question in Stackoverflow: [http://stackoverflow.com/questions/307596/time-difference-in-c] – Manto Jun 25 '12 at 22:43
3 Answers
Most systems support a high performance timing mechanism. On Windows you can use the high performance timer API:
On *nix systems you can use clock_getres() and clock_gettime()
.
You should be able to figure out how to use those to time your code.

- 122,712
- 22
- 185
- 265
About the best you can do in portable code is to time with clock()
.
clock_t start = clock();
// code to time goes here
clock_t stop = clock();
double seconds = double(stop-start)/CLOCKS_PER_SEC;
C++11 adds a new header named <chrono>
with classes for time_point
and duration
that can make the job simpler and cleaner. None of these, however, guarantees millisecond-level accuracy (or even precision). The new classes have typedefs for duration down to the nanosecond range, but no guarantee about whether your real results will be that accurate or not (but with most typical OSes, I'm pretty sure the answer is usually "not").

- 476,176
- 80
- 629
- 1,111
-
I wouldn't sat "the best". You'd be better off `#ifdef`ing OS specific functions as `clock()` can have a rather low resolution – Ed S. Jun 25 '12 at 22:52
-
`#ifdef`ing OS-specific functions is (by definition) not portable code any more. The resolution of `clock` varies, but certainly *can* be quite low. For portable code, it's still about the best you can do though. They're not really intended for this purpose, but can be put to this use anyway. – Jerry Coffin Jun 25 '12 at 22:53
-
Ok, yet in practice it results in code that works just fine and has better capability to measure small time spans. But yes, it's not technically portable as it is non-standard. – Ed S. Jun 25 '12 at 22:54
Here is what I use for c++ (not 11), but many libraries might have more elaborate solutions. For the code you need Qt but it could easily be done without. Might be you also need to replace CLOCK_MONOTONIC depending on your operating system.
#ifndef PROFILER_H
#define PROFILER_H
#include <sys/time.h>
#include <QString>
class Profiler
{
public:
Profiler(QString const& name);
long measure() const;
long measureNs() const;
double measureMs() const;
double measureS() const;
void printNs() const;
void printMs() const;
void printS() const;
private:
QString mName;
timespec mTime;
};
#endif // PROFILER_H
#include "profiler.h"
#include <QDebug>
#include <assert.h>
#include <iostream>
Profiler::Profiler(QString const& name):mName(name){
clock_gettime(CLOCK_MONOTONIC, &mTime); // Works on Linux
}
long int Profiler::measureNs() const{
timespec end;
clock_gettime(CLOCK_MONOTONIC, &end); // Works on Linux
long int diff = (end.tv_sec-mTime.tv_sec) * 1000000000 + (end.tv_nsec - mTime.tv_nsec);
assert(diff>0);
return diff;
}
double Profiler::measureMs() const{
return measureNs()/1000000.0;
}
double Profiler::measureS() const{
return measureMs()/1000.0;
}
void Profiler::printNs() const{
qDebug() << mName << "Time elapsed:" << measureNs() << "ns";
}
void Profiler::printMs() const{
qDebug() << mName << "Time elapsed:" << measureMs() << "ms";
}
void Profiler::printS() const{
qDebug() << mName << "Time elapsed:" << measureS() << "S";
}
Usage:
Profiler pro("Tag you want");
function();
pro.printMs();

- 4,738
- 4
- 28
- 57