01-09-2018 08:24 AM
whats the correct timer to use in us, for measure code performance?
clock() or gettimeofday? I use both, but result are different. This is for zc706
diff=clock() - start ;
usec = diff * 1000000 / CLOCKS_PER_SEC;
perf_diff=(uint32_t)((program_timer.perf_stop.tv_usec) -( program_timer.perf_start.tv_usec));
printf( "start=%d, stop==%d, Performance time=%d us, Time clk %d us\n", (uint32_t)program_timer.perf_start.tv_usec, (uint32_t)program_timer.perf_stop.tv_usec, perf_diff
01-09-2018 10:21 AM
So the short answer to your question is - gettimeofday() is the right solution for your purpose.
Long answer - they are radically different. clock() measures "process time", ie the amount of time the process itself netted running plus its children. It is based in kernel's "rusage" - or process book keeping. I believe the result is given in seconds, which is not really the most precise unit to measure time.
gettimeofday() is wall clock. It is usually a VDSO (ie userspace) call that (usually) does not require a kernel switch, which would make the call return very fast, thus making it more precise. However gettimeofday() can be affected by time shifts that occur from time keeping (ie NTP/PTP), especially if there is a time warp (ie when the difference between network time and local time is so big, the OS will not even attempt to adjust the clock smoothly). So from time to time you can get a completely wrong measure.
The best way to compute time is reading the clock ticks from the CPU. On standalone you can use Xtime_GetTime() which will encapsulate these calls for you. On Linux, you have to do it in assembly - but you have to enable the PMU with a custom kernel module, which is a more involved solution.
01-09-2018 01:12 PM
thx, though I read online some recommend using clock() because its the CPU clk?
as for gettimeofday() I guess occasionally there is wrap around time which give wrong result?
01-09-2018 07:46 PM
clock() wraps around every 72 minutes on a system where CLOCKS_PER_SEC is defined as 1,000,000 (most Linux)
gettimeofday() wraps around every 68 years - next in 2038.
That said, none are suitable to be used as a "performance" clock. You should really use processor clock ticks (ARM) or an AXI Timer.
01-11-2018 06:53 AM
01-11-2018 09:30 AM - edited 01-11-2018 02:01 PM
@s002wjhw Use XTime_GetTime()
You have to include "xtime_l.h"
On Linux this module works - the code attached is for the Beaglebone but it is pretty much the same processor
09-10-2019 08:20 AM