This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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';
  }

Regards,

Eelis
>From 3176b9c351aa87b2b5e786a303acb59d99eaec58 Mon Sep 17 00:00:00 2001
From: Eelis van der Weegen <eelis@eelis.net>
Date: Sat, 8 Jan 2011 18:20:05 +0100
Subject: [PATCH] Exploit move semantics in std::accumulate.

---
 libstdc++-v3/include/bits/stl_numeric.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/stl_numeric.h b/libstdc++-v3/include/bits/stl_numeric.h
index 86ae2cf..61b7d06 100644
--- a/libstdc++-v3/include/bits/stl_numeric.h
+++ b/libstdc++-v3/include/bits/stl_numeric.h
@@ -120,7 +120,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
       __glibcxx_requires_valid_range(__first, __last);
 
       for (; __first != __last; ++__first)
-	__init = __init + *__first;
+	__init = _GLIBCXX_MOVE(__init) + *__first;
       return __init;
     }
 
@@ -147,7 +147,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
       __glibcxx_requires_valid_range(__first, __last);
 
       for (; __first != __last; ++__first)
-	__init = __binary_op(__init, *__first);
+	__init = __binary_op(_GLIBCXX_MOVE(__init), *__first);
       return __init;
     }
 
-- 
1.7.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]