Correct way to logging elapsed time in C++

半腔热情 提交于 2020-12-30 10:40:21

问题


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.


回答1:


Standard functions such as time often have a very low resolution. And yes, a good way to get around this is to run your test many times and take an average. Note that the first few times may be extra-slow because of hidden start-up costs - especially when using complex resources like GPUs.

For platform-specific calls, take a look at QueryPerformanceCounter on Windows and CFAbsoluteTimeGetCurrent on OS X. (I've not used POSIX call clock_gettime but that might be worth checking out.)

Measuring GPU performance is tricky because GPUs are remote processing units running separate instructions - often on many parallel units. You might want to visit Nvidia's CUDA Zone for a variety of resources and tools to help measure and optimize CUDA code. (Resources related to OpenCL are also highly relevant.)

Ultimately, you want to see how fast your results make it to the screen, right? For that reason, a call to time might well suffice for your needs.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/5999219/correct-way-to-logging-elapsed-time-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!