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

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

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

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

    Emphasis on the may. And, yes, I could see it as a possibility - after all, they can unroll loops if it's more efficient. Don't forget, optimisers have access to compiler implementation details that we don't.

    Basically, my advice is: don't second-guess the optimiser; it's smarter than you.
    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


  2. #17
    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

    Quote Originally Posted by Mybowlcut View Post
    Are you serious? Are they really that intelligent?
    Without a doubt. Of all of the developers I know, there has been exactly ONE who could consistantly write code that outperformed the optimizations performed by the compiler (David Rainer). He is was the only person I have ever met who could keep track of Pentium Pipleline states, L!/L2 caching all while writing code.

    As a side note, this is one of the big advantages in .NET. With C++ the optimizer is part of the compiler and only has access to the implementations available in the current coimpilation unit. While there are some "whole program optimizers", with .NET this becomes trivial, since ALL of the MSIL is available when the JIT compiler runs. Thius means that it can do such tricks such as cross DLL inlining, multiple level loop unrolling, and more.
    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

  3. #18
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

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

    I had a recent example of an 'obvious' optimisation not turning out to be any use.

    Our code uses FFTs (Fast Fourier Transforms) which involve many repeated sine and cosine calculations. One 'obvious' optimisation is to pre-calculate the values and reuse them by using a lookup table.

    Except that on timing the two methods there was NO DISCERNIBLE DIFFERENCE! Calculating a sine or cosine in a Core 2 Duo was no slower than using a lookup table!

    To quote an old saying, "The proof of the pudding is in the eating".
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #19
    Join Date
    Nov 2003
    Posts
    1,405

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

    Quote Originally Posted by laserlight View Post
    How would you find the performance bottle neck without measurement?
    There's nothing wrong with using measurements to find bottlenecks in a program.

    What I find to be a bad idea is to use measurements to choose between programming constructs while laying down code. This appearantly is what the OP plans to do. It's much better to use qualitative criteria. There's often a best choise which often also is the fastest.

    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.
    It's my experience that often when a performance bottleneck has been found a more substantial change is required to remove it. (more substantial than some local tweaking).
    Last edited by _uj; January 11th, 2009 at 03:39 PM.

Page 2 of 2 FirstFirst 12

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)