Updated April 5, 2023
Introduction to clock() C++
clock() in C++ is defined as a function in the C++ header file that enables the developer to use the returned value of the approximate time for processing the program. This returned value from the function is then used by developers in order to find out the elapsed time. In order to achieve the functionality of computing the processor time, we call the clock() function at consecutive intervals. This interval is determined by adjudging the block of code for which we would need to find out the processing time. With the 2 values that are returned from different time scopes of the program, they are subtracted with the former minus the latter. In this article we will look at the syntax of how clock() variable is using in the C++ programming language and what variety does it bring to the table.
Syntax
Declaration of clock variable in C++:
clock_t variable_name
Here clock_t is the alias of a fundamental arithmetic data type that records the clock tick counts. The unit of the data type is the time of a constant.
Calling the clock function in C++:
variable_name = clock(< void >)
The argument which the function takes in is void type, which essentially means that there is no argument that is passed to the function while calling or declaring it, and the variable is cast to type clock_t.
Macro or constant involved in clock function in C++.
CLOCKS_PER_SEC
This is a macro in C++ that is extended in order to represent the number of clock tick that happens in a second. Dividing by this macro will yield a number of seconds in order to denote the processing time of a program.
How clock() works in C++?
In the introduction, we got to know that the function returns the processor time that is taken by a program for it to execute completely. This is also dependent on the fact of how the resources are allocated by the operating system. In cases where the process is run in multiple threads the return value from clock() may be ahead of the actual clock. In the opposite scenario, if there is a presence of other processes the clock() function may be lagging from the actual clock speed. And as a result of the same, the output we get from the clock() function might be less or more than the actual time clock. When the clock() function is called it refers to the ctime header file. ctime header file contains the definitions of various functions and all these functions have common functionality. The functionality in this header file is about manipulating data and time information on the program where any of these functions are called. Not only this, the header files also contain various macros, and one of these macros we would use to convert the clock ticks into minutes.
Now after the header file is referred to, clock() function gets the command of getting all the prerequisites and then gets back the approximate processing time that is taken by the program till the point the function is called. In case there is a failure, we would get a value of -1.
Now that we get the time at 2 intervals i.e. one at the start of the program and the other at the end of the block of the program of which we want to determine the processing time, we subtract the 2 values in order to get the difference of the processing time. Now, in order to convert the difference in time to seconds, we would have to divide it by a system-specific length that is described as a variable in CLOCKS_PER_SEC.
The formula for calculating the processing time in seconds is:
(T end – T start) / CLOCKS_PER_SEC
Examples
Here are the following examples mention below.
Example #1
Processing time of a remainder function which is an inbuilt function in C++.
Code:
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main ()
{
float x,y;
clock_t clockVariable;
// Using remainder function
clockVariable = clock();
for(int i=1; i<100000000; i++)
{
y = i%100000000;
}
clockVariable = clock() - clockVariable;
//cout<<y;
cout << "Using mod operator, processing of a loop of 100000000 integers took " << (float)clockVariable/CLOCKS_PER_SEC << " seconds" << endl;
// Without remainder function
clockVariable = clock();
for(int i=1; i<100000000; i++)
{
y = i - round(i/100000000)*100000000;
}
clockVariable = clock()- clockVariable;
cout << "Without using mod operator, processing of same 100000000 integers for loop took " << (float)clockVariable/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}
Output:
Example #2
Finding the hypotenuse of 100000 random triangles (the triangles are identical in both the processing).
Code:
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main ()
{
float x,y;
clock_t clockVariable;
// Using remainder function
clockVariable = clock();
for(int i=1; i<100000; i++)
{
y = hypot(100000,i);
}
clockVariable = clock() - clockVariable;
//cout<<y;
cout << "Using hypot operator, processing of a loop of 100 integers took " << (float)clockVariable/CLOCKS_PER_SEC << " seconds" << endl;
// Without remainder function
clockVariable = clock();
for(int i=1; i<100000; i++)
{
y = sqrt(100000*100000+i*i);
}
clockVariable = clock()- clockVariable;
cout << "Without using hypot operator, processing of same 100 integers for loop took " << (float)clockVariable/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}
Output:
While looking at both the examples, we see that using clock() in both the examples taught us that, even if we use functions in the processing of the loop, it might or might not be the most optimized method for processing. Sometimes a code with simple calculation might provide the optimal time of execution in order to fulfill a certain use case.
Conclusion
In this article, we have looked at how clock() function works in C++ and also 2 examples that show that not only having an in-built is convenient, sometimes it might lead to a decline in the optimum processing speed of code execution and hence it is always handy to do such processing speed checks during the code execution so that we know we have the most optimized one!
Recommended Articles
This is a guide to clock() C++. Here we discuss how the clock() function works in C++ along with the examples and outputs. You may also have a look at the following articles to learn more –