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?

Wok
  • 4,956
  • 7
  • 42
  • 64
John
  • 1,110
  • 3
  • 14
  • 28
  • Possible duplicate: http://stackoverflow.com/questions/307596/time-difference-in-c – Wok Sep 26 '10 at 12:16

2 Answers2

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;
}
Sadeq
  • 7,795
  • 4
  • 34
  • 45
  • wok: Rounding perhaps to help when casting to int, so `999` rounds down to one not zero. – John Sep 26 '10 at 12:24
  • @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 Sep 26 '10 at 12:27
  • @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`. – Sadeq Sep 26 '10 at 12:32
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

Dima
  • 1,253
  • 3
  • 21
  • 31
  • An interesting thought, Thank you. sorry I can't +1, not logged in. – John Sep 26 '10 at 12:53
  • 1
    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. – user433534 Oct 01 '10 at 13:44