Home
Support

Knowledge Base

BSPs and drivers
Community resources
Product documentation
Questions?
Contact us

Accurate time measurements in code
 
________________________________________________________________________

Applicable Environment
________________________________________________________________________
  • Topic: Accurate time measurements in code
  • SDP: 6.5.0
  • Target: All supported targets
________________________________________________________________________

Question
________________________________________________________________________

How can I measure the amount of time it takes to run a set of instructions?

________________________________________________________________________

Solution
________________________________________________________________________

If you are unable to use the IDE to obtain a measurement of the amount of time a set of instructions are taking you can use the kernel call ClockCycles() and grab the number of CPU clock cycles and compare it to an earlier value to obtain the total elapsed cycles. Once you have this though you will likely want to know how long a clock cycle actually takes on your system as they vary between systems. This can be done via a syspage entry. Here is a simple example showing the ClockCycles as a measurement tool.

#include <sys/neutrino.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syspage.h>

int main( void )
{
uint64_t cps, cycle1, cycle2, ncycles;
float sec;
cycle1=ClockCycles( ); /* snap the time */
printf("foo\n"); /* do something */
cycle2=ClockCycles( ); /* snap the time again */
ncycles=cycle2-cycle1;
printf("%lld cycles elapsed \n", ncycles);

/* find out how many cycles per second */
cps = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
printf( "This system has %lld cycles/sec.\n",cps );
sec=(float)ncycles/cps;
printf("The printf function took %lld cycles which was %f seconds\n",ncycles, sec);

return EXIT_SUCCESS;
}

Note that the code above may not be accurate when running on modern x86 machines due to out-of-order execution. Intel suggests two different methods of improving measurement accuracy [download.intel.com/embedded/software/IA/324264.pdf]. The attached C file contains sample code implementing Intel's recommendation to use the "RDTSCP" instruction.

________________________________________________________________________
If you require assistance with any of these steps, please reach out to QNX Technical Support.
________________________________________________________________________


Related Attachments
 Attachment Name Size Last Modified
 main.c 3KB 2/5/2013 9:31 PM





Please contact us with your questions or concerns.