c++c++11

clang osx march=native approximately 1/3rd speed of no optimisation


I have a project where speed is paramount, so was experimenting with compiler flags to try and get some free performance. I have two builds that are identical except for the additional flag march=native in build 2.

For completeness the flags are:

A) -std=c++14 -g -fno-omit-frame-pointer -O3

B) -std=c++14 -g -fno-omit-frame-pointer -O3 -march-native

Running benchmarks on these builds yields a confusing result:

A) 61s

B) 160s

What can possibly going on here?


Solution

  • Using -march=native optimizes code for your current cpu. Most of the time, it will improve the speed of execution. Sometimes it may not produce the fastest possible code because it enables certain CPU instructions.

    echo | clang -E - -march=native -###
    

    will display what clang has enabled through -march=native. The most likely culprit is CMOV, which is enabled by -march=native. You can see an explanation of why that might slow things down in the answer to this question: gcc optimization flag -O3 makes code slower than -O2.