Boost.ChronoでTrace

library request - gmame.comp.lib.boost.devel


こんなことできるトレースライブラリがほしいなー、というリクエストが出ていたのですが

#include <tracer.h>

void foo()
{
    TRACE_LIFETIME;
    usleep(rand() % 100000);
}

int main()
{
    TRACE_LIFETIME;
    for(int i = 0; i < 100; ++i)
        foo();
}
All the program time: 05265686 us (micro secs)

TRACE POINT:
 Function                : int main()
 Calls                   : 0000000001 times
 Average T between calls : 0000000000 us
 Average life T          : 0005264459 us
 File                    : main.cpp
 Line                    : 00000009

TRACE POINT:
 Function                : void foo()
 Calls                   : 0000000100 times
 Average T between calls : 0000051665 us
 Average life T          : 0000052606 us
 File: main.cpp
 Line                    : 00000004


すかさず「それBoost.Chronoでできるよ」というリプライがありましたとさ。
「Boost.Chrono version 0.3の新機能StopWatchでできるよ
http://old.nabble.com/-chrono--v0.3--New-Stopwatch-Feature-to27202338.html

#include <boost/stopwatches.h>
#include <cmath>

#define TRACE_LIFETIME \
  static boost::chrono::stopclock_accumulator<> acc(BOOST_CHRONO_ACCUMULATOR_FUNCTION_FORMAT); \
  boost::chrono::stopclock_accumulator<>::scoped_run _(acc);

int f1(long j)
{
    TRACE_LIFETIME;

    for ( long i = 0; i < j; ++i )
        std::sqrt( 123.456L ); // burn some time

    return 0;
}

int main()
{
    TRACE_LIFETIME;

    f1(100000);
    f1(200000);
    f1(300000);

    return 0;
}
int f1(long int) : 3 times, sum=0.031s, min=0.006s, max=0.013s, mean=0.010s
int main() : 1 times, sum=0.031s, min=0.031s, max=0.031s, mean=0.031s