以下のコードを検討してください。
#include <iostream>
#include <random>
#include <chrono>
#include <memory>
const int N = 1 << 28;
int main()
{
const int seed = 0;
std::mt19937 gen;
std::uniform_real_distribution<double> dis;
std::normal_distribution<double> normal;
std::unique_ptr<bool[]> array = std::unique_ptr<bool[]>(new bool[N]);
for (int i = 0; i < N; i++)
{
if (dis(gen) > 0.5)
array[i] = true;
else
array[i] = false;
}
int sum = 0;
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++)
{
if (array[i])
sum++;
}
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count() << " microsecond" << std::endl;
std::cout << sum << std::endl;
sum = 0;
t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++)
{
sum+=array[i];
}
t2 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count() << " microsecond" << std::endl;
std::cout << sum << std::endl;
}
行にコメントを付けるとstd::cout << sum << std::endl;
、実行時間はゼロ (または十分に近い) として表示されます。O3 コンパイル フラグを使用して、icpc、icl (v19.1.2)、g++ (v9.2) など、さまざまなコンパイラで確認しました。
これはアウトオブオーダー (動的) 実行の例ですか?