140

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.

1
  • 1
    Note that wall-clock time isn't always a good way to time microbenchmarks. To get consistent results, you have to work around CPU frequency-scaling (including Intel turbo or the AMD equivalent, which lets your CPU clock higher when thermal/power limits allow). Profiling with performance counters can give you measurements in core clock cycles (and also details about whether a bottleneck is cache misses vs. instruction throughput vs. latency, by looking at counters other than just cycles). On Linux, perf stat -d ./a.out Sep 21, 2017 at 7:59

7 Answers 7

221
#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';
}
8
  • 5
    From what I can see here cplusplus.com/reference/ctime/clock, you don't need use the "std::" notation. Just use "clock()"
    – birgersp
    Jan 28, 2016 at 18:23
  • 4
    @Birger: In all the project I worked yet the code style requires std:: before every std:: call. Jan 23, 2017 at 8:31
  • 3
    Does this return the answer in seconds? May 18, 2017 at 12:54
  • 1
    @ArnavBorborah Yes, it does.
    – JoeVictor
    May 31, 2017 at 21:38
  • 1
    @Th.Thielemann both clock() and clock_t are from the C Standard Library's header of time.h, and therefore do not need the use of std namespace prefixes after the inclusion of their libraries. <ctime> wraps that value and function with the std namespace, but it's not required to use. Check here for implementation details: en.cppreference.com/w/cpp/header/ctime Jun 19, 2018 at 18:53
78

An alternative solution, which is portable and with higher precision, available since C++11, is to use std::chrono.

Here is an example:

#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;

int main()
{
    auto t1 = Clock::now();
    auto t2 = Clock::now();
    std::cout << "Delta t2-t1: " 
              << std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count()
              << " nanoseconds" << std::endl;
}

Running this on ideone.com gave me:

Delta t2-t1: 282 nanoseconds
2
  • 11
    If you are suggesting to use C++11, you could just as well write using Clock=std::chrono::high_resolution_clock;. See type alias.
    – JHBonarius
    Sep 21, 2017 at 8:04
  • 1
    std::chrono::high_resolution_clock is not monotonic across all std lib implementations. From the cppreference - Generally one should just use std::chrono::steady_clock or std::chrono::system_clock directly instead of std::chrono::high_resolution_clock: use steady_clock for duration measurements, and system_clock for wall-clock time. Apr 9, 2020 at 8:59
33

clock() returns the number of clock ticks since your program started. There is a related constant, CLOCKS_PER_SEC, which tells you how many clock ticks occur in one second. Thus, you can test any operation like this:

clock_t startTime = clock();
doSomeOperation();
clock_t endTime = clock();
clock_t clockTicksTaken = endTime - startTime;
double timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;
3
  • 6
    timeInSeconds is always coming 0.000000for me. How would I fix it ?
    – noufal
    Oct 10, 2013 at 7:32
  • 3
    @noufal Maybe the time spent is so short it shows up as 0. You could try using a long double to get more precision.
    – Gerard
    May 12, 2014 at 13:51
  • probably your clock resolution isn't high enough, so no time elapsed. Apr 4, 2019 at 14:59
4

On Windows at least, the only practically accurate measurement mechanism is QueryPerformanceCounter (QPC). std::chrono is implemented using it (since VS2015, if you use that), but it is not accurate to the same degree as using QueryPerformanceCounter directly. In particular it's claim to report at 1 nanosecond granularity is absolutely not correct. So, if you're measuring something that takes a very short amount of time (and your case might just be such a case), then you should use QPC, or the equivalent for your OS. I came up against this when measuring cache latencies, and I jotted down some notes that you might find useful, here; https://github.com/jarlostensen/notesandcomments/blob/master/stdchronovsqcp.md

0
#include <iostream>
#include <ctime>
#include <cstdlib> //_sleep()  --- just a function that waits a certain amount of milliseconds

using namespace std;

int main()
{

    clock_t cl;     //initializing a clock type

    cl = clock();   //starting time of clock

    _sleep(5167);   //insert code here

    cl = clock() - cl;  //end point of clock

    _sleep(1000);   //testing to see if it actually stops at the end point

    cout << cl/(double)CLOCKS_PER_SEC << endl;  //prints the determined ticks per second (seconds passed)


    return 0;
}

//outputs "5.17"
1
  • 1
    This does not add to the already answered question. Sleep after cl = clock() - cl is not needed. And the cout prints seconds not ticks per second. cl stores the clock ticks. Nov 9, 2017 at 17:53
0

Probably you might be interested in timer like this : H : M : S . Msec.

the code in Linux OS:

#include <iostream>
#include <unistd.h>

using namespace std;
void newline(); 

int main() {

int msec = 0;
int sec = 0;
int min = 0;
int hr = 0;


//cout << "Press any key to start:";
//char start = _gtech();

for (;;)
{
        newline();
                if(msec == 1000)
                {
                        ++sec;
                        msec = 0;
                }
                if(sec == 60)
                {
                        ++min;
                        sec = 0; 
                }
                if(min == 60)
                {
                        ++hr;
                        min = 0;
                }
        cout << hr << " : " << min << " : " << sec << " . " << msec << endl;
        ++msec;
        usleep(100000); 

}

    return 0;
}

void newline()
{
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
2
  • You may want to check the first condition ... 10 msec = 1 sec? Nov 9, 2017 at 17:55
  • 2
    This will accumulate relative error in the time because you don't include the time it takes to print, and usleep won't always return after exactly the amount you ask for. Sometimes it will be longer. You should check the current time at the start, then check the current time and subtract to get the absolute time since you started every time through the loop. Dec 29, 2017 at 19:41
0

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

1
  • 1
    The question is ask only for c++. So it is nice you reference to other programming languages/scripts, but it is out of topic.
    – dboy
    May 29, 2020 at 13:14

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.