tags:

views:

216

answers:

5

I can examine the optimization using profiler, size of the executable file and time to take for the execution.

I can get the result of the optimization. But I have these questions,

  • How to get the optimized C code.
  • Which algorithm or method used by C to optimize a code.

Thanks in advance.

+5  A: 

Usually the code isn't optimized as C. Usually optimization passes are done long after the C has been converted into some form of intermediate representation that is easier for a compiler to work with in memory. Therefore, a direct answer to your question is that the optimized C code never exists.

Billy ONeal
yeah I can get the assembler code by using objdump -s command.and we can produce assembly code by using cc -S
sganesh
But If I know How it is doing optimization I can try to implement my own optimizing
sganesh
Optimizing the actual C is pretty much not possible to do in a meaningful way. Most types of static analysis algorithms are designed to work on some form of "three address" code already translated by a compiler. Algorithms are easier to write when the data they operate on is simpler. Start with the "dragon book" and work you way up from there. ( http://en.wikipedia.org/wiki/Dragon_Book_(computer_science) )
Billy ONeal
+5  A: 

The C compiler generally does not produce optimized C and compile that; it generally produces optimized assembly language or machine code.

The closest you can get is probably to compile a file to assembly with no optimization and again with highest optimization, and then compare the assembly output. You will have to have a good grasp of assembly language to do that. If you are using gcc, read about the -S and -O switches for how to do (or not do) this.

Let me clarify some things about the compiler. The algorithms and data structures used by the code you wrote will never be changed by the compiler; the compiler will only be doing things that are exactly equivalent to your C code. That's why it wouldn't usually make sense for the compiler to produce optimized C.

If your goal is to write faster code, then, your best bet is to write better C by using better algorithms and data structures at the C level by carefully using the profiler.

If your goal is just to understand optimization, try Program Optimization and Compiler Optimization on Wikipedia for some general information.

Jesse Millikan
+1 for examination of assembler.
Billy ONeal
I am using gcc only. I know about -S and -O1 ,2,3 levelsBut I want to know the method to optimize a code.Then only I can try my own optimization
sganesh
@sganesh: Then the question should be: How can I get started writing a code optimizer? Trying to reverse-engineer what other compilers are doing won't help you. If you want to do that, just look at their source code.
Billy ONeal
yeah. But before that If I know answers for those questions it will be easy for me.
sganesh
No, it won't... this just isn't easy. Reading the assembler with and without optimisation (and even individual optimisations) is the most help you'll get.
Andrew McGregor
A: 

if you understand assembler, you can inspect the assembler generated code by compiler.

uray
True -- but this really has nothing to do with the OP's question... not to mention parrots Jessie Mikkikan's answer that was 4 minutes before yours...
Billy ONeal
+5  A: 

you can get an idea of optimization using the option -fdump-tree-optimized with gcc . and you'll get an optimised file. you cannot run the code but using that you can get an idea of optimization . dont forget to include -O2 or -O3 or some other level.

abubacker
+1  A: 

If you're using GCC, use an argument to optimize the code and use --save-temps as an argument. Everyone saying C code isn't optimized as C when compiling with GCC is wrong to an extent. Write a recursive Fibonacci sequence generator in C, and read through the preprocessed code. The aforementioned argument also saves the generated assembly in the directory GCC is called from. If you're more comfortable with Intel-syntax assembly, use -masm=intel as an argument as well.

Anonymous
In gcc, --save-temps is used to store the preprocessed file. using -E we can see that file. But the --save-temps will store thisfile permanently.
sganesh