Compiling on your computer actually improves performance?

15

Any programmer knows that when compiling a C / C ++, the compiler can optimize code to generate faster executables. But, it's also said there is compiler optimization for your processor.

This means that if I created a C program and compiled it on a desktop with an Intel Core i3, the program is optimized to run at maximum performance on Intel Core i3 but will run normally on other x86_64 processors. I do not know if this is true, just saying.

Based on this, I see a lot to say that the best way to have a program running at maximum efficiency on your computer is to install it directly from the source code. So much so that some Gentoo Linux enthusiasts use this idea to say that Gentoo Linux is the fastest distribution on the planet (Gentoo is a distribution where everything is installed directly from the source code, from the kernel to the most basic applications). >

But I'm wary of all this. Is this talk that something compiled on your computer will run with the maximum performance on it is real? And adding: Does Gentoo Linux be a distribution where everything is installed directly from the source code really makes it a system that has a remarkable performance?

    
asked by anonymous 17.05.2014 / 10:01

1 answer

11

The fact is that when you compile a code with your compiler, by default it assumes you want to distribute the executable to others. So much as it optimizes, code needs to keep running on any popular processor in the market. Thus the compiler can not simply use very advanced instructions such as vectorization and the like.

To improve performance in this case there are two very interesting GCC options:

  • -march= cpu-type

    Defines the minimum that the compiler should take over the CPU. If you specify one that supports AVX instructions for example, the compiler will use them freely without worrying about the code not working on previous processors. The cool thing is that you can type like this: -march=native and the optimization will be based on the processor of your machine. If the code is to be run only by you, use this flag. Not forgetting other optimization options such as -O3 and -fomit-frame-pointer .

  • -mtune= cpu-type

    This option is similar. The compiler will generate code optimized for that particular model, but it also generates conditionals to check if the features are actually present in the processor and includes alternate implementations. In practice the code gets larger, but runs on any processor and has performance comparable to -march on the target processor.

    if (cpu suporta AVX)
        ComputeComAVX();
    else if (cpu suporta SSE2)
        ComputeComSSE2();
    else
        ComputeGenerico();
    

For GCC 4.9.0, you can find here the list of supported processors and other relevant flags, such as -mavx to indicate that you can use AVX.

3.17.17 Intel 386 and AMD x86-64 Options

On my machine the architecture used by default is the i686 (Pentium Pro) of 1995. So there's definitely a significant performance improvement, especially in code that can be vectorized.

    
17.05.2014 / 12:28