Performance profiler: how to use unsigned long to measure time in microseconds?

I use unsigned long to collect performance data when profiling the application as follows

  • unsigned long time_taken = (unsigned long) ((finish_time_in_nano_sec - start_time_in_nano_sec)/1000);

  • Write time_taken to a file. Do this for every function called in my application.

  • Now, after running the application, ask a separate program to read the above file and summarize the time spent on each function, including the number of calls, for example

func_name aggregate_time call_counts

The problem is that for some functions, the aggregate_time field is a 20-digit value, i.e. The maximum value that an unsigned long can contain. This cannot be true, because I measure the time in microseconds and do not start my application for more than 20 seconds. How can this be a 20 digit value?

Do you see errors in steps 1,2 and 3?

Regards, Krishna

EDIT:

1) Time measurement: clock_gettime (CLOCK_REALTIME, & start_time); clock_gettime (CLOCK_REALTIME, & end_time); unsigned long time_taken = (unsigned long) ((finish_time.tv_nsec - art_time.tv_nsec) / 1000);

2) File recording: fwrite (& time_taken, sizeof (unsigned long), 1, data file);

3) The file has been read: fread (& time_taken, sizeof (long), 1, data file);

+3
3

:

clock_gettime(CLOCK_REALTIME, &start_time);
clock_gettime(CLOCK_REALTIME, &finish_time);
unsigned long time_taken = (unsigned long)((finish_time.tv_nsec - start_time.tv_nsec)/1000);

1- start_time finish_time, .

tv_sec:

unsigned long time_taken;
time_taken = (unsigned long)((finish_time.tv_sec - start_time.tv_sec) * 1000000);
time_taken += (unsigned long)(finish_time.tv_nsec / 1000);
time_taken -= (unsigned long)(start_time.tv_nsec / 1000);

(, 23989032,452 struct timespec .tv_sec = 23989032 .tv_nsec = 452000000)

+5

, , .

, :

+1

, . , , , , RDTSC, , CPU. AMD ​​ , , , , , .

, , , Win32 API SetProcessAffinityMask.

EDIT: , . API , . RDTSC .

: - - , . , , , , , , , fwrite. , , ( , , fwrite, fwrite).

0

Source: https://habr.com/ru/post/1780788/


All Articles