views:

148

answers:

6

I'm using the standard gcc compiler in math software development with C-language. I don't know that much about compilers or compiler options, and I was just wondering, is it possible to make faster executables using another compiler or choosing better options? The default Makefile sets options -ffast-math and -O3 and I think both of them have some impact in the overall calculation time. My software is using memory quite extensively, so I imagine some options related to memory management might do the trick?

Any ideas?

+1  A: 

If you have specific hardware you can target your code for, the (hardware) company often releases paid-for compilers optimized for that hardware.

For example:

These compilers will generally produce better code optimization-wise.

Dan McGrath
One one project (in 2006 or 2007), we examined an older version of gcc versus the Sun C++ compiler, and our quick evaluation showed the Sun compiler to be about twice as fast for our calculations. Unfortunately, incompatibilities with Oracle's OCCI prevented us from using it, as well as a more recent version of gcc.
David Thornley
+3  A: 

If you are running Linux on x86 then typically the Intel or PGI compilers will give you significantly faster performing executables.

The downsides are that there are more knobs to tune and that they come with a hefty price tag!

Tom
EKO Pathscale makes fast code too. As a rule of thumb I expect any of these 3 compilers to reduce run time by 40% right out of the box. Of course, like all generalisations, this one is suspicious.
High Performance Mark
I'll just add that a couple of thousand dollars (or a few hundred or whatever the compilers cost) is nothing compared to the cost of having software developers hand-tuning code for much smaller performance improvements, or of having code running slowly throughout its life.
High Performance Mark
as far as I know gcc is very good at optimizing codes for performance; if you read the info page for gcc, Optimize Options, you see there are a lot of optimize options; -On is just a short form for common ones. Then the only way to make it even better is to write better code: better algorithms and less memory access (the user says it uses memory extensively)
ShinTakezou
It's worth noting that Intel compiler boosts performance only on Intel CPUs.
el.pescado
@High Performance Mark: Agreed on the cost of hand-tuning, but having the code run "slowly" is often perfectly acceptable. It depends on what the code is, what it's doing, how often it's used, and whether it's anywhere near CPU-bound, among other things.
David Thornley
@David: you're right that slow is OK for some. I tend to forget that not everyone on SO is working in high-performance computing in the oil business :-)
High Performance Mark
+1  A: 

As you say your program is memory heavy you could test to use a different malloc implementation than the one in standard library on your platform. For example you could try the jemalloc (http://www.canonware.com/jemalloc/).

cjg
+4  A: 

Here are some tips about gcc performance: do benchmarks with -Os, -O2 and -O3. Sometimes -O2 will be faster because it makes shorter code. Since you said that you use a lot of memory, try with -Os too and take measurements.

Also check out the -march=native option (it is considered safe to use, if you are making executable for computers with similar processors) on the client computer. Sometimes it can have considerable impact on performance. If you need to make a list of options gcc uses with native, here's how to do it:
Make a small C program called test.c, then

$ touch test.c
$ gcc -march=native -fverbose-asm -S test.c
$ cat test.s

credits for code goto Gentoo forums users. It should print out a list of all optimizations gcc used. Please note that if you're using i7, gcc 4.5 will detect it as Atom, so you'll need to set -march and -mtune manually.

Also read this document, it will help you (still, in my experience on Gentoo, -march=native works better) http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html

You could try with new options in late 4.4 and early 4.5 versions such as -flto and -fwhole-program. These should help with performance, but when experimenting with them, my system was unstable. In any case, read this document too, it will help you understand some of GCC's optimization options http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

AndrejaKo
+5  A: 

Before experimenting with different compilers or random, arbitrary micro-optimisations, you really need to get a decent profiler and profile your code to find out exactly what the performance bottlenecks are. The actual picture may be very different from what you imagine it to be. Once you have a profile you can then start to consider what might be useful optimisations. E.g. changing compiler won't help you if you are limited by memory bandwidth.

Paul R
+1 profiling and getting awareness about where your program spends most of its time is always one of the better things to do to produce first better code (and then relying only on compilers' optimizations after we're sure we are using the best algorithms and code...)
ShinTakezou
+1. What ShinTakezou said.
John R. Strohm
+1  A: 

Keep in mind they most improvements to be had by changing compilers or settings will only get you proportional speedups where as adjusting algorithms you can sometimes get improvements in the O() of your program. Be sure to exhaust that before you put to much work into tweaking settings.

BCS