0

C++ 初心者からの質問で、早朝に頭痛がします。ご覧になりたい場合は、ページの下部にあるコードにスキップしてください。識別子は異なるが同じタイプ (つまり ) の複数の変数にいくつかの操作を適用していますdouble。操作は、外部関数呼び出しから、またはメイン内で行われます。

6つのシナリオを考える

(1) ローカルオブジェクトが関数を呼び出していない

(2) 関数を呼び出していない参照オブジェクト

(3) 関数を呼び出していないベクトル内の要素への参照

(4) ローカルオブジェクト呼び出し関数

(5) 関数を呼び出していない参照オブジェクト

(6) ベクトル呼び出し関数内の要素参照

興味深い結果が得られました(とにかく私にとって)。(1) と (2) は平均 574 ミリ秒かかりましたが、(3)、(4)、(5)、(6) はすべて約 2.77 秒かかりました。

(4)、(5)、および (6) は、おそらく要素を渡すことによる関数呼び出しから生じるオーバーヘッドによるものであることを認めます。いくつかの疑問が生じます。

  • ベクトル要素への参照の計算 (つまり (3)) も、関数の呼び出しと同じ時間がかかるのはなぜですか? これは、ベクトル要素への参照の呼び出しと、同様の関数への値の提供との間に何らかのオーバーヘッドがあることを意味しますか? double&(この場合の関数は ではなくを取ることに注意してくださいdouble)。

  • 関数パラメーターをすべて に変更すると&double、(1) と (2) に 2.7 秒かかるのはなぜですか? つまり、(1) と (2) に対して関数を呼び出しているわけではありません。(他の誰かがこれを試すことができますか - 私はこれが奇妙だと思ったので)

  • これらのいずれかを最適化する特別な方法はありますか?

CODE: Windows MinGWg++ 4.7.2でコンパイル済み。g++ -std=c++11 -O3

#include <iostream> // c++ input/output libraries
#include <stdio.h>  
#include <vector> 

#include "timer.h"  

void do_some_calc(double aa, double bb, double cc, double dd, double ee)
{
double total{0}, add{0};
for(int tests=0; tests<5; ++tests) {
    Timer Time;
    Time.start();
    for(int i=0; i<100000; ++i)
    {
        for(int j=0; j<2000; ++j)
        {
            add = aa*bb/cc*dd/ee;
            total += add;
            aa=aa/2;
            bb=bb/2;
            cc=cc/2;
            dd=dd/2;
            ee=ee/2;
            aa=aa*2;
            bb=bb*2;
            cc=cc*2;
            dd=dd*2;
            ee=ee*2;

        }
    }
    cout << total << " with " << add << endl;
    Time.finish("func call");
}
}

int main()
{

// the numbers 12, 13,14,13 and 12 tied to a vector
std::vector<double> ch{12,13,14,13,12};

// the numbers 12, 13,14,13 and 12 tied to independent objects
double a = 12;
double b = 13;
double c = 14;
double d = 13;
double e = 12;

    // reference to objects
double& a_ref = a;
double& b_ref = b;
double& c_ref = c;
double& d_ref = d;
double& e_ref = e;

    // reference to vector elements
double& a_vref = ch[0];
double& b_vref = ch[1];
double& c_vref = ch[2];
double& d_vref = ch[3];
double& e_vref = ch[4];



cout << "1) normal without function (i.e. local):" << endl;
double total{0}, add{0};
for(int tests=0; tests<5; ++tests) {
    Timer Time;
    Time.start();
    for(int i=0; i<100000; ++i)
    {
        for(int j=0; j<2000; ++j)
        {
            add = a*b/c*d/e;
            total += add;
            a=a/2;
            b=b/2;
            c=c/2;
            d=d/2;
            e=e/2;
            a=a*2;
            b=b*2;
            c=c*2;
            d=d*2;
            e=e*2;

        }
    }
    cout << total << " with " << add << endl;
    Time.finish("obj");
}

cout << "\n\n2) reference to double obj without function (i.e. local):" << endl;
total=0, add=0;
for(int tests=0; tests<5; ++tests) {
    Timer Time;
    Time.start();
    for(int i=0; i<100000; ++i)
    {
        for(int j=0; j<2000; ++j)
        {
            add = a_ref*b_ref/c_ref*d_ref/e_ref;
            total += add;
            a_ref=a_ref/2;
            b_ref=b_ref/2;
            c_ref=c_ref/2;
            d_ref=d_ref/2;
            e_ref=e_ref/2;
            a_ref=a_ref*2;
            b_ref=b_ref*2;
            c_ref=c_ref*2;
            d_ref=d_ref*2;
            e_ref=e_ref*2;

        }
    }
    cout << total << " with " << add << endl;
    Time.finish("ref obj");
}

cout << "\n\n3) reference to double obj from vector without function (i.e. local):" << endl;
total=0, add=0;
for(int tests=0; tests<5; ++tests) {
    Timer Time;
    Time.start();
    for(int i=0; i<100000; ++i)
    {
        for(int j=0; j<2000; ++j)
        {
            add = a_vref*b_vref/c_vref*d_vref/e_vref;
            total += add;
            a_vref=a_vref/2;
            b_vref=b_vref/2;
            c_vref=c_vref/2;
            d_vref=d_vref/2;
            e_vref=e_vref/2;
            a_vref=a_vref*2;
            b_vref=b_vref*2;
            c_vref=c_vref*2;
            d_vref=d_vref*2;
            e_vref=e_vref*2;

        }
    }
    cout << total << " with " << add << endl;
    Time.finish("ref vec");
}


//cout << "\n\nreference to obj from vector without function (i.e. local):" << endl;

cout << "\n\n4) normal with function:" << endl;
do_some_calc(a,b,c,d,e);

cout << "\n\n5) reference to double obj with function:" << endl;
do_some_calc(a_ref,b_ref,c_ref,d_ref,e_ref);

cout << "\n\n6) reference to double obj from vector with function:" << endl;
do_some_calc(a_vref,b_vref,c_vref,d_vref,e_vref);

return 0;
}

