我想以一种非标准的方式描述我的执行情况。使用gprof,Valgrind,Oprofile。。。对于给定的函数,我只得到其执行时间的平均值。我想得到这个执行时间的标准差。
例子:

void a()
  sleep ( rand() % 10 + 10 )

void b()
  sleep ( rand() % 14 + 2 )

main
  for (1 .. 100)
    a()
    b()

使用标准工具,a和b函数将具有类似的行为。你知道有什么工具能给我这个结果吗,用自动的方法。
我已经用TAU测试过了,但直到现在,它还没有真正的相关性。我认为有这样一个解决办法,但我对陶没有足够的信心。如果有人是Tau专家,我会尽量保留所有的函数执行时间,并在最后进行计算。但我不知道怎么用Tau来说明。
我想剖析C/C++代码,但是如果你有其他编程语言的领头羊,我是开放的。

最佳答案

一个分析工具不是魔术,你可以在几行中为任何目的滚动自己的工具。
可能是这样的:

// code profile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class cProfile
{
public:
    // construct profiler for a particular scope
    // call at begining of scope to be timed
    // pass unique name of scope
    cProfile( const char* name )
    {
        myName = string( name );
        QueryPerformanceCounter( (LARGE_INTEGER *)&myTimeStart );
    }
    // destructor - automatically called when scope ends
    ~cProfile();

    // constructor - produces report when called without parameters
    cProfile();


private:
    typedef accumulator_set<__int64, stats<tag::variance(lazy)> > acc_t;
    static map < string, acc_t > myMap;
    string myName;
    __int64 myTimeStart;
};
map < string, accumulator_set<__int64, stats<tag::variance(lazy)> > > cProfile::myMap;


cProfile::~cProfile()
{
    __int64 t=0;
    QueryPerformanceCounter( (LARGE_INTEGER *)&t );
    t -= myTimeStart;


    map < string, acc_t >::iterator p = myMap.find( myName );
    if( p == myMap.end() ) {
        // this is the first time this scope has run
        acc_t acc;
        pair<string,acc_t > pr(myName,acc);
        p = myMap.insert( pr ).first;
    }
    // add the time of running to the accumulator for this scope
    (p->second)( t );

}
// Generate profile report
cProfile::cProfile()
{
    __int64 f;
    QueryPerformanceFrequency( (LARGE_INTEGER *)&f );

    printf("%20s Calls\tMean (secs)\tStdDev\n","Scope");
    for( map < string, accumulator_set<__int64, stats<tag::variance(lazy)> > >::iterator p = myMap.begin();
        p != myMap.end(); p++ )
    {
        float av = mean(p->second) / f;
        float stdev = sqrt( ((double) variance(p->second))  ) / f;
        printf("%20s %d\t%f\t%f\n",p->first.c_str(),
            boost::accumulators::count(p->second), av, stdev);
    }
}
void a()
{
    cProfile profile("a");

    Sleep ( rand() % 10 + 10 );
}
void b()
{
    cProfile profile("b");

    Sleep ( rand() % 20 + 5 );
}


int _tmain(int argc, _TCHAR* argv[])
{
    for (int k=1;k<=100;k++) {
        a();
        b();
    }

    cProfile profile_report;

    return 0;
}

产生
       Scope Calls      Mean (secs)     StdDev
           a 100        0.014928        0.002827
           b 100        0.015254        0.005671

10-02 05:42
查看更多