473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Compiling with -O option gives different running time!

Hi,

When I compile my program adding -O option, the running time of my
program is much smaller. For example if i compile my program like this

g++ -Wall -g prog2.cc avltree.cc cells_list.cc hashsep_query.cc
hashsep_point.cc int_list.cc loadavltree.cc -o p2 -lm

its run time is around 12 seconds.

but if i compile it with

g++ -Wall -g -O prog2.cc avltree.cc cells_list.cc hashsep_query.cc
hashsep_point.cc int_list.cc loadavltree.cc -o p2 -lm

its run time is around 7 seconds.
I am really new at C++ so can't figure it out why is it so different.
Any idea?

Thanks,
Aamir

Jun 13 '06 #1
17 1746

aamirche...@gmail.com wrote:
Hi,

When I compile my program adding -O option, the running time of my
program is much smaller. For example if i compile my program like this


<snip>

run

man gcc

And then search for "-O"

Tom

Jun 13 '06 #2
aa*********@gmail.com wrote:

When I compile my program adding -O option, the running time of my
program is much smaller. For example if i compile my program like this

g++ -Wall -g prog2.cc avltree.cc cells_list.cc hashsep_query.cc
hashsep_point.cc int_list.cc loadavltree.cc -o p2 -lm
Urm ... this is off-topic twice (C++, and implementation-specific).
its run time is around 12 seconds.

but if i compile it with

g++ -Wall -g -O prog2.cc avltree.cc cells_list.cc hashsep_query.cc
hashsep_point.cc int_list.cc loadavltree.cc -o p2 -lm


If you don't know what -O is for, why did you add it to the command line?

--
Chris "and if you did, why are you asking?" Dollin
"We did not have time to find out everything we wanted to know." /A Clash of Cymbals/

Jun 13 '06 #3

aa*********@gmail.com wrote:
Hi,

When I compile my program adding -O option, the running time of my
program is much smaller. For example if i compile my program like this

g++ -Wall -g prog2.cc avltree.cc cells_list.cc hashsep_query.cc
hashsep_point.cc int_list.cc loadavltree.cc -o p2 -lm

its run time is around 12 seconds.

but if i compile it with

g++ -Wall -g -O prog2.cc avltree.cc cells_list.cc hashsep_query.cc
hashsep_point.cc int_list.cc loadavltree.cc -o p2 -lm

its run time is around 7 seconds.
I am really new at C++ so can't figure it out why is it so different.
Any idea?

-O is the "optimization compiler flag".
With that... the compilation time is a bit longer, but executables are
smaller and run much faster.
-O2 gives even better optimizations.
-O3 gives yet even better optimizations, but executables greater (in
kilobytes) than with -O2.

When releasing a program you should always compile it with -O2 or -O3
(and probably -fomit-frame-pointer).

Jun 13 '06 #4
"SuperKoko" <ta*******@yahoo.fr> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...

aa*********@gmail.com wrote:
When I compile my program adding -O option, the running time of my
program is much smaller.

What did you expect it to do? Making your program run faster is the entire
point of the -O option (in GCC at least, but also for most other compilers).
-O is the "optimization compiler flag".
With that... the compilation time is a bit longer, but executables are
smaller and run much faster.
-O2 gives even better optimizations.
-O3 gives yet even better optimizations, but executables greater (in
kilobytes) than with -O2.

When releasing a program you should always compile it with -O2 or -O3
(and probably -fomit-frame-pointer).


Unless, of course, your program no longer gives correct output with those
optimization levels due to compiler bugs, or you care about the ability to
debug problems in your own code. Many, many vendors ship unoptimized (or
minimally optimized) code because it works better and is easier to
test/debug. There are few programs that are so performance-critical that
the risk of -O3 and -fomit-frame-pointer is worth the gain.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
--
Posted via a free Usenet account from http://www.teranews.com

Jun 13 '06 #5
Thanks everybody. It was helpful.

Actually I had implemented an algorithm and when I compile my proram
with -O it is much faster whereas when I compile the code of my
comparators with -O it doesn't have any effect on their running time.
What may be the reason?
Is it fair to compare the running time using optimization?
As suggested, optimization may lead to wrong output so if this happen
does this mean that their was something wrong in implementation ? or
wat?

Thanks again for your help.

Aamir

Jun 13 '06 #6

aa*********@gmail.com wrote:
Thanks everybody. It was helpful.

