Getting getrusage() to measure system time in C

后端 未结 3 352
甜味超标
甜味超标 2020-12-17 17:40

I would like to measure the system time it takes to execute some code. To do this I know I would sandwich said code between two calls to getrusage(), but I get some unexpect

相关标签:
3条回答
  • 2020-12-17 18:11

    Use gprof. This will give give you the time taken by each function. Install gprof and use these flags for compilation -pg -fprofile-arcs -ftest-coverage.

    0 讨论(0)
  • 2020-12-17 18:12

    You should use usage.ru_utime, which is user CPU time used, instead.

    0 讨论(0)
  • 2020-12-17 18:36

    You're misunderstanding the difference between "user" and "system" time. Your example code is executing primarily in user-mode (ie, running your application code) while you are measuring, but "system" time is a measure of time spent executing in kernel-mode (ie, processing system calls).

    ru_stime is the correct field to measure system time. Your test application just happens not to accrue any such time between the two points you check.

    0 讨论(0)
提交回复
热议问题