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.