Actually I had implemented an algorithm and when I compile my proram
with -O it is much faster whereas when I compile the code of my
comparators with -O it doesn't have any effect on their running time.
What may be the reason?
I don't think you understand, even superficially what a compiler is.
That is your first problem. You don't need to be writing the next GCC
to know the jist of optimization.
Is it fair to compare the running time using optimization?
Usually. Could also compare size.
As suggested, optimization may lead to wrong output so if this happen
does this mean that their was something wrong in implementation ? or
wat?


9 times out of 10 it means your program is broken and that you have
something that exploits it [like variables on the stack...]

Tom

Jun 13 '06 #7
<aa*********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Thanks everybody. It was helpful.

Actually I had implemented an algorithm and when I compile my proram
with -O it is much faster whereas when I compile the code of my
comparators with -O it doesn't have any effect on their running time.
What may be the reason?
That is a bit strange; I've never run across code that doesn't improve _at
all_ when optimized, but I'm sure it's possible.

If one writes the most obvious (and correct) code to solve a problem, it
will likely run slowly without optimization, but when optimization is
enabled the compiler should be able to make it go much faster. If one
writes very tight, slightly incorrect code, the compiler may not be able to
speed it up without breaking it, and so it can't help.

As the saying goes, "it's a lot easier to make correct code fast than it is
to make fast code correct."
Is it fair to compare the running time using optimization?
Depends why you're comparing. In most cases, software is compared with the
highest optimization level that results in correct code.
As suggested, optimization may lead to wrong output so if this happen
does this mean that their was something wrong in implementation ? or
wat?


It depends. If the code is correct, odds are wrong output is caused by a
compiler bug; however, unless one is a very good coder, there's a much
higher chance that the code was not correct in the first place and the more
aggressive optimization levels merely exposed bugs that were there all
along.

If you don't know how to determine which of those applies to your code,
assume that the bug is yours, not the compiler's.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
--
Posted via a free Usenet account from http://www.teranews.com

Jun 13 '06 #8
Stephen Sprunk wrote:
"SuperKoko" <ta*******@yahoo.fr> wrote in message
.... snip ...
-O is the "optimization compiler flag".
With that... the compilation time is a bit longer, but executables
are smaller and run much faster.
-O2 gives even better optimizations.
-O3 gives yet even better optimizations, but executables greater
(in kilobytes) than with -O2.

When releasing a program you should always compile it with -O2
or -O3 (and probably -fomit-frame-pointer).

Provided you test it thusly. You can also use -Os to optimize code
size.

Unless, of course, your program no longer gives correct output
with those optimization levels due to compiler bugs, or you care
about the ability to debug problems in your own code. Many, many
vendors ship unoptimized (or minimally optimized) code because it
works better and is easier to test/debug. There are few programs
that are so performance-critical that the risk of -O3 and
-fomit-frame-pointer is worth the gain.


When something fails under higher optimization levels it is often
an indication of faulty coding triggering undefined or
implementation defined behaviour. This is more likely than a
compiler bug, although the extra compilation effort increases the
likelihood of exposing a compiler insect.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Jun 13 '06 #9
aa*********@gmail.com wrote:

Actually I had implemented an algorithm and when I compile my proram
with -O it is much faster whereas when I compile the code of my
comparators with -O it doesn't have any effect on their running time.
What may be the reason?
Is it fair to compare the running time using optimization?
As suggested, optimization may lead to wrong output so if this happen
does this mean that their was something wrong in implementation ? or
wat?


You should always include context so that your article can stand by
itself. If you are using a google access that has not fixed the
reply bug see below for means to rectify that. Remember, google is
NOT usenet, it is only a flawed interface to usenet.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Jun 13 '06 #10
"CBFalconer" <cb********@yahoo.com> wrote in message
news:44***************@yahoo.com...
Stephen Sprunk wrote:
Unless, of course, your program no longer gives correct output
with those optimization levels due to compiler bugs, or you care
about the ability to debug problems in your own code. Many, many
vendors ship unoptimized (or minimally optimized) code because it
works better and is easier to test/debug. There are few programs
that are so performance-critical that the risk of -O3 and
-fomit-frame-pointer is worth the gain.


When something fails under higher optimization levels it is often
an indication of faulty coding triggering undefined or
implementation defined behaviour. This is more likely than a
compiler bug, although the extra compilation effort increases the
likelihood of exposing a compiler insect.


And I did say that in a later post. In ten years of working with GCC, I've
found exactly one bug in -O3, and it was already fixed in a more recent
version. However, I'm a hobbyist and not a professional coder; YMMV.

