c++ - 为什么 C++ 可执行文件在与较新的 libstdc++.so 链接时运行速度如此之快?

标签 c++ gcc optimization libstdc++

我有一个项目(代码 here ),在其中运行基准测试来比较计算点积的不同方法(Naive 方法、Eigen 库、SIMD 实现等)的性能。我正在新的 Centos 7.6 虚拟机上进行测试。我注意到,当我使用不同版本的 libstdc++.so.6 时,我获得了显着不同的性能。

当我启动新的 Centos 7.6 实例时,默认的 C++ 标准库是 libstdc++.so.6.0.19。当我运行基准测试可执行文件(链接到此版本的 libstdc++)时,输出如下:

Naive Implementation, 1000000 iterations: 1448.74 ns average time
Optimized Implementation, 1000000 iterations: 1094.2 ns average time
AVX2 implementation, 1000000 iterations: 1069.57 ns average time
Eigen Implementation, 1000000 iterations: 1027.21 ns average time
AVX & FMA implementation 1, 1000000 iterations: 1028.68 ns average time
AVX & FMA implementation 2, 1000000 iterations: 1021.26 ns average time

如果我下载 libstdc++.so.6.0.26 并更改符号链接(symbolic link) libstdc++.so.6 以指向这个较新的库并重新运行可执行文件(无需重新编译或更改其他任何内容),结果如下:

Naive Implementation, 1000000 iterations: 297.981 ns average time
Optimized Implementation, 1000000 iterations: 156.649 ns average time
AVX2 implementation, 1000000 iterations: 131.577 ns average time
Eigen Implementation, 1000000 iterations: 92.9909 ns average time
AVX & FMA implementation 1, 1000000 iterations: 78.136 ns average time
AVX & FMA implementation 2, 1000000 iterations: 80.0832 ns average time

为什么速度有如此显着的提高(某些实现快了 10 倍)?

由于我的用例,我可能需要链接到 libstdc++.so.6.0.19。在使用旧版本的 libstdc++ 时,我可以在代码中/在我这边做些什么来查看这些速度改进吗?

编辑: 我创建了一个最小的可重现示例。

main.cpp

#include <iostream>
#include <vector>
#include <cstring>
#include <chrono>
#include <cmath>
#include <iostream>

typedef std::chrono::high_resolution_clock Clock;

const size_t SIZE_FLOAT = 512;

double computeDotProductOptomized(const std::vector<uint8_t>& v1, const std::vector<uint8_t>& v2);
void generateNormalizedData(std::vector<uint8_t>& v);

int main() {
     // Seed for random number
    srand (time(nullptr));

    std::vector<uint8_t> v1;
    std::vector<uint8_t> v2;

    generateNormalizedData(v1);
    generateNormalizedData(v2);

    const size_t numIterations = 10000000;
    double totalTime = 0.0;

    for (size_t i = 0; i < numIterations; ++i) {
        auto t1 = Clock::now(); 
        auto similarity = computeDotProductOptomized(v1, v2);
        auto t2 = Clock::now();

        totalTime += std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
    }

    std::cout << "Average Time Taken: " << totalTime / numIterations << '\n';

    return 0;
}

double computeDotProductOptomized(const std::vector<uint8_t>& v1, const std::vector<uint8_t>& v2) {
    const auto *x = reinterpret_cast<const float*>(v1.data());
    const auto *y = reinterpret_cast<const float*>(v2.data());

    double similarity = 0;

    for (size_t i = 0; i < SIZE_FLOAT; ++i) {
        similarity += *(x + i) * *(y + i);
    }

    return similarity;
}

void generateNormalizedData(std::vector<uint8_t>& v) {
    std::vector<float> vFloat(SIZE_FLOAT);
    v.resize(SIZE_FLOAT * sizeof(float));

    for(float & i : vFloat) {
        i = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
    }

    // Normalize the vector
    float mod = 0.0;

    for (float i : vFloat) {
        mod += i * i;
    }

    float mag = std::sqrt(mod);

    if (mag == 0) {
        throw std::logic_error("The input vector is a zero vector");
    }

    for (float & i : vFloat) {
        i /= mag;
    }

    memcpy(v.data(), vFloat.data(), v.size());
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(dot-prod-benchmark-min-reproducible-example C CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Ofast -ffast-math -march=broadwell")
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 14)

add_executable(benchmark main.cpp)

centos-release-7-6.1810.2.el7.centos.x86_64上编译,使用cmake版本3.16.2gcc (GCC) 7.3 .1 20180303 Intel(R) Xeon(R) Gold 6140 CPU @ 2.30GHz,4 个 vCPU

使用libstdc++.so.6.0.19:平均花费时间:1279.41 使用libstdc++.20.6.0.26:平均花费时间:168.219

最佳答案

rustyx是正确的。正是在循环中使用了 auto t1 = Clock::now(); 导致了性能不佳。一旦我将计时移到循环之外(时间是花费的总时间),那么它们运行得同样快:

    const size_t numIterations = 10000000;
    auto t1 = Clock::now(); 

    for (size_t i = 0; i < numIterations; ++i) {
        auto similarity = computeDotProductOptomized(v1, v2);
    }

    auto t2 = Clock::now();

    std::cout << "Total Time Taken: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << " ms\n";

关于c++ - 为什么 C++ 可执行文件在与较新的 libstdc++.so 链接时运行速度如此之快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59570753/

相关文章:

c++ - OpenCV 中的叠加图像

c++ - 检测继承的函数是否被覆盖

c - #define 与 static const 的优化(在 avr-gcc 中)

c++ - 灰度双线性 block 提取 - SSE 优化

java - 大输入上的慢速字符串连接

c - 查找具有一定步数/计数的最小起始值的数字

c++ - 使用 WxWidgets 异步执行

c++ - oop - C++ - 实现特定类型行为的正确方法?

c - 在内存中使用位域的缺点

c++ - boost 的正则表达式无法编译