Does compiling on your computer really improve performance?

Asked

Viewed 673 times

16

Any programmer knows that when building a C/C++, the compiler can optimize the code to generate faster executables. But, it is also said that there is the 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 peak performance on the Intel Core i3 but will normally run on other x86_64 processors. I don’t know if that’s true, just saying.

Based on this, I see a lot to say around 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 make use of this idea to say that Gentoo Linux is the fastest distribution on the planet (Gentoo is a distribution where everything is installed directly from source code, from kernel to most basic applications).

But I’m suspicious of all this. Is this talk that something compiled on your computer will run at peak performance on it real? And, adding: The fact that Gentoo Linux is a distribution where everything is installed directly from the source code actually makes it a system that has a remarkable performance?

1 answer

12

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

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

  • -march=cpu-type

    Sets the minimum that the compiler should assume 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 you can write like this: -march=native and the optimization will be based on the processor of your machine. If the code is to be executed only by you, use this flag. Not forgetting of course the other optimization options like -O3 and -fomit-frame-pointer.

  • -mtune=cpu-type

    That option is similar. The compiler will generate code optimized for that particular model, but it also generates conditionals to check whether the functionality is actually present in the processor and includes alternative implementations. In practice the code gets bigger, 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 the -mavx to indicate that you can use AVX.

3.17.17 Intel 386 and AMD x86-64 Options

In my machine the architecture used by default is the i686 (Pentium Pro) of 1995. So without a doubt there is a significant improvement in performance, especially in code that can be vectored.

  • Interesting. But the -O3 option is not dangerous because it is dangerous for the compiler to change the behavior of the program?

  • @Sid For programs that are not full of Undefined behaviour and related, no. No optimization changes well-defined code behavior. If you need to switch options for your code to work, there is something wrong with the code and not with the option ;)

  • So I think playing -O3 in the Gentoo file of which I don’t remember the name, a file that configures global flags by compiler is not recommended. Compiling things straight from source I see thousands of warnings of unused variables, deprecated functions, etc. But, and the talk that Gentoo is the fastest distribution that exists because it’s all made from source code, it’s true?

  • @Sid If you’re compiling with -march=native the performance gain is about 30%, in general. But remember that to tell you whether it is faster or not you need to attend oranges with oranges. Geento will be faster than any other distribution that uses the same packages in the same versions. When you start comparing bananas with apples none of this makes sense. About optimizing break a library, I would consider this a bug.

  • No, I don’t think it would be a bug because it has the premise that the product developers are 100% concerned about the quality of the code, which is not so. If it were, I wouldn’t see thousands of warnings while compiling Mate. Besides, the fact that the library is out there doesn’t mean that it’s good, Openssl says so. Have you seen the code of that freak?

  • Well, then I guess the answer is that specific optimization only for your computer exists, doesn’t it? But not by default, it requires flags.

  • @Sid I consider a bug, but it is likely that they mark as WONTFIX. Ai goes from library to library. Even the guys from glibc claim that it is impossible to make a libc without warnings... About optimization, yes. If you just compile without touching the flags (probably Gentoo does) you get nothing better than a generic binary downloaded from the internet. But you should also turn on some optimization, otherwise there will be no way for the compiler to take advantage of the latest instructions. At least -ftree-vectorize.

  • Looking at all this, I wonder what is this Gentoo: If static optimization is only available through flags, What is the use of creating a system from which you spend hours and hours compiling your apps to have a full OS and ultimately the result will be the same as a generic binary distribution? - So I don’t even think about trying Gentoo.

  • @Sid Ora, Gentoo uses these flags, if nothing would make sense.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.