matlab mex-file timing kernel i need to know the time

i’m writing a matlab mex-file in cuda. I havo to optimize a kernel execution, so i need to know the time that some parts of the kernel take to execute.
In wich way i can do this?


You can try the even-based time measuring

/ * initialization */
cudaEvent_t start, stop;
float elapsedTime;

/* Call function on GPU */
cudaEventRecord(start, 0);

Put your kernel call here

cudaEventRecord(stop, 0);
cudaEventElapsedTime(&elapsedTime, start, stop);

/* Print time */

printf("GPU execution time with event = %f ms\n", elapsedTime );

OR, using a timer:

unsigned int timer = 0;
CUT_SAFE_CALL( cutCreateTimer( &timer ) );
CUT_SAFE_CALL( cutResetTimer( timer ) );

CUT_SAFE_CALL( cutStartTimer( timer ) );

    /* Call function on GPU */
   Put your kernel call here

   CUT_SAFE_CALL( cutStopTimer( timer ) );

   printf("GPU execution time = %f ms\n",cutGetTimerValue( timer ) );

Thanks a lot