CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: [RESOLVED] Faster to construct many times or construct once and assign many times?

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] Faster to construct many times or construct once and assign many times?

    Hey.

    I have a for loop. Inside that for loop I pass a point to a function. The point is made from two integer values that are part of (declared and initialised inside) the for loop. I have the option to either create a point before the loop and then assign its x and y values manually each iteration:
    Code:
    point.x = x;
    point.y = y;
    
    some_func(point);
    or construct a point each iteration from the two values:
    Code:
    some_func(point(x, y));
    Premature optimisation aside, this is a question that I have asked myself many times before and I think it's time I chose the right option instead of wasting energy each time it happens pondering which is faster.

    Thanks!
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Faster to construct many times or construct once and assign many times?

    The only way you're going to get a definitive answer is to put together a test harness and time the two approaches. Never try to second-guess such fine differences.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Faster to construct many times or construct once and assign many times?

    The best thing to do is test it, since the answer to your question is compiler and system dependent. However, that said, on tests that I had done in the past with VS2005 on a P4, char's where certainly faster to construct than assign, and I think the same was also true for int's. I'm not sure about short's (on some systems, despite being two bytes, these are slower than int's). Doubles were faster to assign than construct.

    Again, I stress, that testing is the best option since the answer to your question is compiler and system dependent. But, my general rule (which may be misplaced) is, built in types of 4 bytes or less where the value is different on each pass, I'll construct and initialise inside the loop, but anything else (including user defined types), I'll construct and initialise outside of the loop, and assign within the loop.

    If you are really bothered about which is faster and want an absolute answer though, then profile it, and pick the most appropriate solution.

    I assume that you are passing point to some_func by reference?
    Last edited by PredicateNormative; January 6th, 2009 at 04:52 AM.

  4. #4
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Faster to construct many times or construct once and assign many times?

    Quote Originally Posted by Graham View Post
    The only way you're going to get a definitive answer is to put together a test harness and time the two approaches. Never try to second-guess such fine differences.
    Quote Originally Posted by PredicateNormative View Post
    The best thing to do is test it, since the answer to your question is compiler and system dependent. However, that said, on tests that I had done in the past with VS2005 on a P4, char's where certainly faster to construct than assign, and I think the same was also true for int's. I'm not sure about short's (on some systems, despite being two bytes, these are slower than int's). Doubles were faster to assign than construct.

    Again, I stress, that testing is the best option since the answer to your question is compiler and system dependent. But, my general rule (which may be misplaced) is, built in types of 4 bytes or less where the value is different on each pass, I'll construct and initialise inside the loop, but anything else (including user defined types), I'll construct and initialise outside of the loop, and assign within the loop.

    If you are really bothered about which is faster and want an absolute answer though, then profile it, and pick the most appropriate solution.

    I assume that you are passing point to some_func by reference?
    Ok. Seems I've found my answer. Are there any profilers that you'd recommend that work with Visual Studio? I'd be better off using a proper one than writing my own test code as at least I can use it again.

    In the actual scenario, yes, I do pass point by reference to some_func.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Faster to construct many times or construct once and assign many times?

    I don't know of a free one, but the one I used in my old company to do the tests was DevPartner by Compuware. You can get a free trial version...

    http://www.compuware.com/products/de...rk=200809-5755

  6. #6
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Faster to construct many times or construct once and assign many times?

    Quote Originally Posted by PredicateNormative View Post
    I don't know of a free one, but the one I used in my old company to do the tests was DevPartner by Compuware. You can get a free trial version...

    http://www.compuware.com/products/de...rk=200809-5755
    Cheers.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  7. #7
    Join Date
    Nov 2008
    Location
    Netherlands
    Posts
    77

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    i think its safe to assume that assignment is in most cases faster.
    especially for classes that have dynamic allocation, strings, arrays etc.
    unless it is a small POD.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    This brings to mind Item #18 of Sutter and Alexandrescu's C++ Coding Standards: declare variables as locally as possible, but with an exception that it can sometimes be beneficial to hoist a variable out of a loop. However, their explanation of the exception is that "most of the time that won't obfuscate the code's intent at all, and it can actually help clarify what work is done inside the loop and what calculations are loop-invariant". That explanation does not seem to apply here since the work is indeed done inside the loop, hence "hoisting" the variable is just a matter of optimisation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    It REALL is a case for measurement (or at least careful analysis) and has no "correct" answer i the general case.

    I have even found cases where:
    Code:
    Class basicInstance;
    // Lots of setup for basicInstance
    for (....)
    {
       Class loopInstance = basicInstance;
        // Tweak this from the standard
        ....
    }
    is the fastest.

    I agree with the premis of let "intent" be your initial guiding factor, and only deviate from that if you MEASURE a performance issue (you know my whole speech on that )
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    [QUOTE=Mybowlcut;1798716]
    Quote Originally Posted by Mybowlcut View Post
    Premature optimisation aside,
    The best alternative is,

    some_func(point(x, y));

    It expresses your intent in the shortest and most restrictive way. It provides the compiler with more information about your intent than any other alternative and it thereby creates the most room for optimizations. This should guide your choise, not system dependent measurements.

    If exactly this spot turns out to be a performance bottleneck then chances are that none of these choises will give the fastest code anyway. You most likely will do a rewrite of a larger section of code.
    Last edited by _uj; January 6th, 2009 at 02:02 PM.

  11. #11
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Faster to construct many times or construct once and assign many times?

    Quote Originally Posted by Mybowlcut View Post
    Ok. Seems I've found my answer. Are there any profilers ........
    As I've argued in my previous post. Don't make a choise in cases like this based on system dependent measurements.

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    Quote Originally Posted by _uj
    The best alternative is,

    some_func(point(x, y));

    It expresses your intent in the shortest and most restrictive way.
    I agree. In fact, I would not call it an alternative, but rather the first thing that should be reached for since it follows the principle of declaring variables near first use.

    Quote Originally Posted by _uj
    This should guide your choise, not system dependent measurements.
    How would you find the performance bottle neck without measurement?

    Quote Originally Posted by _uj
    If exactly this spot turns out to be a performance bottleneck then chances are that none of these choises will give the fastest code anyway. You most likely will do a rewrite of a larger section of code.
    Wouldn't that depend on the (relative) cost of construction and assignment? It does seem reasonable to me that "hoisting" the variable out of the loop such that assignment is used instead of construction could be a valid optimisation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Nov 2008
    Location
    Netherlands
    Posts
    77

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    Quote Originally Posted by _uj View Post
    The best alternative is,

    some_func(point(x, y));
    in this case yes, its a POD

    if there is dynamic allocation, then this might not be prefered.

  14. #14
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    Don't forget that the optimiser may well hoist the declaration out of the loop for you (and in a more efficient way than you could achieve manually), so the point could well be moot.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  15. #15
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: [RESOLVED] Faster to construct many times or construct once and assign many times

    Quote Originally Posted by Graham View Post
    Don't forget that the optimiser may well hoist the declaration out of the loop for you (and in a more efficient way than you could achieve manually), so the point could well be moot.
    Are you serious? Are they really that intelligent?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)