How to use clock() in C++

dato datuashvili picture dato datuashvili · Jul 10, 2010 · Viewed 394.7k times · Source

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.

Answer

Dolph picture Dolph · Jul 10, 2010
#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';
}