compilation parameters, code optimization


 
Thread Tools Search this Thread
Top Forums Programming compilation parameters, code optimization
# 1  
Old 08-15-2008
compilation parameters, code optimization

Hi all,

I implemented an application, through using c++ and compiled it with g++.
At first, what I did is (@ compilation):
g++ calcBacon.C -o test -DDEBUG

after I ran my application it took almost 120 sec. to finish its execution

when I compiled with optimization parameters, execution time dramatically changed
what I did was:
g++ calcBacon.C -o test -O3 -DDEBUG

after this compilation overall execution took 35 sec. to finish.
It is a big difference (~85secs)...

My questions:
1) Is it related with my implementation? I mean, is my implementation so bad, therefore compiler optimization responds very good to my code? (is this the explanation?).
2) Do you know any other optimization parameter like this? Could you please explain in what cases they work good?

Thanks in advance
# 2  
Old 08-15-2008
1. The fact that the compiler was able to optimise your code well does not mean that it was badly written, just that the optimiser is doing its job. If code has been written in a way that helps the optimiser do its job, then that's good, but "hand-optimised" code tends to become unmaintainable gibberish. Concentrate more on finding better algorithms and writing clear code.

2. GCC, like any decent compiler, has dozens of individual optimisation flags; they are fully described in the manual. O3, O2 and so on are short-hand ways of turning a whole bunch of them on. You can experiment with fine-tuning the optimisation if you need to or are curious, but this can be time consuming, so you had better be sure it's worth the effort. Most people just switch on -O2 or O3 and leave it at that.
# 3  
Old 08-15-2008
Thanks for the answers..

I also wonder if optimized codes are stable. Can they produce unexpected outputs or failures?

According to below article, they might cause some unexpected errors but it is old (2003):
http://www.linuxfromscratch.org/hint...timization.txt
# 4  
Old 08-15-2008
spirtle gave you geat advice - usually 90% of bottlenecks are caused by algorithm choice. Some others unavoidable...

Do you know how to profile code?
compile
Code:
g++ -g -p mycode.cpp -o test

Run the code, then try
Code:
gprof test

This will show the time spent in each function - so you can locate the botteleneck.
Read the info page or man page for gprof.

By the way - it appears like you are compiling C code with a c++ compiler? Is that the case? If so, use gcc, not g++
# 5  
Old 08-26-2008
I was implementing a C++ code. I don't know why but the guys who wrote in C++ here prefer .C (capital letter) instead of .cpp.

By the way, I tried to profile my code as you mentioned, however I couldn't make it. I think if the application gets some parameters from outside (like ./myexecutable file.txt) then it fails.

Thanks for the information though, I really appreciate that.

System: Ubuntu Hardy
# 6  
Old 09-04-2008
Linux statistical profilers

It looks like recompiling the application with -p probably broke it. IMO, profiling with gprof is almost always a last resort as it is so intrusive that it may skew your profile.

This may be an unwanted advert, but you can use the statistical profiler I am working on now - Zoom. It does calltrees and side by side source and asm, which I believe are huge value-adds.

If you're only want open source solutions, search for oprofile and sysprof.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Code optimization

Hi all I wrote below code: #!/bin/sh R='\033 do you have any idea how to optimize my code ? (to make it shorter eg.) (11 Replies)
Discussion started by: primo102
11 Replies

2. Shell Programming and Scripting

Source code compilation

Need assistance in Source code compilation . When installing a software compiling a source code . Whatever the output that prints on the screen i want to log it into a file. How can i see output and store the output to file ./configure make make install Is there other way of seeing output... (5 Replies)
Discussion started by: ajayram_arya
5 Replies

3. Shell Programming and Scripting

Shell Script passing parameters to sqlplus code

Hello All, I am interested in finding out a way to pass parameters that are entered at the prompt from HP unix and passed to SQLPlus code with a Shell Script. Is this possible? Thanks (4 Replies)
Discussion started by: compprog11
4 Replies

4. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

5. Programming

Compilation error when compiling Pro*C code

I'm running a query similar to the one that I'm describing below -: _______________________________ EXEC SQL INSERT INTO TABLE1 ( C1 ,C2 ,C3 ,C4 ) (SELECT DISTINCT B.V1 ,B.V2 ,( SELECT D.V3 FROM TABLE2 D WHERE D.V3 = C.V4) ,B.V4 FROM TABLE2 B ,TABLE3 C WHERE B.V3 = C.V4) ;... (1 Reply)
Discussion started by: maheshp
1 Replies

6. Programming

Program exited with code 01: does it indicate unsuccessful compilation?

(gdb) r --------------------- enter Breakpoint 1, 0x0000000000409d40 in main () (gdb) n Single stepping until exit from function main, which has no line number information. Find_Cmd_Option: found option no. 2: seed (s) Find_Cmd_Option: found option no. 5: dfile (c) Initial no. div... (1 Reply)
Discussion started by: cdbug
1 Replies

7. Programming

Re : Pass parameters from Cron job to Java code

Hello All, Hope all is fine. I am newbie to Unix. I am using Bourne Shell (sh). One of the question I have is that I am trying to read XML file and based on reading that XML file I want to run same java programs at different hours. When I run the Java code, I wanted to pass parameters to my... (1 Reply)
Discussion started by: samshaw
1 Replies

8. HP-UX

HP-UX 64 compilation causing some code to seg fault

Hello everyone, Today we are attempting to port some legacy C code to a 64 HP-UX machine at my company and there is kind of a strange error we ran into. there is a small function they have defined called zgetenv that accepts a char* and basically just does some null checking and returns ... (0 Replies)
Discussion started by: khadesh
0 Replies

9. UNIX for Dummies Questions & Answers

Help on optimization of the script

Hi, I have prepared script which is taking more time to process. find below script and help me with fast optimized script:- cat name.txt | while read line do name=$(echo $line| awk '{print $8}') MatchRecord=$(grep $name abc.txt | grep -v grep ) echo "$line | $MatchRecord" | awk... (2 Replies)
Discussion started by: aju_kup
2 Replies
Login or Register to Ask a Question