[PATCH] Exploit move semantics in std::accumulate.

Christopher Jefferson chris@bubblescope.net
Sat Jan 8 18:05:00 GMT 2011


On 8 Jan 2011, at 17:27, Eelis van der Weegen wrote:

> The attached simple patch reduces the runtime of the following program
> from 1.4 seconds to 0.005 seconds on my machine:
> 
>  #include <iostream>
>  #include <string>
>  #include <numeric>
>  #include <vector>
> 
>  int main()
>  {
>    std::vector<std::string> v;
>    for(int i = 0; i != 20000; ++i)
>      v.push_back(std::string(50, 'a'+(i%10)));
> 
>    std::cout << std::accumulate(v.begin(), v.end(),
>      std::string()).size() << '\n';
>  }

Your patch is a really nice idea. I'm not 100% sure it is allowed by the C++0x standard (although, I can't imagine any valid code which would possibly break it).

Chris



More information about the Libstdc++ mailing list