Is it realistic to use -O3 or -Ofast to compile your benchmark code or will it remove code?

后端 未结 3 877
猫巷女王i
猫巷女王i 2021-01-18 23:25

When compiling the benchmark code below with -O3 I was impressed by the difference it made in latency so i began to wonder whether the compiler is not \"cheatin

3条回答
  •  借酒劲吻你
    2021-01-19 00:00

    You should always benchmark with optimizations turned on. However it is important to make sure the things you want to time do not get optimized away by the compiler.

    One way to do this by printing out calculation results after the timer has stopped:

    long long x = 0;
    
    for(int i = 0; i < iterations; i++) {
    
        long start = get_nano_ts(); // START clock
    
        for(int j = 0; j < load; j++) {
            if (i % 4 == 0) {
                x += (i % 4) * (i % 8);
            } else {
                x -= (i % 16) * (i % 32);
            }
        }
    
        long end = get_nano_ts(); // STOP clock
    
        // now print out x so the compiler doesn't just ignore it:
        std::cout << "check: " << x << '\n',
    
        // (omitted for clarity)
    }
    

    When comparing benchmarks for several different algorithms that can also serve as a check that each algorithm is producing the same results.

提交回复
热议问题