views:

147

answers:

2

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?

+5  A: 

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;
}
PC2st
+1 Still, I do not understand the `+ 0.5`.
wok
wok: Rounding perhaps to help when casting to int, so `999` rounds down to one not zero.
John
@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.
John
@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`.
PC2st
@John: Your welcome :D
PC2st
Ok, thank you both!
wok
+2  A: 

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

Dima
An interesting thought, Thank you. sorry I can't +1, not logged in.
John
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.