The real issue is that higher optimization makes debugging _your_ code much
more difficult, and -fomit-frame-pointer can make it tough to figure out
where to look. If -O3 exposes more of my bugs, that's great, but excessive
optimization may make it impossible for me to isolate them. One of the
tribulations of optimization is that even including test code to find bugs
may change the resulting code so that the bug no longer happens, and
stepping through the code in a debugger is mind-boggling because it no
longer remotely resembles what you wrote.

This is why, as I said, many commercial software vendors simply don't use
high (or any) optimization on what they ship. (Plus they don't pay the cost
of users needing faster machines to run unoptimized code.)

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
--
Posted via a free Usenet account from http://www.teranews.com

Jun 13 '06 #11
CBFalconer wrote:
When releasing a program you should always compile it with -O2
or -O3 (and probably -fomit-frame-pointer).


Provided you test it thusly. You can also use -Os to optimize code
size.


Splitting hairs [I'm sorry, I know *you* know but for the rest of the
audience...]

-Os doesn't mean "optimize for size" it means "perform optimizations
not likely to increase the size" so things like GCSE, strength
reduction, other inductions. On the other hand, it won't do things
like loop unrolling or function inlining.

It won't modularize your code [at least not that I know of] so if you
have a common body of code repeated literally throughout the code -Os
won't help.

Generally -Os and -O2 produce similar footprints on x86_32 platforms
from what I've seen.

Also on some code snippets -O3 can be SLOWER than -O2. So it always
pays to be able to benchmark what you are doing.

Tom

Jun 13 '06 #12
Stephen Sprunk wrote:
And I did say that in a later post. In ten years of working with GCC, I've
found exactly one bug in -O3, and it was already fixed in a more recent
version. However, I'm a hobbyist and not a professional coder; YMMV.
Anyone who has really worked GCC has found at least one bug or two.
[mmm my precious bugs...]. hehehehe.

-O3 on x86 is generally fairly stable in the 3.4 and as far as I can
tell 4.1 trees. 3.2 and 3.0 sucked for me and 3.3 is a bit flaky.
The real issue is that higher optimization makes debugging _your_ code much
more difficult, and -fomit-frame-pointer can make it tough to figure out
where to look. If -O3 exposes more of my bugs, that's great, but excessive
optimization may make it impossible for me to isolate them. One of the
tribulations of optimization is that even including test code to find bugs
may change the resulting code so that the bug no longer happens, and
stepping through the code in a debugger is mind-boggling because it no
longer remotely resembles what you wrote.
Usually it can still tell at least what access cause the problem [e.g.
segfaults]. You're right though. It makes stepping the code
impossible and stack traces impossible to form.
This is why, as I said, many commercial software vendors simply don't use
high (or any) optimization on what they ship. (Plus they don't pay the cost
of users needing faster machines to run unoptimized code.)


Wrong though. Many commercial vendors tune down the optimizations
because their buggy code won't run at higher levels.

I've had numerous bugs in my LT projects traceable to stack overflows
that would only show up at -O2 or higher. So my -O0 debug builds would
run fine and the -O3 would fail.

Of course I don't ship code that can't build in O2, Os and O3. But the
folk I work with routinely have makefiles hacked with "use -O0 for this
file" to get around bugs in their code, the compiler or both. It has
nothing to do with debugging because they have -g3 builds for that.

Tom

Jun 13 '06 #13
CBFalconer <cb********@yahoo.com> writes:
[...]
When something fails under higher optimization levels it is often
an indication of faulty coding triggering undefined or
implementation defined behaviour. This is more likely than a
compiler bug, although the extra compilation effort increases the
likelihood of exposing a compiler insect.


<QUIBBLE>
I think it's more likely to be undefined behavior than
implementation-defined behavior. If implementation-defined behavior
(which the implementation must document) changes with the optimization
level, it's probably a bug in the compiler -- unless the compiler's
documentation says that the behavior changes with optimization level.
</QUIBBLE>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 13 '06 #14
to********@gmail.com wrote:
Stephen Sprunk wrote:
.... snip ...
This is why, as I said, many commercial software vendors simply
don't use high (or any) optimization on what they ship. (Plus
they don't pay the cost of users needing faster machines to run
unoptimized code.)


Wrong though. Many commercial vendors tune down the optimizations
because their buggy code won't run at higher levels.

I've had numerous bugs in my LT projects traceable to stack
overflows that would only show up at -O2 or higher. So my -O0
debug builds would run fine and the -O3 would fail.

