Helloes!

I'm using getrusage to get the usertime and systemtime, but sometimes the times returned are negative -->
SELF:
user time: 1313366639 microseconds
system time: -1547562616 microseconds

I'm not sure I've understood getrusage correctly, so I'm posting my code in hope of getting a few tips on how to avoid the negative numbers... :o)

The output is supposed to be in microseconds, and I read that the output from tv_sec + tv_usec can be both in microseconds and seconds... How do I know which one I get?

struct rusage ru;
struct timeval tim;
getrusage(RUSAGE_SELF, &ru);
tim=ru.ru_utime; 
double sutime = (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0;

printf("SELF:\n"); 
printf("        user time: %d microseconds\n", sutime);

tim=ru.ru_stime;
double sstime = (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0;

printf("        system time: %d microseconds\n", sstime);

> printf(" user time: %d microseconds\n", sutime);
Go read the manual page for printf conversions again - %d is NOT for doubles.

If you're using gcc as your compiler, then add these flags to the command line gcc -W -Wall prog.c It will then tell you when the printf/scanf format doesn't match the parameters you pass.

commented: Thanks for the help! ;o) +1

Are you trying to printf() a double into an integer format specifier %d?
You could cast to an integer, but it looks like you are flirting with the integer size limits.

commented: Thanks for the help! ;o) +1

> printf(" user time: %d microseconds\n", sutime);
Go read the manual page for printf conversions again - %d is NOT for doubles.

If you're using gcc as your compiler, then add these flags to the command line gcc -W -Wall prog.c It will then tell you when the printf/scanf format doesn't match the parameters you pass.

*laughs* Stupid newbie error! hehe... Didn't even cross my mind as I haven't worked with doubles in C until now :oP

One more thing though, is the output correct? Is it in microseconds?

And thanks to both of you for helping me out on this :o)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.