Motti Motti -4 years ago 43
C++ Question

Quick and dirty way to profile your code

What method do you use when you want to get performance data about specific code paths?

Answer Source

This method has several limitations, but I still find it very useful. I'll list the limitations (I know of) up front and let whoever wants to use it do so at their own risk.

  1. The original version I posted over-reported time spent in recursive calls (as pointed out in the comments to the answer).
  2. It's not thread safe, it wasn't thread safe before I added the code to ignore recursion and it's even less thread safe now.
  3. Although it's very efficient if it's called many times (millions), it will have a measurable effect on the outcome so that scopes you measure will take longer than those you don't.

I use this class when the problem at hand doesn't justify profiling all my code or I get some data from a profiler that I want to verify. Basically it sums up the time you spent in a specific block and at the end of the program outputs it to the debug stream (viewable with DbgView), including how many times the code was executed (and the average time spent of course)).

#pragma once
#include <tchar.h>
#include <windows.h>
#include <sstream>
#include <boost/noncopyable.hpp>

namespace scope_timer {
    class time_collector : boost::noncopyable {
        __int64 total;
        LARGE_INTEGER start;
        size_t times;
        const TCHAR* name;

        double cpu_frequency()
        { // cache the CPU frequency, which doesn't change.
            static double ret = 0; // store as double so devision later on is floating point and not truncating
            if (ret == 0) {
                LARGE_INTEGER freq;
                ret = static_cast<double>(freq.QuadPart);
            return ret;
        bool in_use;

        time_collector(const TCHAR* n)
            : times(0)
            , name(n)
            , total(0)
            , start(LARGE_INTEGER())
            , in_use(false)

            std::basic_ostringstream<TCHAR> msg;
            msg << _T("scope_timer> ") <<  name << _T(" called: ");

            double seconds = total / cpu_frequency();
            double average = seconds / times;

            msg << times << _T(" times total time: ") << seconds << _T(" seconds  ")
                << _T(" (avg ") << average <<_T(")\n");

        void add_time(__int64 ticks)
            total += ticks;
            in_use = false;

        bool aquire()
            if (in_use)
                return false;
            in_use = true;
            return true;

    class one_time : boost::noncopyable {
        LARGE_INTEGER start;
        time_collector* collector;
        one_time(time_collector& tc)
            if (tc.aquire()) {
                collector = &tc;
                collector = 0;

            if (collector) {
                LARGE_INTEGER end;
                collector->add_time(end.QuadPart - start.QuadPart);

// Usage TIME_THIS_SCOPE(XX); where XX is a C variable name (can begin with a number)
#define TIME_THIS_SCOPE(name) \
    static scope_timer::time_collector st_time_collector_##name(_T(#name)); \
    scope_timer::one_time st_one_time_##name(st_time_collector_##name)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download