# Clock() function in Optix

**URL:** <https://forums.developer.nvidia.com/t/clock-function-in-optix/170161>\
**Category:** OptiX\
**Created:** [March 5, 2021, 3:49pm UTC](https://forums.developer.nvidia.com/t/clock-function-in-optix/170161 "2021-03-05T15:49:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sukumar.srikanth](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sukumar.srikanth](https://forums.developer.nvidia.com/u/sukumar.srikanth)\
**Post date:** [March 5, 2021, 3:49pm UTC](https://forums.developer.nvidia.com/t/clock-function-in-optix/170161/1 "2021-03-05T15:49:40Z")

</div>

The clock function in Optix is not working as expected.  
I am using Optix 6.5. I am using RTX 2080 TI. I obtained the clock frequency via _cudaDevAttrClockRate_ (154500)  
For testing I used OptixHello from the samples and reduced my launch dimensions to 1x1.  
I computed clock function on both CPU and GPU. I applied a delay in my GPU code.  
The result of the time difference / clockRate that I get in the GPU is incorrect.  
Code snippet for reference

```auto
  long long int startTotalTime = clock64();
  long long int delayTime = 10000000000;
  while (clock64() < (startTotalTime + delayTime));
  result_buffer[launch_index] = make_float4(draw_color, 0.f);
  long long int endTotalTime = clock64();
  double totalTime = ((double)(endTotalTime - startTotalTime)) / 154500.0;
  printf("Total time GPU in ms %lf\n", totalTime);

```

The result that I get is  
Total time GPU in ms 64724.929042  
Total time CPU in ms 5299.000000

What I infer is that the clockRate that I use is probably incorrect.  
What is the clock rate I need to be using ?

---

<div class="post-metadata">

**Author:** ![dhart](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/dhart/32/14043_2.png) [@dhart](https://forums.developer.nvidia.com/u/dhart)\
**Post date:** [March 5, 2021, 10:08pm UTC](https://forums.developer.nvidia.com/t/clock-function-in-optix/170161/2 "2021-03-05T22:08:41Z")

</div>

Hi there, the function `clock()` is a CUDA function, and there’s a CUDA sample demonstrating how to use it properly in the CUDA SDK samples named 0\_Simple/clock\_nvrtc. Documentation is here: [https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#time-function](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#time-function)

The docs mention that `cudaDevAttrClockRate()` returns the peak clock rate in kilohertz, which might not the be your current clock rate. You can lock your clock rate to a specific value for profiling and timings using `nvidia-smi`.

–  
David.

---

<div class="post-metadata">

**Author:** ![sukumar.srikanth](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sukumar.srikanth](https://forums.developer.nvidia.com/u/sukumar.srikanth)\
**Post date:** [March 9, 2021, 8:11am UTC](https://forums.developer.nvidia.com/t/clock-function-in-optix/170161/3 "2021-03-09T08:11:01Z")

</div>

Hi David,

Thanks for the direction, indeed I use CUDA sample example for clock.  
I managed to lock my clock rate as you suggested and indeed it does give me timings that make sense.

Sukumar

---

<div class="post-metadata">

**Author:** ![nadeemm](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nadeemm/32/367020_2.png) [@nadeemm](https://forums.developer.nvidia.com/u/nadeemm)\
**Post date:** [October 12, 2021, 6:11pm UTC](https://forums.developer.nvidia.com/t/clock-function-in-optix/170161/4 "2021-10-12T18:11:32Z")

</div>

Closed on October 12, 2021, 6:11pm UTC