Of course I don't ship code that can't build in O2, Os and O3.
But the folk I work with routinely have makefiles hacked with
"use -O0 for this file" to get around bugs in their code, the
compiler or both. It has nothing to do with debugging
because they have -g3 builds for that.


Congratulations. You have matured greatly in the past two years
and no longer generate an urge to plonk. This is partly for the
benefit of those who still have you in their plonk files.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Jun 13 '06 #15
CBFalconer wrote:
Of course I don't ship code that can't build in O2, Os and O3.
But the folk I work with routinely have makefiles hacked with
"use -O0 for this file" to get around bugs in their code, the
compiler or both. It has nothing to do with debugging
because they have -g3 builds for that.


Congratulations. You have matured greatly in the past two years
and no longer generate an urge to plonk. This is partly for the
benefit of those who still have you in their plonk files.


I haven't matured, I just haven't pissed you off lately.

I didn't do jack squat then for the approval of others [re: other
people suck] and I still don't.

It just happens my exploits are exploitable [e.g. my LT projects] so
people put up with the shit I pull. Which is good for me cuz it's fun
and gives me something to do other than watch day time television.

Tom, back in your plonkfile I go I suspect :-)

Jun 13 '06 #16

Stephen Sprunk wrote:
"SuperKoko" <ta*******@yahoo.fr> wrote in message
When releasing a program you should always compile it with -O2 or -O3
(and probably -fomit-frame-pointer).


Unless, of course, your program no longer gives correct output with those
optimization levels due to compiler bugs, or you care about the ability to
debug problems in your own code. Many, many vendors ship unoptimized (or
minimally optimized) code because it works better and is easier to
test/debug. There are few programs that are so performance-critical that
the risk of -O3 and -fomit-frame-pointer is worth the gain.

Or a progam bug due to the fact that the programmer doesn't know well
the whole ISO/IEC 14882:2003 standard. ;)
Things like converting an "unsigned*" to a "unsigned short*" and using
the "unsigned short*" to access the data which contains unsigned
integers... With no-aliasing optimizations, it might behave differently
than the programmer expected (and don't say that the compiler is buggy
here).

Jun 14 '06 #17

aa*********@gmail.com wrote:
Thanks everybody. It was helpful.

Is it fair to compare the running time using optimization?
As suggested, optimization may lead to wrong output so if this happen
does this mean that their was something wrong in implementation ? or
wat?

Except if you intend to release your program without optimizations, it
is very unfair to compare speed without optimizations.
An optimizations level of 1 is the minimum if you want to compare the
real speed that the program should have with a normal compiler (there
are even compilers such as Borland C++ which always write a code with
optimizations, even if the compiler is not a good optimizer).

IMHO, it is better to use a very stable, perfectly debugged, old
version of a compiler, and compile with optimizations, than using the
latest version, and compile without optimizations.
GCC without any optimization produce the worst code I have ever seen
for a C++ compiler (the intent is that the code behaves exactly as a
naive programmer expects).
Even Turbo C++ 1 gives much better results.

Jun 14 '06 #18

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

Similar topics

6
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with...
4
by: bryan | last post by:
I'm still using Beta 2 of VS2005. I'd like to be able to precompile my web site (similar to 1.x) so that I don't have to put source files on the deployment server. Is there anyway to do this other...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
7
by: Sverker Nilsson | last post by:
I have been informed that Guppy-PE (http://guppy-pe.sourceforge.net) has failed to compile its extension modules with a Microsoft .NET 2003 compiler under Windows 2000. One of the problems,...
22
by: Vijay | last post by:
With the option strict On set..... Dim fs As FileStream = File.OpenRead(strFile) With fs Dim buffer(fs.Length) As Byte ' <--- Option Strict On disallows implicit conversions from 'Long' to...
8
by: WebSnozz | last post by:
I have an application written in C that does a lot of low level stuff. It does a lot of things like casting from void*'s. I want to create a new GUI for it in either C# or MC++, but reuse the...
6
by: alf | last post by:
Hi, I want to add some library but it can not be comipled? Here is an output: D:\>cl Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86 Copyright (C) Microsoft...
8
by: Gilles Ganault | last post by:
Hello I need to compile PHP5 with SQLite statically and without PDO. I don't need DB-independence, so it's fine using sqlite_*() functions instead of PDO. 1. I've downloaded, compiled, and...
63
by: Kapteyn's Star | last post by:
Hi newsgroup The program here given is refused by GCC with a error i cannot understand. It says rnd00.c: In function ‘main’: rnd00.c:26: error: expected expression before ‘]’ token ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.