2014-03-25 5 views
1

저는 느린 코드를 최적화하기 위해 gprof를 사용하기 시작했습니다. 나는 하나의 산출물에 혼란스럽고, 당신이 나를 도울 수 있기를 바랍니다. 여기gprof 출력에 혼란 - 너무 많은 전화?

그것이 : I 함수 500 회 실행하고

   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] 

및 루프에 대한 너무 61,000 런 하나씩 실행, 122,000 (2)에 대응하고, 사이즈 (122)이며, 183,000 내지 3, 244,000

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; 

     } 

    } 

모든 항목을 제외한, 나에게 의미가 : 여기에 5에 4, 305,000에 루프 값 Vector3D (된 Vector3D CONST &는) 분명히 5 번이라고합니다. 하지만 코드에 아무 것도 없습니다! 그것의 아이들은 확실히 그것을 부른다. 그러나 나는 그들이 gprof 목록에 그들 자신의 항목을 가지고 있다고 생각했다. 내 생각에 결함이 어디 있니? Particles []가 Vector3D의 벡터가되는 것과 관련이 있습니까?

죄송합니다. 이것이 분명한 질문 인 경우 - gprof를 사용하기 시작했으며 C++의 초심자이기도합니다. 그러나 나는 온라인에서 답을 찾을 수 없었으므로 나를 도울 수 있기를 바랍니다.

감사합니다.

답변

3

이는 Vector3D 유형의 객체에 대해 copy constructor입니다. 복사 생성자를 암시 적으로 호출하는 경우는 기존 인스턴스를 값으로 인수를 함수에 전달할 때입니다. 내 생각 엔 AddToVectorSetPositionSetVelocity에 대한 통화에서 이러한 현상이 발생합니다. 아래 설명에 표시된대로 총 5 건입니다.

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; 
+1

당신은 : 여기에 설명을 읽고, 알고 치료하면 copy constructor는 당신이 SetVelocity를 호출 할 때 호출되지 않는 이유 당신은 궁금 수 있습니다 (값 Vector3D는()), 즉, 설명 최고 확실히 맞아. 이 함수들을 "const Vector3D & const"로 만드는 것을 잊어 버렸습니다. 자바에서 오는 것에 익숙해 져야합니다. 실수로 얼룩을지게하고 너무 빨리 답변 해 주셔서 감사합니다. – noctilux