#include "Timer.h"これは私が作成したカスタムで、ここで時間を計算するために使用しました

/*
Timer class for c++11 and pre c++11 (i.e. c++03 and c++99 etc) [version 0.1]
This is currently static and does not include multiple starts
Author:
currently tested on GCC only
*/
#ifndef TIMER_H
#define TIMER_H


#include <string>
#include <iostream>
#if (__cplusplus >= 201103L)
#include <chrono>   // include new c++11 object for timer
#include <ratio>
#else
#include <ctime>    // include pre c++11 object for timer
#endif

class Timer  {

private:
#if __cplusplus >= 201103L
typedef std::chrono::high_resolution_clock::time_point hiResClock;
typedef std::chrono::duration<long double,std::micro> micro_t;
hiResClock store;
#else
long double store;
#endif

public:
    void start(void);                       // [c++11]  method: start     timer
void finish(const std::string& disp);           // [both]   method: finish timer

};  // end of class Timer


inline void Timer::start(void)
{
#if __cplusplus >= 201103L
store = std::chrono::high_resolution_clock::now();
#else
store = (long double)std::clock()/CLOCKS_PER_SEC;
#endif
}

void Timer::finish(const std::string& disp)
{
std::cout << "Time taken: ";
#if __cplusplus >= 201103L
Timer::micro_t out = std::chrono::duration_cast<Timer::micro_t>    (std::chrono::high_resolution_clock::now()-store);
long double temp = out.count();
if(temp<1000)
    std::cout << out.count() << " micro-seconds" << std::endl;
else if(temp<1000000)
    std::cout << out.count()/1000 << " milli-seconds" << std::endl;
else if(temp<1000000000)
    std::cout << out.count()/1000000 << " seconds" << std::endl;
else if(temp<60000000000)
    std::cout << out.count()/60000000L << " minutes" << std::endl;
else
    std::cout << out.count()/3600000000ULL << " hours" << std::endl;
#else
    std::cout << ((long double)std::clock()/CLOCKS_PER_SEC-store) << " seconds" << std::endl;
#endif
    std::cout << "  For: " << disp << std::endl;
}

#endif  // instantiate Timer.h once
4

1 に答える 1

0

これは技術的には答えではありませんが、パフォーマンス測定を行うときはクロックを使用しないことをお勧めします。これは、テストを実行する時点で、CPU が SpeedStep モードである場合とそうでない場合があるためです (つまり、保存のために低い周波数で実行する)。パワー)。

代わりに、次の x86 固有のものを試してください。

http://en.wikipedia.org/wiki/Time_Stamp_Counter

次のように使用できます。

#include <cstdint>

// Read the CPU Time Stamp Counter
::uint64_t getTicks() noexcept
{
     register ::uint32_t lo, hi;
#ifdef SUPPORTS_RDTSCP
     __asm__ __volatile__ ("rdtscp" // On i7 we can remove cpuid and use rdtscp
            : "=a"(lo), "=d"(hi)
            :
            : );
#else
__asm__ __volatile__ ("cpuid \n\t rdtsc" // On lesser chips there is no RDTSCP instruction
            : "=a"(lo), "=d"(hi)    // Works in 32- or 64-bit modes (don't use "=A"!!!)
            :
            : "ebx", "ecx");        // Because of cpuid
#endif
    return (::uint64_t)hi<<32 | lo;
}

ご覧のとおり、使用しているチップのタイプに基づいて SUPPORTS_RDTSCP を定義する必要があります。

CPU の実行速度に関係なく、特定の命令シーケンスで経過したティック数を測定すると、ティック数はほぼ同じになります。パイプライン処理と順不同の実行により、わずかに異なるものになることに注意してください。ただし、使用しているクロックのものを使用するよりもはるかに近いです。

于 2013-03-27T09:23:18.600 に答える