gprof输出困惑 - 电话太多了?

时间:2014-03-25 23:01:10

标签: c++ gprof

我刚刚开始使用gprof来优化我的慢速代码。我对一个输出感到困惑,我希望你能帮助我。

这是:

                0.01    0.46     500/500         System::Update() [2]
[3]     96.2    0.01    0.46     500         Verlet::Advance() [3]
                0.02    0.19   61000/61122       CalculateAcceleration(std::vector<Particle, std::allocator<Particle> > const&, int) [4]
                0.00    0.06  183000/244127      Vector3D::Vector3D() [8]
                0.00    0.06  305000/676956      Vector3D::Vector3D(Vector3D const&) [6]
                0.00    0.03  122000/122000      Particle::SetPosition(Vector3D const&) [18]
                0.00    0.03  122000/122000      Particle::SetVelocity(Vector3D const&) [19]
                0.02    0.01  183000/183000      Vector3D::LinearCombine(double, Vector3D, double) [23]
                0.00    0.03  549000/921083      Vector3D::~Vector3D() [14]
                0.00    0.00  122000/364484      Vector3D::AddToVector(Vector3D) [30]
                0.00    0.00   61000/182242      std::pow(double, int) [44]
                0.00    0.00   61000/303484      Vector3D::ScalarMultVector(double) [51]
                0.00    0.00   61500/7579826     std::vector<Particle, std::allocator<Particle> >::size() const [25]
                0.00    0.00  366000/366122      std::vector<Particle, std::allocator<Particle> >::operator[](unsigned int) [127]
                0.00    0.00  122000/365606      Particle::GetPosition() const [128]

我正在运行该函数500次,而for循环的大小为122,因此61,000对应于每次运行一次执行,122,000对2,183,000到3,244,000到4和305,000到5.这是循环:

 
    void Advance() {

        for(int i = 0; i < (int)Particles.size(); i++) {

            const Vector3D& CurrentR_NMinus1 = Particles[i].GetPosition();
            const Vector3D& CurrentR_N = Particles1[i].GetPosition();
            const Vector3D& CurrentA_N = CalculateAcceleration(Particles1,i);

            // Calculate R_N+1

            Vector3D CurrentR_NPlus1;
            CurrentR_NPlus1.AddToVector(CurrentR_N);
            CurrentR_NPlus1.LinearCombine(2, CurrentR_NMinus1, -1);
            CurrentR_NPlus1.LinearCombine(1, CurrentA_N, pow(StepSize,2));

            // Calculate V_N

            Vector3D CurrentV_N;
            CurrentV_N.AddToVector(CurrentR_NPlus1);
            CurrentV_N.LinearCombine(1,CurrentR_NMinus1,-1);
            CurrentV_N.ScalarMultVector(1/(2*StepSize));

            // Update

            Particles[i].SetPosition(CurrentR_N);
            Particles[i].SetVelocity(CurrentV_N);
            t0 += StepSize;

            Particles1[i].SetPosition(CurrentR_NPlus1);
            Particles1[i].SetVelocity(Vector3D());
            t1 += StepSize;

        }

    }

所有条目对我都有意义,除了一个:Vector3D(Vector3D const&amp;)显然被调用了5次。但它在代码中没有任何地方!它的孩子肯定称之为,但我认为他们在gprof列表中有自己的条目。我思考的缺陷在哪里?它是否与Particles []成为Vector3Ds的载体有关?

很抱歉,如果这是一个显而易见的问题 - 我刚刚开始使用gprof,而且我也是C ++的新手。我还没能在网上找到答案,所以我希望你能帮助我。

非常感谢你的时间!

1 个答案:

答案 0 :(得分:3)

对于类型为Vector3D的对象,这是copy constructor。您可以隐式调用复制构造函数的一种情况是,您将现有实例按值作为参数传递给函数。我的猜测是在AddToVectorSetPosition以及SetVelocity的调用中发生的,正如我在下面的评论中指出的那样,总共有5个电话。你可能很好奇为什么在你调用SetVelocity(Vector3D())时没有调用copy constructor,如果你想知道,最好通过阅读这里的解释来解释:

Correct usage of rvalue references as parameters

const Vector3D& CurrentR_NMinus1 = Particles[i].GetPosition();
const Vector3D& CurrentR_N = Particles1[i].GetPosition();
const Vector3D& CurrentA_N = CalculateAcceleration(Particles1,i);

// Calculate R_N+1

Vector3D CurrentR_NPlus1;
CurrentR_NPlus1.AddToVector(CurrentR_N);          // copy constructor called
CurrentR_NPlus1.LinearCombine(2, CurrentR_NMinus1, -1);
CurrentR_NPlus1.LinearCombine(1, CurrentA_N, pow(StepSize,2));

// Calculate V_N

Vector3D CurrentV_N;
CurrentV_N.AddToVector(CurrentR_NPlus1);          // copy constructor called
CurrentV_N.LinearCombine(1,CurrentR_NMinus1,-1);  
CurrentV_N.ScalarMultVector(1/(2*StepSize));

// Update

Particles[i].SetPosition(CurrentR_N);             // copy constructor called
Particles[i].SetVelocity(CurrentV_N);             // copy constructor called
t0 += StepSize;

Particles1[i].SetPosition(CurrentR_NPlus1);       // copy constructor called
Particles1[i].SetVelocity(Vector3D());
t1 += StepSize;