QMap - use hint on insert in QMap::toStdMap
authorThorbjørn Lund Martsum <tmartsum@gmail.com>
Mon, 1 Oct 2012 04:42:28 +0000 (06:42 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Sat, 27 Oct 2012 05:35:48 +0000 (07:35 +0200)
Giving the std-map a hint (normally) improves insert performance.
There seems to be no reason not to provide this hint.

Change-Id: I4344607ebf54574a3ae9666d87a41a3c14762361
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/corelib/tools/qmap.h
tests/benchmarks/corelib/tools/qmap/main.cpp

index 642cf82..0e726e8 100644 (file)
@@ -946,7 +946,7 @@ Q_OUTOFLINE_TEMPLATE std::map<Key, T> QMap<Key, T>::toStdMap() const
     const_iterator it = end();
     while (it != begin()) {
         --it;
-        map.insert(std::pair<Key, T>(it.key(), it.value()));
+        map.insert(map.begin(), std::pair<Key, T>(it.key(), it.value()));
     }
     return map;
 }
index 05b52ba..c3b9c18 100644 (file)
@@ -60,6 +60,7 @@ private slots:
     void lookup_string_int();
 
     void iteration();
+    void toStdMap();
 };
 
 
@@ -159,6 +160,17 @@ void tst_QMap::iteration()
     }
 }
 
+void tst_QMap::toStdMap()
+{
+    QMap<int, int> map;
+    for (int i = 0; i < 100000; ++i)
+        map.insert(i, i);
+
+    QBENCHMARK {
+        std::map<int, int> n = map.toStdMap();
+        n.begin();
+    }
+}
 
 QTEST_MAIN(tst_QMap)