1

so I was using rusage to print out how long it takes from the user, and system to process a command, something along the lines of,

 //DO STUFF HERE printf(" TOTAL TIMES: "); tusage.ru_utime.tv_sec = rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec; 
24
  • What's the problem with using getrusage in linux? Commented Mar 14, 2013 at 8:02
  • use gettimeofday, see this Commented Mar 14, 2013 at 8:07
  • @Art the same code prints 0.00 in linux, but same code prints actual value in mac os why is that? Commented Mar 14, 2013 at 8:07
  • @RichardMckenna, because linux is fast ;D Commented Mar 14, 2013 at 8:08
  • @RichardMckenna because your program didn't take any measurable time to run? Your exact example works for me (even if I question why you'd use a struct rusage for storage for the sum instead of just a struct timeval). Commented Mar 14, 2013 at 8:09

1 Answer 1

1

Try this:

#include <stdio.h> #include <sys/resource.h> int main() { struct rusage rusage; struct rusage tusage; int i, j, r=0; for (i = 0; i < 10000; i++) { for (j = 1; j < 100000; j++) { r = i % j + i / j; } } getrusage(RUSAGE_SELF, &rusage); printf("TOTAL TIME \n"); tusage.ru_utime.tv_sec = rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec; tusage.ru_utime.tv_usec = rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec; tusage.ru_utime.tv_sec += tusage.ru_utime.tv_usec / 1000000; tusage.ru_utime.tv_usec = tusage.ru_utime.tv_usec % 1000000; printf("%ld.%06ld\n", tusage.ru_utime.tv_sec, tusage.ru_utime.tv_usec); return r; } 
Sign up to request clarification or add additional context in comments.

2 Comments

no it did not. But why does something on a mac take long yet all my realistic commands are showing up as 0.00
I don't really know, just a call to getrusage returns non-zero time for me (0.001000).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.