C++: Timing in Linux (using clock()) is out of sync (due to OpenMP?)

◇◆丶佛笑我妖孽 提交于 2019-11-27 16:23:38

user 0m45.735s

clock() measures CPU time the process used (as good as it can) per 7.27.2.1

The clock function returns the implementation’s best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation.

and not wall clock time. Thus clock() reporting a time close to the user time that time reports is normal and standard-conforming.

To measure elapsed time, if you can assume POSIX, using clock_gettime is probably the best option, the standard function time() can also be used for that, but is not very fine-grained.

I would suggest clock_gettime using CLOCK_MONOTONIC for the clock.

Depending on your specific system, that should give near-microsecond or better resolution, and it will not do funny things if (e.g.) someone sets the system time while your program is running.

I would suggest that for benchmarking inside OpenMP applications you use the portable OpenMP timing function omp_get_wtime(), which returns a double value with the seconds since some unspecified point in the past. Call it twice and subtract the return values to obtain the elapsed time. You can find out how precise time measurements are by calling omp_get_wtick(). It returns a double value of the timer resolution - values closer to 0.0 indicate more precise timers.

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