# CPU vs GPU Timer Is CUDA Timer accurate ?

**URL:** <https://forums.developer.nvidia.com/t/cpu-vs-gpu-timer-is-cuda-timer-accurate/14955>\
**Category:** CUDA Programming and Performance\
**Created:** [February 16, 2010, 4:11am UTC](https://forums.developer.nvidia.com/t/cpu-vs-gpu-timer-is-cuda-timer-accurate/14955 "2010-02-16T04:11:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imperialguy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@imperialguy](https://forums.developer.nvidia.com/u/imperialguy)\
**Post date:** [February 16, 2010, 4:11am UTC](https://forums.developer.nvidia.com/t/cpu-vs-gpu-timer-is-cuda-timer-accurate/14955/1 "2010-02-16T04:11:55Z")

</div>

hi,

In a particular code, when I use CUDA timer and CPU timer, they give different results. For example,

cutCreateTimer(&gpu\_timer);  
cudaThreadSynchronize();  
cutStartTimer(gpu\_timer);

\<\<\< Runkernels here \>\>\>

cudaThreadSynchronize();  
cutStopTimer(gpu\_timer);  
double gpu\_time = cutGetTimerValue(gpu\_timer) \* 1e-3;

Now, I use CPU timers for the same thing

clock\_t gpu\_start = clock();

\<\<\< Runkernels here \>\>\>

double gpu\_time = ((double)clock() - gpu\_start) / (CLOCKS\_PER\_SEC);

The GPU Timer reports very less time (say) 10ms. While the CPU timer reports around 200ms.

Why is this happening ?

---

<div class="post-metadata">

**Author:** ![\_Big\_Mac](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Big\_Mac](https://forums.developer.nvidia.com/u/_Big_Mac)\
**Post date:** [February 16, 2010, 9:19am UTC](https://forums.developer.nvidia.com/t/cpu-vs-gpu-timer-is-cuda-timer-accurate/14955/2 "2010-02-16T09:19:17Z")

</div>

CUDA timers are much more precise than clock(). clock() has worse resolution and such timing will give incorrect results unless the time difference is in the order of seconds. I’ve also had issues with using clock() in a multithreaded app under Linux.

To get proper timings for short tasks use system-specific functions (gettimeofday on Linux, QueryPerformanceCounter on Windows) or a good timing library that wraps around them (like CUDA timers do AFAIK).

---

<div class="post-metadata">

**Author:** ![cuda2010](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cuda2010](https://forums.developer.nvidia.com/u/cuda2010)\
**Post date:** [February 19, 2010, 5:45am UTC](https://forums.developer.nvidia.com/t/cpu-vs-gpu-timer-is-cuda-timer-accurate/14955/3 "2010-02-19T05:45:46Z")

</div>

I guess the cutil timing function cutGetTimerValue() is just a wrap of QueryPerformanceCounter().  
Is it?

---

<div class="post-metadata">

**Author:** ![\_Big\_Mac](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@\_Big\_Mac](https://forums.developer.nvidia.com/u/_Big_Mac)\
**Post date:** [February 19, 2010, 10:13am UTC](https://forums.developer.nvidia.com/t/cpu-vs-gpu-timer-is-cuda-timer-accurate/14955/4 "2010-02-19T10:13:13Z")

</div>

If it works as NVIDIA’s shared utils for OpenCL (and it probably does) then it wraps around QueryPerformanceCounter() under Windows and gettimeofday() under Linux. Both of these system-specific functions give very high resolution host timing.
