473,569 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler optimizations

Word up!

If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?

Cheers.

Jan 15 '08 #1
29 1885
sammy wrote:
....
If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?
The answer to both questions is "possibly". WIthout knowing the
details of your code AND the details of gcc's optimization strategies,
we can't be sure. Something that is in general an optimization could
easily, for a specific program using specific inputs, be a
pessimization instead.

For gcc issues, I'd recommend using a forum specialized for gcc; this
isn't it.

Jan 16 '08 #2
On Jan 15, 3:43*pm, sammy <s...@noemail.s pamwrote:
Word up!

If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?
Higher optimization levels just means that compilers are requested to
use more esoteric types of optimization tricks. There is no guarantee
that {for instance} -O1 is faster than -O0 for that matter.
Take the example of inlining... It may make the code run faster due to
reduced function calls or it may make the code so large that stuff
that used to fit in the cache no longer does.

I suggest you direct specific performance problems with the GCC
compiler to the GCC compiler newsgroups.

P.S.
You can get good results with recent versions of GCC by using profile
guided optimization.
Jan 16 '08 #3
ja*********@ver izon.net wrote:
sammy wrote:
...
>If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?

The answer to both questions is "possibly". WIthout knowing the
details of your code AND the details of gcc's optimization strategies,
we can't be sure. Something that is in general an optimization could
easily, for a specific program using specific inputs, be a
pessimization instead.

For gcc issues, I'd recommend using a forum specialized for gcc; this
isn't it.
And he may be getting confused by the quantum effects on the system
timer.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 16 '08 #4
On Wed, 16 Jan 2008 00:43:25 +0100, sammy wrote:
Word up!

If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?
One point to ponder, which isn't specific to gcc but to optimisation in
general...

Many optimisations consume more space than the less optimised code. Loop
unrolling, for example, can do this. While this _can_ result in faster
code, it can _also_ potentially result in side effects such as exhausting
the cache memory. The net result can be a significant slowdown.

This sort of thing isn't really a bug; the optimiser has no way to know
what machines the code will run on.
Jan 16 '08 #5
Kelsey Bjarnason wrote:
On Wed, 16 Jan 2008 00:43:25 +0100, sammy wrote:
>Word up!

If there are any gcc users here, maybe you could help me out. I have
a program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with
-O3, even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?

One point to ponder, which isn't specific to gcc but to optimisation
in general...

Many optimisations consume more space than the less optimised code.
Loop unrolling, for example, can do this. While this _can_ result in
faster code, it can _also_ potentially result in side effects such as
exhausting the cache memory. The net result can be a significant
slowdown.

This sort of thing isn't really a bug; the optimiser has no way to
know what machines the code will run on.
If not the compiler/optimizer, who else?

Bye, Jojo
Jan 16 '08 #6
"Kelsey Bjarnason" <kb********@gma il.comwrote in message
>
This sort of thing isn't really a bug; the optimiser has no way to know
what machines the code will run on.
What input is probably more significant.
The length of an unbounded string is almost certainly a few tens of bytes or
less, for instance, so it makes sense to run a byte-by-byte strlen().
However if the string happens to be a DNA sequence then it may be hundreds
of kilobytes long, and so aligning to a word boundary and doing 32 or 64 bit
fetches will speed up code considerably. It is extremely difficult to tell a
compiler the difference between a sequence and a username, they are both
just strings of arbitrary length, to it.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 16 '08 #7
Kelsey Bjarnason wrote:
This sort of thing isn't really a bug; the optimiser has no way to know
what machines the code will run on.
If the compiler writers think that this is relevant and they need to figure
out a way to know any property of the target machine, couldn't they simply
add an option that enables the user to specify those target properties?
Rui Maciel
Jan 16 '08 #8
sammy wrote:
Word up!

If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.

Have I found a bug in gcc? Could I be doing something wrong?

Cheers.
According to my experience with gcc all optimizations beyond 2 are
a waste of time.

The problem with gcc is that every person interested in compiler
algorithms has hacked gcc to put his/her contribution, making
the whole quite messy.

Within lcc-win, I have targeted only ONE optimization strategy:

Code size.

There is nothing that runs faster than a deleted instruction. Lcc-win
features a very simple peephole optimizer, that is after a single
goal: delete redundant loads/stores, and in general to try to
reduce the code size as much as possible.

No other optimizations are done (besides the obvious ones done at
compile time like constant folding, division by constants, etc)

Gcc tries it all. I think there is no optimization that exists
somewhere in compiler books that hasn't been tried in gcc.
Code movement/aligning of the stack/global CSE/
aggressive inlining/ and a VERY long ETC!

The result is not really impressing. the compiler is very slow
and the program is not very fast:

