# Compare GPU and CPU function time

**URL:** <https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542>\
**Category:** CUDA Programming and Performance\
**Created:** [May 19, 2011, 10:51pm UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542 "2011-05-19T22:51:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![batmanzsolti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@batmanzsolti](https://forums.developer.nvidia.com/u/batmanzsolti)\
**Post date:** [May 19, 2011, 10:51pm UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/1 "2011-05-19T22:51:56Z")

</div>

Hi!

I have a CPU and a GPU function. I want to compare these functions running time. I use NVIDIA Visual Profiler, but it only shows the GPU time.  
Is there any way to compare these function running time?

---

<div class="post-metadata">

**Author:** ![schmichael](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@schmichael](https://forums.developer.nvidia.com/u/schmichael)\
**Post date:** [May 20, 2011, 4:33am UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/2 "2011-05-20T04:33:59Z")

</div>

I have been using this, which is an amalgam of code snippets from various places (if anyone knows a better way, or if this is wrong, please share!):

[EDIT1] I think you need to #include to use CLOCKS\_PER\_SEC

[EDIT2] And as far as I could tell, CPU time reports in microseconds. That’s why I multiply by 1000 to have it in same units as the CUDA timing, which returns in milliseconds

```auto
float elapsed_cpu, elapsed_gpu;

	// begin timing CPU

	clock_t t1, t2;

	t1 = clock();

	... // CPU function here

	t2 = clock();

	elapsed_cpu = ((float)t2 - (float)t1) / CLOCKS_PER_SEC * 1000;	//cpu elapsed time in ms

	// begine timing GPU

	cudaEvent_t start, stop;

	cudaEventCreate(&start);

	cudaEventCreate(&stop);

	cudaEventRecord(start, 0);

	... // GPU launch here

	cudaEventRecord(stop, 0);

	cudaEventSynchronize(stop);

	cudaEventElapsedTime(&elapsed_gpu, start, stop);

	cudaEventDestroy(start); 

	cudaEventDestroy(stop);

```

---

<div class="post-metadata">

**Author:** ![batmanzsolti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@batmanzsolti](https://forums.developer.nvidia.com/u/batmanzsolti)\
**Post date:** [May 20, 2011, 9:21am UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/3 "2011-05-20T09:21:38Z")

</div>

> [@](#):
>
> I have been using this, which is an amalgam of code snippets from various places (if anyone knows a better way, or if this is wrong, please share!):
> 
> [EDIT1] I think you need to #include to use CLOCKS\_PER\_SEC
> 
> [EDIT2] And as far as I could tell, CPU time reports in microseconds. That’s why I multiply by 1000 to have it in same units as the CUDA timing, which returns in milliseconds
> 
> ```auto
> float elapsed_cpu, elapsed_gpu;
> 
> // begin timing CPU
> 
> clock_t t1, t2;
> 
> t1 = clock();
> 
> ... // CPU function here
> 
> t2 = clock();
> 
> elapsed_cpu = ((float)t2 - (float)t1) / CLOCKS_PER_SEC * 1000;	//cpu elapsed time in ms
> 
> // begine timing GPU
> 
> cudaEvent_t start, stop;
> 
> cudaEventCreate(&start);
> 
> cudaEventCreate(&stop);
> 
> cudaEventRecord(start, 0);
> 
> ... // GPU launch here
> 
> cudaEventRecord(stop, 0);
> 
> cudaEventSynchronize(stop);
> 
> cudaEventElapsedTime(&elapsed_gpu, start, stop);
> 
> cudaEventDestroy(start); 
> 
> cudaEventDestroy(stop);
> 
> ```

Thank You!

But when I print the elapsed times the elapsed\_cpu’s decimals is always zeros. Example:

```auto
printf("\nCPU:%f, GPU:%f",elapsed_cpu, elapsed_gpu);

```

CPU:17.000000, GPU:5.772768

CPU:17.000000, GPU:5.781632

CPU:17.000000, GPU:5.798272

---

<div class="post-metadata">

**Author:** ![cricri1](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cricri1](https://forums.developer.nvidia.com/u/cricri1)\
**Post date:** [May 20, 2011, 12:24pm UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/4 "2011-05-20T12:24:58Z")

</div>

on cpu some function give ms 0 15 ms 31 ms … so no decimale  
on visual basic it s  
Public Declare Function timeGetTime Lib “winmm.dll” () As Long  
for have 1 ms need  
Public Declare Function timeBeginPeriod Lib “winmm.dll” (ByVal uPeriod As Long) As Long  
timebeginperiod 1

some function can give better on my computer can give 1/266001 second  
Public Declare Function QueryPerformanceCounter Lib “kernel32” Alias “QueryPerformanceCounter” (lpPerformanceCount As currency) As Long  
Public Declare Function QueryPerformanceFrequency Lib “kernel32” Alias “QueryPerformanceFrequency” (lpFrequency As currency) As Long

QueryPerformanceFrequency tt1  
QueryPerformanceCounter tt2  
QueryPerformanceCounter tt3  
MsgBox (tt3 - tt2) / tt1 \* 1000 & “ms”

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [May 20, 2011, 2:46pm UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/5 "2011-05-20T14:46:49Z")

</div>

On unixy systems gettimeofday() with it’s microsecond resolution often is the best timer (note that other timers might have worse resolution even if they use microseconds as units). So I regularly have code like the following to time stuff:

```auto
#include <sys/time.h>

	struct timeval start_time, end_time;

	gettimeofday(&start_time, NULL);

	// do some stuff

	gettimeofday(&end_time, NULL);

	float runtime = (end_time.tv_sec-start_time.tv_sec)

	                + (end_time.tv_usec-start_time.tv_usec) * 1e-6;

	printf("stuff took %f seconds to execute.\n", runtime);

```

---

<div class="post-metadata">

**Author:** ![MichaA\_Skrzypkowski](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MichaA\_Skrzypkowski](https://forums.developer.nvidia.com/u/MichaA_Skrzypkowski)\
**Post date:** [May 23, 2011, 11:08am UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/6 "2011-05-23T11:08:53Z")

</div>

For CPU code I use Qt library.

```auto
QTime t;

t.start();

algorithm->runH();

elapsed_time_h = t.elapsed();

total_time_h += elapsed_time_h;

```

I works pretty good. You can also use some other library, like BOOST.

It’s a better solution if you are targeting many platforms.

---

<div class="post-metadata">

**Author:** ![schmichael](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@schmichael](https://forums.developer.nvidia.com/u/schmichael)\
**Post date:** [May 30, 2011, 5:40am UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/7 "2011-05-30T05:40:18Z")

</div>

> [@](#):
>
> But when I print the elapsed times the elapsed\_cpu’s decimals is always zeros. Example:
> 
> ```auto
> printf("\nCPU:%f, GPU:%f",elapsed_cpu, elapsed_gpu);
> 
> ```
> 
> CPU:17.000000, GPU:5.772768
> 
> CPU:17.000000, GPU:5.781632
> 
> CPU:17.000000, GPU:5.798272

I believe this is because the standard C [font=“Courier New”]clock()[/font] function only has granularity down to seconds. If you need to time sub-second timeframes, you will need to use a custom timer implementation as suggested.

I should have mentioned this limitation with my method, but it was OK for me since I’m timing things in the order of 100’s of seconds at least, so the error is less than 1% for my purposes.

Cheers,

Mike

---

<div class="post-metadata">

**Author:** ![schmichael](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@schmichael](https://forums.developer.nvidia.com/u/schmichael)\
**Post date:** [May 30, 2011, 5:47am UTC](https://forums.developer.nvidia.com/t/compare-gpu-and-cpu-function-time/22542/8 "2011-05-30T05:47:03Z")

</div>

You might find the discussion [here](http://stackoverflow.com/questions/588307/c-obtaining-milliseconds-time-on-linux-clock-doesnt-seem-to-work-properly) useful. As has been suggested, Qt and BOOST are discussed, along with some custom classes.
