【发布时间】:2016-11-14 22:23:43
【问题描述】:
我有相同算法的 cuda 和 cpp 实现。在 CUDA 中,我使用事件进行时间测量:
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0); // start time measurement
// some cuda stuff
cudaEventRecord(stop, 0); // stop time measurement
cudaEventSynchronize(stop); // sync results
cudaEventElapsedTime(&time, start, stop);
printf ("Elapsed time : %f ms\n", time);
在 c++ 中,我使用 timeofday 进行测量:
struct timeval start, end;
long seconds, useconds;
float mseconds;
gettimeofday(&start, NULL);
// some work to do
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mseconds = (seconds * 1000 + useconds/1000.0) + 0.5;
printf ("Elapsed time : %f ms\n", mseconds);
这是获得良好可比结果的正确方法吗?
提前致谢!
【问题讨论】:
-
如果这对您来说足够好,那么为了比较就可以了(查看您的毫秒精度,无需关心较长的运行时间)。如果您想要 C++ 标准方式、C++11 及更高版本的内容,请参阅std::chrono -
steady_clock长时间(避免在此过程中调整系统日期)或high_resolution_clockas-good-a-precision-as -your-C++-standard-lib-and-OS-can-provide.