Thread: question on optimization

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    question on optimization

    as far as i know, a c++ compiler inputs c++ code, optimizes, converts it to assembly code, optimizes again, and then assembles into opcodes.

    how does optimization work? here's some code:
    Code:
    a = b;
    c = b;
    b = a;  //swaps b and c
    
    b = b + 1;
    in assembly it's probably something close to:
    Code:
    xchg ebx, ecx;
    inc ebx;
    does the optimizer just search for pre-defined patterns of logic? does it search for any kind of redundancy? how effective are the optimizations?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    i always thought that the compiler outputted optimized code .... also, theres one little state that you forgot, the code is turned into and object once compiled. then its linked.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    let me rephrase:
    when optimizations are done, does the compiler search for redundancies of any kind, or just look for patterns of predefined redundant logic?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    maybe a combination of both ...?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The compiler optimizes the code AFTER it has been converted to assembly. And the method/degree of optimization depends on the compiler, so it's hard to define what/how the optimization is being performed. I always turn off optimization for the simple fact that I would rather do it myself - hey, if I write ****ty/slow code, that's my fault! See, if the compiler optimizes erroneously (ie: inside a thread, etc) then you might have some mysterious bugs on your hands! Nonetheless, the 'volatile' keyword is there to protect against these things from happening to optimized code (usually...)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM
  4. Optimization Question
    By saxman in forum C Programming
    Replies: 7
    Last Post: 06-30-2004, 10:49 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM