Boost logo

Boost :

From: Josh Napoli (jnapoli_at_[hidden])
Date: 2006-01-06 10:03:06

If you need more accuracy for modern x86 CPUs, it is relatively easy to
make an implementation of timer that uses the performance counter.

Here is how to read the timer and get the period in Windows:

          DWORD BufSize = _MAX_PATH;
          DWORD dwMHz = _MAX_PATH;
          HKEY hKey;

          // open the key where the proc speed is hidden:
          //long lError =

          // query the key:
          RegQueryValueEx(hKey, LPCTSTR("~MHz"), NULL, NULL, (LPBYTE)
&dwMHz, &BufSize);

          value_ = 1./(dwMHz*1000000);

      static ULARGE_INTEGER readTimer()
#ifdef _WIN64
        return __rdtsc();
        ULARGE_INTEGER retval;
        unsigned int LowPart;
        unsigned int HighPart;
          xor eax, eax
            xor ebx, ebx
            xor ecx, ecx
            xor edx, edx
            _emit 0x0f // RDTSC
            _emit 0x31
            mov [LowPart], eax // TICK COUNTER STARTS HERE
            mov [HighPart], edx // TICK COUNTER STARTS HERE
        retval.LowPart = LowPart;
        retval.HighPart = HighPart;
        return retval;

-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of Howard Hinnant
Sent: Tuesday, January 03, 2006 10:19 PM
To: boost_at_[hidden]
Subject: Re: [boost] Performance test and memory usage

On Jan 3, 2006, at 5:12 PM, axter wrote:

> What boost libraries are available for running code performance test

There is timer:

which is very handy, but in my experience not a complete solution for
performance testing on modern machines because of the variability of
the results. I've had good experience with the following timer driver:

template <class F>
test(F f)
     std::vector<float> t;
     int i;
     for (i = 0; i < 10; ++i)
     double total_time = std::accumulate(t.begin(), t.end(), 0.0);
     while (i < 1000 && total_time < 1.0)
         total_time += t.back();
     std::sort(t.begin(), t.end());
     t.resize(t.size() * 9 / 10);
     return (float)(std::accumulate(t.begin(), t.end(), 0.0) / t.size

Which can be used like:

     // whatever set up needs to happen
     clock_t t = clock(); // or boost::timer, or whatever
     // time this
     } // scope to time any destructors as appropriate
     t = clock() - t; // or whatever
     // whatever tear down needs to happen
     return (float)(t/(double)CLOCKS_PER_SEC); return seconds (or
whatever) as a float

int main()
     cout << test(time_something) << '\n';

The basic idea of "test" is to call the function to be timed many
times (say up to 1000), throw away the slowest 10% of those times,
and average the rest. Why? <shrug> shoot-from-the-hip "theory" and
experience that its return is fairly consistent to 2, maybe 3
digits. The test function has a "time out" feature where it quits if
the accumulated measured time grows beyond 1 second (which may not be
appropriate for all tests). But it is easy to get bored when you
unexpectedly find yourself waiting 30 minutes for timing result, so
that's why it's there. Otoh, I put in a minimum repetition of at
least 10, no matter how long the measured time is, so you get at
least some accuracy (tweak knobs as you like). Note that the
accumulation/averaging happens in double, even though the data and
answer are float (just paranoia really). Weakness: If you're timing
something that is quicker than the minimum resolution of your timer,
this doesn't work. But otherwise, this is better than the
traditional loop inside the timer as it throws away those results
that happen to get interrupted by your email checker running. :-)
Anyway, if it is useful to you, use it. If not, no sweat.

> and
> memory usage (if any)?

If you limit it to new/delete (neglecting malloc), the following
would be easy to adapt:

(some day I'm going to have to start using tinyurl (or whatever it is)).

The above is just an overloaded new using hash_map<void*, mem_info,
malloc_allocator<appropriate pair> >. (map could just as easily be

You could collect statistics from the container, and from mem_info.
The malloc_alloc is necessary so that the container itself doesn't
pollute your measurement.


Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at