Function return value

This is a discussion on Function return value within the C Programming forums, part of the General Programming Boards category; Hello! It seems I have some misunderstanding about some aspects of C programming or there are some compiler issues. I ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    Function return value

    Hello!

    It seems I have some misunderstanding about some aspects of C programming or there are some compiler issues. I have a function:

    Code:
    double myFunction1( myData *data )
    {
      double result1;
      /* a lot of computations here */
      ...
    
      double totalResult = result1;
    
      return totalResult;
    }
    I call this function in my program a lot of times. Let's say it takes 10 minutes to run the program. Now I had to make my function more complicated so I added another part and checked how fast it is. While I was testing for speed I did not added result2 to the totalResult. I was just interested how much new computations take in terms of execution time. I found out. That my program runs 15 minutes.

    Code:
    double myFunction2( myData *data )
    {
      double result1;
      /* a lot of computations here !!! */
      ...
    
      double result2;
      /* a lot of computations here !!! */
      ...
    
      double totalResult = result1;
    
      return totalResult;
    }
    Then, I wrote a final version:

    Code:
    double myFunction3( myData *data )
    {
      double result1;
      /* a lot of computations here !!! */
      ...
    
      double result2;
      /* a lot of computations here !!! */
      ...
    
      double totalResult = result1 + result2; /!!!!
    
      return totalResult;
    }
    ...and I found out that program runs now 30 minutes!!! So the difference in execution time between myFunction2() and myFunction3() is two fold.

    Why is it like this? Might it be that if I don't use result2 the compiler somehow builds executable differently? How I can improve performance in such situation?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is very unlikely that introduction of a single extra variable will noticably affect the time it takes to do some calculations - in fact, it often HELPS to have a set of different variables for the compiler to sort out what's going on - but there's no firm and fast rules here, it depends on the compiler and sometimes also which compiler settings you use.

    It may of course simply be that if you don't use result2, the compiler can optimize away most of your result2 calculations [perhaps even all of it], but once you add result2 to the totalresult, it must perform the calculations - this is a quite common problem when doing benchmark calculations - the compiler realizes that we're doing this 1000 calculations that all have no effect on the final result, so it calculates it once, and removes the now empty loop, and you think "Wow, I can do 1000 of these calculations in no time at all, that's impressive". You then try to fit this into your actual code where you DO use the result, and it takes much longer...

    Obviously, all of this is speculation, as I don't actually know what your code does.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Thanks Mats! It seems that I have the situation that you described in the second paragraph. I added a lot of distance calculations (two nested loops with distance calculations on each steps). Seems that a compiler just threw it away as it didn't go to the final result. I was so happy that my function still runs fast after adding extra computations, but once I wrote a final version everything was ruined - the execution time is not acceptable now.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler are you using, and what switches are you giving it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    I am using GCC:
    Code:
    $ gcc --version
    i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
    I run it like:
    Code:
    $ gcc -O2 prog.c -o prog.exe
    I tried -O3 and did not see any improvements.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could try adding -ffast-math - it means that some math operations are "fast instead of 100% IEEE-754 compliant". This helps quite a bit if you do for example square-root or trigonometry. Some other operations are also a bit faster.

    Edit: If you are, however "pushing the envelope" within your math calculations, e.g. using really huge inputs to sin/cos, you may find that you get a different result - but for general, basic, calculations, it will give better performance. Just check your results vs. the non-fast version with a wide range of inputs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Oh, Mats! Why didn't I ask you before!!! :-)

    I can't believe it. Without -ffast-math a single runs takes 1345 seconds, with this flag - just 218 seconds - 6-fold difference!!! I have to do millions of this runs so speed is a key issue for me. I don't do a lot of sin(),cos(), etc. but I have to calculate a lot of sqrt(), exp(), log() and standard operation (+/-/*//). At least in these two runs the programs converges to the same result.

    Thanks a lot!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's because with fast-math, the code is a single sqrt instruction [takes a few dozen cycles], but the IEEE compliant sqrt is a function call, which not only takes extra time because of the call to the function itself, but it also prevents the compiler from making some optimizations. So whilst I wouldn't have predicted a 6x performance boost, I thought it may improve "quite a bit".

    And, I've been there and done that myself - whishing I had someone to ask that could give me the option. I think I actually found it myself in the gcc man-page.

    You may now try to use -O3 to see if it makes it any better too. Perhaps it doesn't, but it's worth a try.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    I am reading the Optimization section of the GCC man page a as maniac :-)

    The -O3 option didn't improve still. It even made calculations a bit slower, 216 (O2) vs. 227 (O3) seconds. Anyway, I am more than happy now because I don't have break my head thinking about algorithmic optimization.

    Cheers!!!

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I know. The -ffast-math isn't in the optimization section.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21