Timing algorithm: clock() vs time() in C++

后端 未结 5 827
失恋的感觉
失恋的感觉 2020-12-22 23:08

For timing an algorithm (approximately in ms), which of these two approaches is better:

clock_t start = clock();
algorithm();
clock_t end = clock();
double t         


        
相关标签:
5条回答
  • 2020-12-22 23:24

    <chrono> is the best. Visual Studio 2013 provides this feature. Personally, I have tried all the methods mentioned above. I strongly recommend you use the <chrono> library. It can track the wall time and at the same time have a good resolution (much less than a second).

    0 讨论(0)
  • 2020-12-22 23:27

    How about gettimeofday()? When it is called it updates two structs (timeval and timezone), with timing information. Usually, passing a timeval struct is enough and the timezone struct can be set to NULL. The updated timeval struct will have two members tv_sec and tv_usec. tv_sec is the number of seconds since 00:00:00, January 1, 1970 (Unix Epoch) and tv_usec is additional number of microseconds w.r.t. tv_sec. Thus, one can get time expressed in very good resolution.

    It can be used as follows:

    #include <time.h>
    
    struct timeval start_time;
    double mtime, seconds, useconds;
    gettimeofday(&start_time, NULL); //timeval is usually enough
    int seconds  = start_time.tv_sec; //time in seconds
    int useconds = start_time.tv_usec; //further time in microseconds
    int desired_time = seconds * 1000000 + useconds; //time in microseconds
    
    0 讨论(0)
  • 2020-12-22 23:28

    It depends what you want: time measures the real time while clock measures the processing time taken by the current process. If your process sleeps for any appreciable amount of time, or the system is busy with other processes, the two will be very different.

    http://en.cppreference.com/w/cpp/chrono/c/clock

    0 讨论(0)
  • 2020-12-22 23:28

    The time_t structure is probably going to be an integer, which means it will have a resolution of second.

    The first piece of code: It will only count the time that the CPU was doing something, so when you do sleep(), it will not count anything. It can be bypassed by counting the time you sleep(), but it will probably start to drift after a while.

    The second piece: Only resolution of seconds, not so great if you need sub-second time readings.

    For time readings with the best resolution you can get, you should do something like this:

    double getUnixTime(void)
    {
        struct timespec tv;
    
        if(clock_gettime(CLOCK_REALTIME, &tv) != 0) return 0;
    
        return (tv.tv_sec + (tv.tv_nsec / 1000000000.0));
    }
    
    double start_time = getUnixTime();
    double stop_time, difference;
    
    doYourStuff();
    
    stop_time = getUnixTime();
    difference = stop_time - start_time;
    

    On most systems it's resolution will be down to few microseconds, but it can vary with different CPUs, and probably even major kernel versions.

    0 讨论(0)
  • 2020-12-22 23:41

    <chrono> would be a better library if you're using C++11.

    #include <iostream>
    #include <chrono>
    #include <thread>
    
    void f()
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    
    int main()
    {
        auto t1 = std::chrono::high_resolution_clock::now();
        f();
        auto t2 = std::chrono::high_resolution_clock::now();
        std::cout << "f() took "
                  << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
                  << " milliseconds\n";
    }
    

    Example taken from here.

    0 讨论(0)
提交回复
热议问题