Function Clock()
returns the amount of tics that this process have consumed. To access this function, you need to include: #include <time.h>
.
To get the same thing, only in seconds, you can use the CLOCKS_PER_SEC
macro, so you can do: Clock() / CLOCKS_PER_SEC
to get the number of seconds that this process took. But remember that you might loose some precision this way.
You might not need this exact functionality though... If you are looking for the exact time that has elapsed since the program has started, you (as far as I can remember) must use difftime()
function. You might be loosing some precision if you need exact tics, depending on your platform.
This way you must save the current time at the beginning of your application, and subtract it from the current time during the application.
#include <stdio.h>
#include <time.h>
time_t programstart;
int main ()
{
time(&programstart); //assign the current time
/*... the program ...*/
//get seconds since start
double secondssincestart = difftime(time(NULL), programstart);
printf ("the program took: %f seconds\n", secondssincestart);
return 0;
}
EDIT:
Since this post still gets some attention, it is important to note, that today's C++11 has standard, easy to use, and pretty handy library chrono
.