当前位置:  开发笔记 > 编程语言 > 正文

奇怪的基准测试结果

如何解决《奇怪的基准测试结果》经验,为你挑选了1个好方法。

我写了以下基准:

#include  // cout
#include    // pow
#include    // high_resolution_clock    

using namespace std;
using namespace std::chrono;

int64_t calculate(int);

int main()
{
    high_resolution_clock::time_point t1, t2;

    // Test 1
    t1 = high_resolution_clock::now();
    calculate(200);
    t2 = high_resolution_clock::now();

    cout << "RUNTIME = " <<  duration_cast(t2 - t1).count() << " nano seconds" << endl;

    // Test 2   
    t1 = high_resolution_clock::now();
    calculate(200000);
    t2 = high_resolution_clock::now();

    cout << "RUNTIME = " <<  duration_cast(t2 - t1).count() << " nano seconds" << endl;
}

int64_t calculate(const int max_exponent)
{
    int64_t num = 0;

    for(int i = 0; i < max_exponent; i++)
    {
        num += pow(2, i);
    }

    return num;
}

在Odroid XU3上运行此基准测试时,将生成以下输出(8次运行):

RUNTIME TEST 1 = 1250 nano seconds
RUNTIME TEST 2 = 1041 nano seconds

RUNTIME TEST 1 = 1292 nano seconds
RUNTIME TEST 2 = 1042 nano seconds

RUNTIME TEST 1 = 1250 nano seconds
RUNTIME TEST 2 = 1083 nano seconds

RUNTIME TEST 1 = 1292 nano seconds
RUNTIME TEST 2 = 1083 nano seconds

RUNTIME TEST 1 = 1209 nano seconds
RUNTIME TEST 2 = 1084 nano seconds

RUNTIME TEST 1 = 1166 nano seconds
RUNTIME TEST 2 = 1083 nano seconds

RUNTIME TEST 1 = 1292 nano seconds
RUNTIME TEST 2 = 1042 nano seconds

RUNTIME TEST 1 = 1166 nano seconds
RUNTIME TEST 2 = 1250 nano seconds

RUNTIME TEST 1 = 1250 nano seconds
RUNTIME TEST 2 = 1250 nano seconds

第二个指数是第一个指数的1000倍.为什么第二次通话有时会更快完成?

我使用GCC(4.8)作为编译器和-Ofast标志.

更新:我可以在我的i7 4770k上重现类似的行为.



1> Jerry Coffin..:

简短的回答是"死代码消除".编译器发现你从不使用调用函数的结果(并且函数没有副作用),所以它只是取消了调用函数.

打印出函数的结果,事情发生了一些变化.例如:

Ignore: -9223372036854775808    RUNTIME = 0 nano seconds
Ignore: -9223372036854775808    RUNTIME = 23001300 nano seconds

修改后的代码,以防您关心:

#include  // cout
#include    // pow
#include    // high_resolution_clock    

using namespace std;
using namespace std::chrono;

int64_t calculate(int);

int main() {
    high_resolution_clock::time_point t1, t2;

    // Test 1
    t1 = high_resolution_clock::now();
    auto a = calculate(200);
    t2 = high_resolution_clock::now();
    std::cout << "Ignore: " << a << "\t";

    cout << "RUNTIME = " << duration_cast(t2 - t1).count() << " nano seconds" << endl;

    // Test 2   
    t1 = high_resolution_clock::now();
    auto b = calculate(200000);
    t2 = high_resolution_clock::now();
    std::cout << "Ignore: " << b << "\t";

    cout << "RUNTIME = " << duration_cast(t2 - t1).count() << " nano seconds" << endl;
}

int64_t calculate(const int max_exponent) {
    int64_t num = 0;

    for (int i = 0; i < max_exponent; i++) {
        num += pow(2, i);
    }

    return num;
}

从那里你有一个小的细节,你已经超出范围int64_t(很多次)给出未定义的行为 - 但至少有这个是合理的希望打印出来的时间反映了执行指定计算的时间.

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有