Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to logging elapsed time in C++

I'm doing a article about GPU speed up in cluster environment
To do that, I'm programming in CUDA, that is basically a c++ extension. But, as I'm a c# developer I don't know the particularities of c++.

There is some concern about logging elapsed time? Some suggestion or blog to read.

My initial idea is make a big loop and run the program several times. 50 ~ 100, and log every elapsed time to after make some graphics of velocity.

like image 839
Custodio Avatar asked Dec 28 '22 20:12

Custodio


2 Answers

Depending on your needs, it can be as easy as:

time_t start = time(NULL);
// long running process
printf("time elapsed: %d\n", (time(NULL) - start));

I guess you need to tell how you plan this to be logged (file or console) and what is the precision you need (seconds, ms, us, etc). "time" gives it in seconds.

like image 71
Eduardo Costa Avatar answered Dec 30 '22 11:12

Eduardo Costa


I would recommend using the boost timer library . It is platform agnostic, and is as simple as:

#include <boost/timer/timer.hpp>
boost::timer t;
// do some stuff, up until when you want to start timing
t.restart();
// do the stuff you want to time.
std::cout << t.elapsed() << std::endl;

Of course t.elapsed() returns a double that you can save to a variable.

like image 44
MarkD Avatar answered Dec 30 '22 10:12

MarkD