-1

looking for assistance on getting the real, user, sys times of functions within my C program. For instance how long it took to read in a file. I have been looking at the #include and using the time() function with the -p flag but I am striking out on the execution of this. I guess my question is if I have:

time_t total_time;
time_t start, end;

start = time(-p, &start);

<some code>

end = time(-p, &end);

printf("real = %e, user = %S, sys = %S\n", ???????????);

I understand the differences between the three times, just don't know the proper execution of getting the results.

user207421
  • 305,947
  • 44
  • 307
  • 483
LD50
  • 19
  • 3
  • Your question has nothing to do with [tag:real-time]. Don't tag indiscriminately, and don't misuse standard terminology. – user207421 Oct 30 '16 at 00:28

2 Answers2

0

Check time()'s return value:

The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime, gmtime or difftime).

To display the real time, read this answer.

For user and system, read: How to measure user/system cpu time for a piece of program?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Ive read those posts and a whole lot more including the linux man page for time(). I understand what it does, and what each value represents. Im having a problem implementing it. I can not use clock() since I have to run this code on a really old machine and it does not like the clock() func. Thank you for your input though it is much appreciated. – LD50 Sep 22 '16 at 23:57
  • You are welcome @LD50, I upvoted your question, in hopes to gain attention! Wait to see if another answer gets posted. If not, then consider accepting my answer. – gsamaras Sep 22 '16 at 23:59
0

The real elapsed time can be determined by calling time() before and after, and subtracting the results. If you want to be portable, you should use the difftime function to perform the subtraction, because the return value from time() is not guaranteed to be a number.

If you are using a POSIX compliant system, however, the return value from time() will be the number of seconds since the epoch, so you could just subtract them to get the elapsed seconds.

If you wanted a higher resolution result, you could use gettimeofday(), which returns a timeval struct containing millisecond resolution time.

The most portable way to access the user & system times would be to call the clock() C library function, however you have noted in your comments to @gsamaras that your system does not have this function.

Depending on the system you are working with, you may be able to call the system functions times() or getrusage(). getrusage() would be easier to use, because it returns the values as timeval structs, which contain seconds and milliseconds. times() returns clock ticks which you would have to convert if you wanted actual time units.

If your program is threaded, and you are using Linux, getrusage() offers an additional advantage: you can get the resource consumption for the current thread.

Whichever function you choose the process would be to obtain the initial reading, the final reading, and subtract the two results to see the time consumed by your function.

harmic
  • 28,606
  • 5
  • 67
  • 91