Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use clock() in C++

How do I call clock() in C++?

For example, I want to test how much time a linear search takes to find a given element in an array.

like image 857
dato datuashvili Avatar asked Jul 10 '10 19:07

dato datuashvili


People also ask

How does the clock function work in C?

clock() function in C/C++ The clock() function returns the approximate processor time that is consumed by the program. The clock() time depends upon how the operating system allocate resources to the process that's why clock() time may be slower or faster than the actual clock. Syntax: clock_t clock( void );

Is clock () in C accurate?

It has nanosecond precision. Note the difference between precision and accuracy: precision is the unit that the values are reported.

What does clock return in C?

The C library function clock_t clock(void) returns the number of clock ticks elapsed since the program was launched.

What does clock_t mean in C?

clock_t is a typedef of long int and is defined in the time. h header. It is used to store the processor time in terms of the number of CPU cycles passed since the start of the process. To convert the number of CPU clock cycles into seconds, we need to use the CLOCKS_PER_SEC constant, which is also defined in the time.


1 Answers

#include <iostream> #include <cstdio> #include <ctime>  int main() {     std::clock_t start;     double duration;      start = std::clock();      /* Your algorithm here */      duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;      std::cout<<"printf: "<< duration <<'\n'; } 
like image 58
Dolph Avatar answered Oct 12 '22 13:10

Dolph