C++ ctime clock() Function
Example
Measure how long it takes for the program to run:
clock_t before = clock();
int k = 0;
for(int i = 0; i < 100000; i++) {
k += i;
}
clock_t duration = clock() - before;
cout << "Duration: " << (float)duration / CLOCKS_PER_SEC << " seconds";
Try it Yourself »
Definition and Usage
The clock()
function returns a number representing the amount of time that has passed up to the moment that the function was called. It is mainly used to measure differences in time between two moments during the execution of the program.
The size of a unit of time depends on the computer and it can be converted to seconds by using the CLOCKS_PER_SEC
constant.
The clock()
function is defined in the <ctime>
header file.
Syntax
clock();
Parameters
None.
Technical Details
Returns: | A positive number representing the moment in time when the function is called. |
---|