A matrix multiplication program for instance: (time in seconds)

lcc-win -O 1.851
gcc -O2 1.690
gcc -O3 1.802
gcc -O9 1.766
MSVC -Ox 1.427

With -O3 gcc is as slow as lcc-win (what is obviously an excellent
result ) And the delta between gcc and lcc-win in the best case
for gcc is just... 3.1%

If you look at the compilation speed of lcc-win vs gcc (a factor
of 5 or more) and the size of the source code (11MB of C for gcc,
1MB of C for lcc-win) things look clearer.

What is worst for the optimizer compilers is that CPUs are now
so complex that optimizations that before were fine like inlining
have completely lost all their justification now that a processor
can wait up to 50 cycles doing nothing waiting that the RAM
gives it the information.

In this context optimizing for SIZE is a winning strategy. And
allows lcc-win to have almost the same speed as gcc with a FRACTION
of the effort.

Just my $0.02

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jan 16 '08 #9
"sammy" <sa*@noemail.sp amwrote in message
news:sl******** ***********@nos pam.invalid...
If there are any gcc users here, maybe you could help me out. I have a
program and I've tried compiling it with -O2 and -O3 optimization
settings.

The wired thing is that it actually runs faster with -O2 than with -O3,
even though -O3 is a higher optimization setting.
That sometimes happens. Some optimizations, particularly at GCC's higher
levels, are not guaranteed: they pay off most of the time, but sometimes
they hurt you. Also, many of the optimizations depend on the compiler
knowing the exact characteristics of the machine you'll run the code on; if
you tell GCC you have a i386 or a P4, but run the code on an Opteron, you
may get slower execution than if you told it you had an Opteron or used a
lower optimization level.

Depending on the code, using profile-guided optimization can provide a
significant performance boost as the compiler has more data on your specific
program (and your input data) versus static predictions that are tuned for
the "average" program.
Have I found a bug in gcc? Could I be doing something wrong?
If you flip a coin and guess the wrong result, is the coin buggy? No.

A compiler bug is when it doesn't properly translate a correct program.
Unless you're an expert, the most likely cause of improper results is that
your program isn't as correct as you think it is. Many advanced
optimizations cause odd results in C's undefined corners that compiling
simpler optimizations (or none at all) won't expose.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Jan 16 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1700
by: Andrew | last post by:
Where can one find a discussion of the current and future optimizations that the C# compilers do? When I see things like the following I question my faith in csc being good at optimizing. The same kind of thing shows up in cordbg when looking at the JIT code for a similar example % cat y.c using System class A bool On { get { return...
23
2264
by: JKop | last post by:
I'm looking for a compiler, I'm running Windows XP. Can anyone suggest a good one that can output assembly and that has all sorts of good optimizations in it, all sorts of bells and whistles. I'm using Dev C++ at the moment, but it has approximately 9999999999999999999999999999999 bugs in it, so if anyone can suggest a decent environment too,...
14
3114
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab compiler for the embedded target. My question is about how compilers optimize certain code sequences. As an example, take the code below. Will the...
1
1645
by: VM | last post by:
Hello, I'm looking for information on C# compiler optimization or compilation for an engineering short paper I want to write. Any sites with some technical info on the new advances of C# compilation would be greatly appreciated. Any sites that have comparisons between C# compilation an any other language would be even better. My email is...
3
1350
by: babak | last post by:
Hi I am running a project in eVC 4.0 and I have been running into a bug that only appears in the release build of the project. I eventually found out that when I had the compiler option /Od set the project would work properly but when it was not set the bug would appear. I looked for /Od in the MSDN library but it didn't provide much help....
44
3196
by: Don Kim | last post by:
Ok, so I posted a rant earlier about the lack of marketing for C++/CLI, and it forked over into another rant about which was the faster compiler. Some said C# was just as fast as C++/CLI, whereas others said C++/CLI was more optimized. Anyway, I wrote up some very simple test code, and at least on my computer C++/CLI came out the fastest. ...
0
1030
by: Mark Dufour | last post by:
Hello all, As Bearophile pointed out, I have just released Shed Skin 0.0.8. For those of you that do not know Shed Skin, it is an optimizing Python-to-C++ compiler, that allows for translation of pure (unmodified) Python programs into optimized machine language. The speed of generated code is typically 2-40 times, 12 on average, faster...
5
2377
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
7
1959
by: llothar | last post by:
Does anybody have some benchmarks or links to articles that compare this for different compiler implementations? I would especially like to see if it is usefull on MSVC, Intel 9.0 C and gcc. Also what is about the effect of "interprocedural optimization". All my use cases are 98% integer performance dominated. Currently i only use -O2 or...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.