Thread: optimization flags

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    optimization flags

    I have a program that uses alot of vectors. I have tried optimizing it at levels -O and -O2. The performance is much better at -O2. But the results the program gives are different. I have a test harness of about 150 tests that I have run berfore and after the optimization. Maybe the optimization is causing a problem with some of the floating point calculations. Does anyone know which flags in particular in -O2 may help improve the performance of the vectors?? I am using g++ version 3.4.2
    Thanks
    Mark
    Last edited by markucd; 06-29-2006 at 04:59 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > But the results the program gives are different.
    That usually means there is a bug (or an assumption) in your code.
    If your program is correct (and your compiler code generator isn't broken, which seems unlikely), then optimisation should not affect the result.

    > Maybe the optimization is causing a problem with some of the floating point calculations
    Quite possibly.
    http://www.daniweb.com/techtalkforum...ad.php?t=45388
    Previously comparable floats may end up being non-comparable due to implicit extra precision.
    Or put another way, if the compiler succeeds in keeping floating point values in floating point registers for longer, then expect some differences.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Post code?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    Quote Originally Posted by Salem
    Or put another way, if the compiler succeeds in keeping floating point values in floating point registers for longer, then expect some differences.
    I really don't see why that would be if the code is written well... Copying from a register to memory would not cause a change... unless the type is coverted to a less precise vairable in the process (e.g. double is operated on and saved as a float which is then operated on with the result returned as a double). I think that you need to have a look through your code very carefully - easy to loose track of precision.

    Another question - are the differences outside the level of precision you require or are they more significant?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I really don't see why that would be if the code is written well
    There's the rub - "if"

    The code in question isn't well written otherwise it would not show up as having different behaviour depending on optimisation flags.

    Maybe the OP should post code which shows the "problem", then perhaps we can give concrete help on how to fix it rather than just pages of waffle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 1
    Last Post: 04-15-2008, 02:27 AM
  2. Cannot get interface flags: Invalid argument
    By nasim751 in forum C Programming
    Replies: 3
    Last Post: 04-14-2008, 02:29 AM
  3. Using 'flags'
    By cyreon in forum C++ Programming
    Replies: 11
    Last Post: 01-28-2008, 04:21 PM
  4. Bit Flags
    By Padawan in forum C Programming
    Replies: 15
    Last Post: 03-30-2004, 10:38 PM
  5. Flags, the ambiguity of
    By Jeremy G in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2003, 11:41 PM