4
#include <iostream>
#include <chrono>
using namespace std;

class MyTimer {
 private:
  std::chrono::time_point<std::chrono::steady_clock> starter;
  std::chrono::time_point<std::chrono::steady_clock> ender;

 public:
  void startCounter() {
    starter = std::chrono::steady_clock::now();
  }

  double getCounter() {
    ender = std::chrono::steady_clock::now();
    return double(std::chrono::duration_cast<std::chrono::nanoseconds>(ender - starter).count()) /
           1000000;  // millisecond output
  }
  
  // timer need to have nanosecond precision
  int64_t getCounterNs() {
    return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - starter).count();
  }
};

MyTimer timer1, timer2, timerMain;
volatile int64_t dummy = 0, res1 = 0, res2 = 0;

// time run without any time measure
void func0() {
    dummy++;
}

// we're trying to measure the cost of startCounter() and getCounterNs(), not "dummy++"
void func1() {
    timer1.startCounter();  
    dummy++;
    res1 += timer1.getCounterNs();
}

void func2() {
    // start your counter here
    dummy++;
    // res2 += end your counter here
}

int main()
{
    int i, ntest = 1000 * 1000 * 100;
    int64_t runtime0, runtime1, runtime2;

    timerMain.startCounter();
    for (i=1; i<=ntest; i++) func0();
    runtime0 = timerMain.getCounter();
    cout << "Time0 = " << runtime0 << "ms\n";

    timerMain.startCounter();
    for (i=1; i<=ntest; i++) func1();
    runtime1 = timerMain.getCounter();
    cout << "Time1 = " << runtime1 << "ms\n";

    timerMain.startCounter();
    for (i=1; i<=ntest; i++) func2();
    runtime2 = timerMain.getCounter();
    cout << "Time2 = " << runtime2 << "ms\n";

    return 0;
}

I'm trying to profile a program where certain critical parts have execution time measured in < 50 nanoseconds. I found that my timer class using std::chrono is too expensive (code with timing takes 40% more time than code without). How can I make a faster timer class?

I think some OS-specific system calls would be the fastest solution. The platform is Linux Ubuntu.

Edit: all code is compiled with -O3. It's ensured that each timer is only initialized once, so the measured cost is due to the startMeasure/stopMeasure functions only. I'm not doing any text printing.

Edit 2: the accepted answer doesn't include the method to actually convert number-of-cycles to nanoseconds. If someone can do that, it'd be very helpful.

17
  • clock_gettime of librt can be useful.
    – kiner_shah
    Dec 22, 2021 at 5:51
  • try __rdtsc if your compiler support intrinsic functions?
    – Nimrod
    Dec 22, 2021 at 5:57
  • How do you measure the untimed code? How do you measure the 40% difference? Do the 40% also include the setup and teardown of the timers themselves? Or the output? Dec 22, 2021 at 5:58
  • 2
    Why do you need measurements? An have you considered using a profiler? They are there so you don't have to add measurement code to your code yourself. They build callgraphs so you can figure out exactly where your bottlenecks are. Consider asking for the CPU how many cycles it as run (though you will still run into multithreading and other applications adding noise to your measurements) Dec 22, 2021 at 6:00
  • 1
    Making measurements is not free. Are you printing out results during measurement? If so remove those. Dec 22, 2021 at 6:02

1 Answer 1

9

What you want is called "micro-benchmarking". It can get very complex. I assume you are using Ubuntu Linux on x86_64. This is not valid form ARM, ARM64 or any other platforms.

std::chrono is implemented at libstdc++ (gcc) and libc++ (clang) on Linux as simply a thin wrapper around the GLIBC, the C library, which does all the heavy lifting. If you look at std::chrono::steady_clock::now() you will see calls to clock_gettime().

clock_gettime() is a VDSO, ie it is kernel code that runs in userspace. It should be very fast but it might be that from time to time it has to do some housekeeping and take a long time every n-th call. So I would not recommend for microbenchmarking.

Almost every platform has a cycle counter and x86 has the assembly instruction rdtsc. This instruction can be inserted in your code by crafting asm calls or by using the compiler-specific builtins __builtin_ia32_rdtsc() or __rdtsc().

These calls will return a 64-bit integer representing the number of clocks since the machine power up. rdtsc is not immediate but fast, it will take roughly 15-40 cycles to complete.

It is not guaranteed in all platforms that this counter will be the same for each core so beware when the process gets moved from core to core. In modern systems this should not be a problem though.

Another problem with rdtsc is that compilers will often reorder instructions if they find they don't have side effects and unfortunately rdtsc is one of them. So you have to use fake barriers around these counter reads if you see that the compiler is playing tricks on you - look at the generated assembly.

Also a big problem is cpu out of order execution itself. Not only the compiler can change the order of execution but the cpu can as well. Since the x86 486 the Intel CPUs are pipelined so several instructions can be executed at the same time - roughly speaking. So you might end up measuring spurious execution.

I recommend you to get familiar with the quantum-like problems of micro-benchmarking. It is not straightforward.

Notice that rdtsc() will return the number of cycles. You have to convert to nanoseconds using the timestamp counter frequency.

Here is one example:

#include <iostream>
#include <cstdio>

void dosomething() {
    // yada yada
}

int main() {
    double sum = 0;
    const uint32_t numloops = 100000000;
    for ( uint32_t j=0; j<numloops; ++j ) {
        uint64_t t0 = __builtin_ia32_rdtsc();
        dosomething();
        uint64_t t1 = __builtin_ia32_rdtsc();
        uint64_t elapsed = t1-t0;
        sum += elapsed;
    }
    std::cout << "Average:" << sum/numloops << std::endl;
}

This paper is a bit outdated (2010) but it is sufficiently up to date to give you a good introduction to micro-benchmarking:

How to Benchmark Code Execution Times on Intel® IA-32 and IA-64 Instruction Set Architectures

10
  • 1
    That information is not exposed by the kernel, at least that I know of. The right thing to do would be to take a tsc read before and after the loop, together with respective clock_gettime/chrono calls and calculate the average time per cycle. OR you can use a module like this: github.com/trailofbits/tsc_freq_khz
    – user8143588
    Dec 22, 2021 at 6:47
  • 1
    Thank you. It's 2.5x faster than using std::chrono, so I guess this is the answer
    – Huy Le
    Dec 22, 2021 at 6:48
  • 1
    If your TSC counter has a frequency of 3.2GHz, which is USUALLY the maximum frequency of your CPU, then there will be in average 3.2 cycles per nanosecond. keep that in mind.
    – user8143588
    Dec 22, 2021 at 6:52
  • 1
    I've occasionally used it for a microbenchmark when I know I'm running at max cpu speed. It is handy to report times in picoseconds which knowledge of the frequency enables. Though as you rightly point out in your answer, there's lots of pitfalls to look out for in microbenchmarking. Dec 22, 2021 at 19:28
  • 1
    @HowardHinnant so after years using this stuff I came to the conclusion that's better to leave it in cycles. Because sometimes you're running in a dev box at max 2.5GHz but the thing will run on an overclocked 5 GHz server. In cycles, the numbers typically even match my 2011 laptop running at 1.5 GHz. And all the Intel/AMD/Agner reports are all in cycles too so it's easier to correlate. With time, I just learned to memorize cycles.
    – user8143588
    Dec 22, 2021 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.