There are three methods to calculate the computing time in CUDA:
<1> use the functions in cutil. h
Unsigned int timer = 0; // create a timer
CutCreateTimer (& timer); // start timing
CutStartTimer (timer );
{
// Code segment for Statistics
............
}
// Stop timing
CutStopTimer (timer );
// Obtain the time from start time to stop
CutGetTimerValue (timer );
// Delete the timer Value
CutDeleteTimer (timer );
<2> clock function in time. h
Clock_t start, finish;
Float costtime;
Start = clock ();
{
// Code segment for Statistics
............
}
Finish = clock ();
// Obtain the time difference between the two records
Costtime = (float) (finish-start)/CLOCKS_PER_SEC;
The length of the timer unit is 1 millisecond.Precision is also 1 ms.
<3> event
CudaEvent_t start, stop;
CudaEventCreate (& start );
CudaEventCreate (& stop );
CudaEventRecend (start, 0 );
{
// Code segment for Statistics
............
}
CudaEventRecord (stop, 0 );
Float costtime;
CudaEventElapsedTime (& costtime, start, stop );
CudaError_t cudaEventCreate (cudaEvent_t * event) --- create an event object;
CudaError_t cudaEventRecord (cudaEvent_t event, CUstream stream) --- records events;
CudaError_t cudaEventElapsedTime (float * time, cudaEvent_t start, cudaEvent_t end) --- calculate the time difference between two events;
CudaError_t cudaEventDestroy (cudaEvent_t event) --- destroys the event object.
Calculate the time difference between two events (In milliseconds, the precision is0.5Microseconds).
If no event is recorded, this function returns cudaErrorInvalidValue. If any event in the record uses a non-zero stream, the result is uncertain.
Let's take a look at what timing is used in the template when cuda is created:
Unsigned int timer = 0;
CutilCheckError (cutCreateTimer (& timer ));
CutilCheckError (cutStartTimer (timer ));
HelloCUDA <1, 1, 0> (device_result, 11 );
CutilCheckMsg ("Kernel execution failed \ n ");
CudaThreadSynchronize ();
CutilCheckError (cutStopTimer (timer ));
Printf ("Processing time: % f (MS) \ n", cutGetTimerValue (timer ));
CutilCheckError (cutDeleteTimer (timer ));
The precision here is ms.
If you are interested, you can give it a try ~