5

I want to measure the execution time of a c-code segment using Linux. I take one timestamps at the beginning of the code segment and one at the end. But I don't know how to protect the code against IRQs and context switches to high prior tasks. The program runs in user space! The code segment is short so don't panic hosing the system.

Does anyone know an easy solution for this kind of protection?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Fox0815
  • 53
  • 1
  • 3
  • Which meaningful problem are you trying to solve? – autistic Apr 27 '13 at 17:17
  • notice when you roll-over on your tag 'time-mea..', there are 0 followers. There are tags for testing and performance that have more followers. Good luck. – shellter Apr 27 '13 at 17:27
  • 1
    There no way for user space code to do such stuff. Probably, the best thing you could do is measuring like 10000 times and taking the average. The other way around would be emulating the CPU. Also you could measure other things than time. – Powerslave Apr 27 '13 at 17:29
  • Also, `clock_gettime` with `CLOCK_PROCESS_CPUTIME_ID` or `CLOCK_THREAD_CPUTIME_ID` if available. – Daniel Fischer Apr 27 '13 at 17:40
  • Are command line options acceptable? Try `time [args]` or `/usr/bin/time -v [args]` – GRAYgoose124 Apr 27 '13 at 20:11

1 Answers1

3

You can use getrusage(2) to get the CPU time used, rather than just measuring real time. That should get you the answer you want without having to resort to funny business like blocking other programs from running.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469