Re: + or += (was: why not use i++ instead of ++i in for()?)

From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 02/24/04

  • Next message: Bill Gates: "How to know the property of constructor function of a class?"
    Date: Tue, 24 Feb 2004 20:03:16 +0100
    
    

    Bo Persson <bop@gmb.dk> wrote:
    > [...]
    >
    > > > > > > s = a + b + c + d;
    >
    > [...]
    >
    > > > > > > s = a;
    > > > > > > s += b;
    > > > > > > s += c;
    > > > > > > s += d;
    >
    > [...]
    >
    > > T operator+(const T& l, const T& r)
    > > {
    > > T tmp(l);
    > > tmp += r; // invoking 'operator+=()' here
    > > return tmp;
    > > }
    > >
    > > This makes it obvious that, regarding
    > > performance, 'operator+()' can never be
    > > better than 'operator+=()', but it will
    > > often be worse.
    >
    > If you are unlucky, this will allocate a buffer and copy l once, then
    > reallocate the buffer and copy l again, before adding r.

      And then it copies again, upon return,
      possibly also re-allocating again.

    > Another implementation looks like this:
    >
    > T operator+(const T& l, const T& r)
    > {
    > T tmp;
    > tmp.reserve(l.size() + r.size());
    >
    > return tmp.append(l).append(r);
    > }
    >
    > A maximum of one memory allocation. Copies each character exactly once.

      Twice. You keep forgetting the return
      value.
      (As a side note, I once had to learn
      that returning the expression might
      confuse the optimizer so that it cannot
      apply the RVO.)

    > [...]
    > > Still, this creates three temporaries,
    > > where Mark's code creates none. And his
    > > code will not trigger any allocations
    > > either.
    > > What is your point?
    >
    > My point is:
    >
    > Best case, three temporaries on stack, but no allocations.

      Plus one assignment.

    > Mark's best case, no temporaries, but s is updated 3 more times.

      3 times plus one assignment, minus three
      temporaries.

    > Worst case, three temporaries allocated, s is reallocated on assignment.
    > Four in total.
    >
    > Mark's worst case, s is reallocated on assignment, and for each append. Four
    > in total.

      Right, in worst case it is as bad as the
      other. Otherwise it's better.

    > s = a + b + c + d;
    >
    > Looks much better. :-)

      Maybe. I use it whenever perfromance is
      not an issue. Generally, this should be
      most of the time. In my case, it is never
      more than 50%. (I usually don't do GUIs.)

    > Bo Persson

      Schobi

    -- 
    SpamTrap@gmx.de is never read
    I'm Schobi at suespammers dot org
    "Sometimes compilers are so much more reasonable than people."
      Scott Meyers
    

  • Next message: Bill Gates: "How to know the property of constructor function of a class?"

    Relevant Pages

    • Re: Problem with linked list
      ... !pool and see what piece of memory has ... allocated (thankfully you tag your allocations). ... > The buffer is filled the following way: ... >> what's in the section of MYSTRUCT (which you don't appear to have ...
      (microsoft.public.development.device.drivers)
    • Re: Fast way to allocate buffer for producer/consumer scenario
      ... queue and triggers another thread to handle the received data. ... What I now did was to restructure this so that instead of copying the buffer ... VirtualAllochowever interoperates with the OS to reserve pages in virtual and physical memory, which in itself presents an overhead. ... However, I think you'll get the biggest benefit by using a "small buffer" optimization, where smaller allocations are held directly in the object and therefore copied rather than moved. ...
      (microsoft.public.vc.language)
    • Re: Multithreaded File Writes
      ... 7K puts you at nearly 2 gigs of allocations when processing all of your files. ... Drop a smaller buffer in place to lower your mem requirements ... Do your profiling first, figure out where the idle time is going, then work on ... > What is the CPU doing when its idle and there is no disk activity. ...
      (microsoft.public.dotnet.framework.performance)
    • Re: User Memory Quotas
      ... The issue is how many winstation desktops are there on your ... adjust design so objects are released at earliest opportunity) ... allocations you have available. ... > buffer" that all desktops are allocated from. ...
      (microsoft.public.windows.server.security)
    • Re: How can I freeze allocation %?
      ... assignment, you are ... >the largest % of effort the resource expended during any ... I assign Mickey Cobb at 50% Units on ... >> allocations showing how an individual should split his ...
      (microsoft.public.project)