clock() - I/O Function
How is "clock()" used in C++?
Explanation
clock() is an I/O function that returns the amount of time(milliseconds), the calling program is running. To get the value in seconds the it is divided by CLOCKS_PER_SEC. If the time is unavailable it returns "-1".
Syntax:
clock_t clock(void);
Example :
#include <iostream.h> #include <ctime.h> int main() { int j=0; while (j<=500) { cout << endl; j++; }; cout << "Number of clock ticks is:" << clock(); cout << "Time in Seconds is: " << clock()/CLOCKS_PER_SEC; return 0; } |
Result :
Amount of clock ticks: 16
Time in Seconds is: .016
In the above example clock() is used to find the clock ticks elapsed after the program execution. To Convert the time into seconds "CLOCKS_PER_SEC" macro is used.