“rusage” statistics “rusage” statistics unix unix

“rusage” statistics


What is the purpose of:

while (usec >= 1000)    usec /= 10;

I gather that you want the most significant three digits of the usec; in that case, the most straightforward way I can think of is to divide usec by 1000, and be done with that.

Test cases:

  • 999999 ⇒ 999
  • 99999 ⇒ 999 (should be 099)
  • 9999 ⇒ 999 (should be 009)
  • 999 ⇒ 999 (should be 000)


I think there's probably a bug somewhere in your composition of sec and usec. I can't really say what exactly without knowing the kinds of errors you're seeing. A rough guess would be that usec can never be > 999999, so you're relying on overflow to know when to adjust sec. It could also just be a problem with your duration output format.

Anyway. Why not store the utime and stime components as float seconds rather than trying to build your own rusage on output? I'm pretty sure the following will give you proper seconds.

static int timeval_diff_ms(timeval const& end, timeval const& start) {    int micro_seconds = (end.tv_sec  - start.tv_sec) * 1000000         + end.tv_usec - start.tv_usec;    return micro_seconds;}static float timeval_diff(timeval const& end, timeval const& start) {    return (timeval_diff_ms(end, start)/1000000.0f);}

If you want to decompose this back into an rusage, you can always int-div and modulo.