millisecond-accurate benchmarking in C++?
Asked Answered
C

2

7

I do not really wish to profile because I was wanting to do many different small benchmarks on different simple functions. For the life of me I cannot find a way to record the amount of milliseconds in C++, I am using Linux by the way.

Can you suggest the method to get the system clock in milliseconds (I may settle with seconds if I cannot find an easy way..) and what header they are included in?

Coster answered 26/9, 2010 at 12:8 Comment(1)
Possible duplicate: https://mcmap.net/q/413008/-time-difference-in-cTrace
M
20

using gettimeofday function from sys/time.h header file, i use this class:

#include <cstdlib>
#include <sys/time.h>

class Timer
{
    timeval timer[2];

  public:

    timeval start()
    {
        gettimeofday(&this->timer[0], NULL);
        return this->timer[0];
    }

    timeval stop()
    {
        gettimeofday(&this->timer[1], NULL);
        return this->timer[1];
    }

    int duration() const
    {
        int secs(this->timer[1].tv_sec - this->timer[0].tv_sec);
        int usecs(this->timer[1].tv_usec - this->timer[0].tv_usec);

        if(usecs < 0)
        {
            --secs;
            usecs += 1000000;
        }

        return static_cast<int>(secs * 1000 + usecs / 1000.0 + 0.5);
    }
};

for example:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    Timer tm;
    std::ostringstream ooo;
    std::string str;

    tm.start();
    for(int i = 0; i < 10000000; ++i)
    {
        ooo << "This is a string. ";
    }
    tm.stop();
    std::cout << "std::ostingstream -> " << tm.duration() << std::endl;

    tm.start();
    for(int i = 0; i < 10000000; ++i)
    {
        str += "This is a string. ";
    }
    tm.stop();
    std::cout << "std::string -> " << tm.duration() << std::endl;
}
Mceachern answered 26/9, 2010 at 12:12 Comment(3)
wok: Rounding perhaps to help when casting to int, so 999 rounds down to one not zero.Coster
@PC2st: That was the first time I fully "got" what the code does, this will help me alot, I used to be doing my benchmarking in PHP, but man it is inaccurate to measure algorithms. Many thanks.Coster
@wok: John says the reason, + 0.5 is used for rounding the result, but if the sign of the result was negative, then we have to use - 0.5 instead of + 0.5.Mceachern
A
7

If you are using x86 CPU, you can use rdtsc assembler instructions http://en.wikipedia.org/wiki/Rdtsc in order to get number of CPU clocks between execution of two (or more) commands. But: 1. All rdtsc commands should run on same CPU core (in case if you have multi core CPU). 2. CPU should run in constant clock frequency (CPU power management should be disabled).

Dima

Aaren answered 26/9, 2010 at 12:30 Comment(2)
An interesting thought, Thank you. sorry I can't +1, not logged in.Coster
Some PC's actually work across cores, others drift by hundreds of milliseconds. For buggy firmsware, at startup spin two core-bound threads to get { core1-clocks, core2-clocks, core1-clocks... }, each thread writes its value as soon as it sees the other thread has done the same. On my PC, narrowed core-to-core delta down to ~400 ~3GHz cycles. Then to get each timestamp, CPUID(1)/RDTSC/CPUID(2), spinning if the two CPUID instructions report different cores.Cortez

© 2022 - 2024 — McMap. All rights reserved.