I was executing a C code which was a loop of 65535. It was basically , a program which was testing the outputs for all possible values and hence can say, testing the algorithm.
I used the gcc compiler to compile the c code. When i executed the code it took a good 5-6 secs to complete the execution. But now we all know the command prompt window could display only a few hundred recent lines . So i decided to print the output to a text document using the
a.exe > sample.txt
command. To my astonishment , the code was executed in less than a sec. All this could only inspire me to check out ' how could i know the clock cycles required to execute the program.
it took some google searches to know that clock() is the function i was looking for which is in the header file time.h .
here is the glimpse of the program.
#include
main()
{
long int i;
clock_t clk;
// write your code here
for (i=0;i<10000;i++)
{
printf("%ld",i);
}
//say your code ends here
clk = clock(); // clock fuction give the clock cycles elapsed since the start of the program
printf("the no of clock cycles required to execute the is %ld",clk );
}
clock_t is long integer type of data type defined in sys/types.h
the function clock() returns a value of type clock_t so we need to declare a variable of this type .
P.S. : the clock cycles taken by the program would differ based on the other processes running on your system. one should keep in mind that , it does not give the time taken by your code to execute if it is the only process running. But it gives the time taken in the current complexity of the processing environment.