Почему исполняемый файл, разработанный clang ++, медленнее, чем g ++? - PullRequest
0 голосов
/ 03 апреля 2020

Для одного и того же кода на том же компьютере, построенного g++ и clang++ соответственно, почему более поздний код занимает гораздо больше времени, чем предыдущий?

➜ проект> cat branch_prediction_victim.cpp

#include <algorithm>
#include <ctime>
#include <iostream>

int main(int argc, char* argv[])
{
    const size_t array_length = 32768;
    int array_aka[array_length];
    std::srand(std::time(nullptr));
    for (size_t i = 0; i < array_length; ++i)
        array_aka[i] = std::rand() % 256;

    // !!! With this, the next loop runs faster.
    std::sort(array_aka, array_aka + array_length);

    clock_t start = clock();
    long long sum = 0;
    for (int i = 0; i < 100000; ++i)
        for (size_t j = 0; j < array_length; ++j)
            if (array_aka[j] >= 128)
                sum += array_aka[j];
    double elapsed_seconds = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;
    std::cout << elapsed_seconds << std::endl;
    std::cout << "sum = " << sum << std::endl;
}

➜ проект> g++ -O3 -o run branch_prediction_victim.cpp && ./run

0.776086
sum = 314696600000

➜ проект> clang++ -O3 -o run branch_prediction_victim.cpp && ./run

1.00953
sum = 314878900000

➜ проект> g++ --version

g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Проект 1020 *>> clang++ --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

➜ проект>

...