LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-17-2005, 11:33 AM   #1
csvke
Member
 
Registered: Oct 2003
Location: Hong Kong
Distribution: Slackware
Posts: 96

Rep: Reputation: 15
Compile from source - increase in performance?


Heard from someone that it's possible to compile software from source and play with gcc flags to increase performance comparing with pre-comiled binaries... is it true?

as far as i know, re-compiling kernel with suitable options that match your needs and hardware would speed up the system and shrink down the size of the binaries and i can learn some of its techniques in books like O'Reilly understanding linux kernel... but how about other software such as firefox? I use mac mainly and people's been going firebox optimized for G4 and things like that and i'd like to learn but don't know where to start with..
 
Old 01-17-2005, 12:36 PM   #2
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
contrary to popular belief size of the binaries and speed are often not in any way the same thing..
Linux and GNU programs in particular are optimized for speed not size..
But with the kernel like you said just eliminating stuff you don't use is a different story.
Todays processors have some very deep pipes that can handle things like urolled loops which are larger and faster. (don't know about motorola cpus) Also binaries can be sripped of debugging symbols for size but contrary to popular belief this has no effect on the amount of symbols loaded at runtime. This may effect scan time in some way however looking for symbols.

So anyway yes you can increase the perfomance of you machine greatly with compiler flags but do lots of benchmarking first to decide what flags to use and realize alot of what you read on the internet about this is just mythology.

When you compile the kernel you have to hack the Makefile to add the gcc flags and get verbose output so you can see the exact effect of what you are doing. The kernel takes well to agressive optimizations.

Some other programs if you use agressive optimizations end up broken and unstable
(both Firefox and X where broken by optimizations on my machine)

also never try to optimize the core like gcc itself or glibc.

have fun

Last edited by foo_bar_foo; 01-17-2005 at 12:37 PM.
 
Old 01-17-2005, 05:59 PM   #3
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
see here (assuming your running an x86 or x86_64 architecture )
for details on what options you can pass. But be warned, as foo_bar_foo stated above, if you try to over do it you may end up with a program that behaves oddly/crashes regularly. to set the flags do:
Code:
export CFLAGS=" <options "
So for example if you wanted to optimize for your processor ( which we'll assume is an athlon-xp for this example):


Code:
export CFLAGS=" -mcpu=athlon-xp -march=athlon-xp"
the -mcpu options tells the compiler to optimize for the athlon-xp but allow older processors to still run the code. The -march option tells the compiler to fully optimize the code for the athlon-xp, but to the point where athlon-xp is the minimum processor which will run the code.

You will have to experiment with different options to see what you like best. My personal opinion is stick with light optimizations ( like -mcpu and such ) as the more you throw in, the less stable things tend to become. Also, the perfomance gain isn't exactly night and day either. You may get a touch more responsiveness ( this is debatable) but overall ( In my opinion ) its not worth recompiling your whole system over to try to get better performance.

ps - if your compiling c++ code, you'll have to set CXXFLAGS too like this (assuming you have set CFLAGS like above):
Code:
export CXXFLAGS=$CFLAGS

Last edited by __J; 01-17-2005 at 06:02 PM.
 
Old 01-17-2005, 07:33 PM   #4
kuranosuke
LQ Newbie
 
Registered: Sep 2004
Posts: 5

Rep: Reputation: 0
http://gcc.gnu.org/onlinedocs/gcc-3....002d64-Options

Although, to note: you probably will not see much of (if any) a NOTICEABLE speed increase.
See also: Gentoo
And this thread here:
http://episteme.arstechnica.com/eve/...m=811002048631
 
Old 01-17-2005, 08:22 PM   #5
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Some applications really do benefit greatly from being built with options specific to your hardware - mplayer for example.


Håkan
 
Old 01-17-2005, 10:29 PM   #6
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Rep: Reputation: 34
Quote:
Originally posted by foo_bar_foo
Some other programs if you use agressive optimizations end up broken and unstable
(both Firefox and X where broken by optimizations on my machine)
I may not be optimizing as aggressively as you, but my own Firefox build, optimized for Pentium 4 / SSE-2 using -O3 and a few other flags, runs well on my Linux box.
 
Old 01-17-2005, 10:49 PM   #7
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
Quote:
Originally posted by daihard
I may not be optimizing as aggressively as you, but my own Firefox build, optimized for Pentium 4 / SSE-2 using -O3 and a few other flags, runs well on my Linux box.
this is interesting and an indicator of how complex it can all get
i had to take away even the default optimizations or i got the loop thing at startup even as root..
what you said implies it's because i optimized other underlying libs.
 
Old 01-17-2005, 11:06 PM   #8
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
In my opinion, as long as you stick to modest optimizations, like the O flag and cpu optimizations, you'll be alright. But in my experience, trying to throw too many flags into it will break some code. And it also depends on the flags themselves, as optimizing for a pentium 4 gcc will do completely different things than if had optimized for an athlon-xp or something else.
 
Old 01-17-2005, 11:13 PM   #9
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Rep: Reputation: 34
This discussion leaves me wondering what optimization is all about. After all, all the source code is written in grammatically correct language (be it C or Java or whatever else) by definition; otherwise it would not even compile. Yet optimizing that grammatically correct code may break something - to me, that can only mean a bug (or bugs) in the compiler.

I remember the flag "-match=pentium4" did not work with GCC 3.2.x when I compiled Firefox with it. The binary was built, but it would not run. The exact same flag now works with GCC 3.3.x.
 
Old 01-17-2005, 11:31 PM   #10
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
I don't think it's a bug, I mean when you optimize moderately, chances are you'll never have any problems.
But when you start throwing some of these in at random, you could break the build ( especially if the programmer used things which were not illegal, but not technically correct either like depending on sizes of structs, manipulating pointers in unintended ways, etc...). Note that some of those options above when given to gcc will tell gcc to break the ANSI standard if it has to..
 
Old 01-17-2005, 11:41 PM   #11
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
daihard, those are features - not bugs.

Yeah, it's a common sarcastic expression but this time I actually mean it. The optimization flags passed to gcc do not really have anything to do with the code itself. They control the way gcc handles different operations - like unrolling loops to save a few cpu cycles but spend a few more cpu registers.

The flags differ between different versions of the gcc compiler, and CPU types are constantly added or altered: The AMD processor family formerly known as "k8" is now referred to as "athlon-4" in gcc 3.3.5, and so on.


Håkan
 
Old 01-17-2005, 11:54 PM   #12
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Rep: Reputation: 34
I know what you guys are saying, but when it gets right down to business, my opinion is that the compiler should not attempt to optmize the code in a way that may break the resulting binary, no matter how badly the source is written.

I am a C programmer, so I know it's very easy to abuse the C language. However, most of the modern C compilers are able to give you warning about bad, if not illegal, code (such as using pointers interchangeably with non-pointer data types). I agree with you that code full of such warnings may result in non-working binaries if optimized aggressively, but clean code should always be built to run - whether or not optimized. In my opinion, anyway.

(But again, I know it's not too hard to fool the compiler to give you no warnings using explicit casts and other things...)

As for the "-march=pentium4" flag, it was present with GCC 3.2. It just did not work as intended, at least when I built Firefox.
 
Old 01-18-2005, 01:12 AM   #13
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
Quote:
Originally posted by kuranosuke

Although, to note: you probably will not see much of (if any) a NOTICEABLE speed increase.
i disagree with this
i use a very basic non-esoteric benchmark as an example

on my machine with -O flag
10 second file copy 63787KBps
with -march=pentium4 -O3 -funroll-loops -ftracer -momit-leaf-frame-pointer -fprefetch-loop-arrays
10 second file copy 71242KBps

that's what -- a little over 11.5% increase on a basic simple operation
 
Old 01-18-2005, 01:56 AM   #14
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
Quote:
Originally posted by daihard
my opinion is that the compiler should not attempt to optmize the code in a way that may break the resulting binary, no matter how badly the source is written.

wow that would mean just one compiler that compiles things only one way..
(lost in 1982 forever no matter where the hardware goes)
then you just pretend like the code is good just cause you can get it to compile (aka Micr$ft)..

sometimes the compiler has bugs but more often then not it's the code with bugs and design problems that are being brought out by the optimizations and are hiddden otherwise..

so code should be tested on all kinds of equipment and with all kinds of optimizations and even be tested on different compilers to weed out the funky stuff.

you cold turn this around and say -- no matter how badly the compiler is written no code should be written so it breaks when optimized for certain architectures.
 
Old 01-18-2005, 02:19 PM   #15
lnthai2002
Member
 
Registered: Jan 2005
Location: Montreal, QC, CANADA
Distribution: Red Hat Fedora
Posts: 135

Rep: Reputation: 15
Quote:
Originally posted by hw-tph
Some applications really do benefit greatly from being built with options specific to your hardware - mplayer for example.


Håkan
Well, i dont know if it's true or not, but for me, although i compile mplayer 4 times with many options (-gui, -crash-debug, cpuruntime-detection, --enable-dvdread) it still crash whenever i play dvd from dvd+rw !!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Increase performance amnesty_puppy Debian 18 11-24-2004 02:44 PM
Increase Performance berkay Fedora 10 08-23-2004 04:24 PM
how to increase performance(geforce2) mandosi Linux - Hardware 0 03-17-2004 04:21 AM
CGI Performance Increase?? bretthoward Programming 9 01-25-2003 07:33 PM
Why does performance increase when I mount? Vlad_M Linux - Hardware 4 10-11-2002 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration