Thread: speed/optimization concerns

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    speed/optimization concerns

    Alright, I'll admit, I'm somewhat anal retentive when it comes to optimizing my code and getting it to run fast without using more memory than it must, but I don't see that as a bad thing Anyway, I was wondering if anyone knows which would run faster (excuse the vague code, it's just meant as an example)

    Code:
    int a()
    {
        return(42);
    }
    Now, which b() is faster?

    Code:
    int b()
    {
        int temp;
    
        temp=a();
        c(temp);
    }
    or

    Code:
    int b()
    {
        c(a());
    }
    I'm guessing that the second one is, but I'm not sure, and I don't know exactly how the compiler changes the code around for optimizations and such... thanks.
    Away.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What is c???
    Generally, the second should be faster due to the declaration of a temporary variable. However, to be absolutely sure, compile both then use a disassembler to look at the assembly code.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Here's a disassembly of the C code, the 2nd b() looks faster !
    Code:
               int temp;
               temp=a();
    
    004010B8   call        @ILT+10(a) (0040100f)
    004010BD   mov         dword ptr [ebp-4],eax
    
               c(temp);
    
    004010C0   mov         eax,dword ptr [ebp-4]
    004010C3   push        eax
    004010C4   call        @ILT+15(c) (00401014)
    004010C9   add         esp,4
    Code:
               c(a());
    
    00401108   call        @ILT+10(a) (0040100f)
    0040110D   push        eax
    0040110E   call        @ILT+15(c) (00401014)
    00401113   add         esp,4

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First:

    call - 19 clks
    mov - 9 clks
    mov - 9 clks
    push - 11 clks
    call - 19 clks
    add - 3 clks

    Sum: 70 clks

    Second:

    call - 19 clks
    push - 11 clks
    call - 19 clks
    add - 3 clks

    Sum: 52 clks


    Conclusion:

    The second is ~25% faster than the first.
    The first is ~34% slower than the second.

    PS:
    I was really bored, I know .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Both *might* be of the same speed. Some compiler optimize real well and might put temp into a register and don't allocate it at all. That way, it'll be just as fast. Then there is always the VC++ calling technique: __fastcall. That could change the output assembly a bit.

    Conclusion: It depends on the compiler and the compiler settings.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    It heavily depends on compiler and optimization settings. Also, you should always try to find the bottlenecks of your program first and optimize that part, as a performance boost there might make your program visibly faster while a performance boost somewhere else might be wasted time.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mingw32-g++ size concerns
    By Chris87 in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2008, 03:04 PM
  2. any concerns with static memory in C?
    By caltiger in forum C Programming
    Replies: 6
    Last Post: 05-15-2008, 10:03 AM
  3. At My Wit's End (concerns passing char to constructor)
    By MisterWonderful in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2004, 03:13 AM
  4. memory concerns
    By gustavosserra in forum C++ Programming
    Replies: 11
    Last Post: 05-21-2003, 09:29 PM