How to use clock() in C++

Viewed 422926

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.

7 Answers

You can measure how long your program works. The following functions help measure the CPU time since the start of the program:

  • C++ (double)clock() / CLOCKS_PER_SEC with ctime included.
  • Python time.clock() returns floating-point value in seconds.
  • Java System.nanoTime() returns long value in nanoseconds.

My reference: algorithms toolbox week 1 course part of data structures and algorithms specialization by University of California San Diego & National Research University Higher School of Economics

So you can add this line of code after your algorithm:

cout << (double)clock() / CLOCKS_PER_SEC;

Expected Output: the output representing the number of clock ticks per second

Related