From 545ddb70a8d3df46c936e238e4b1fc1a11a3ca80 Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Wed, 22 Feb 2017 18:16:51 +0100
Subject: [PATCH] Replacing map with unordered_map in stopwatch

This should make the code slightly fast.
Notice that it is now necessary to explicitly sort the watches
when printing the log.
---
 src/tools/Stopwatch.cpp | 12 +++++++++---
 src/tools/Stopwatch.h   |  4 ++--
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/tools/Stopwatch.cpp b/src/tools/Stopwatch.cpp
index 344c71914..67d8f6127 100644
--- a/src/tools/Stopwatch.cpp
+++ b/src/tools/Stopwatch.cpp
@@ -25,6 +25,8 @@
 
 #include <cstdio>
 #include <iostream>
+#include <vector>
+#include <algorithm>
 
 /*
 Different clocks can be used
@@ -147,9 +149,13 @@ std::ostream& Stopwatch::log(std::ostream&os)const{
   buffer[0]=0;
   for(unsigned i=0;i<40;i++) os<<" ";
   os<<"      Cycles        Total      Average      Minumum      Maximum\n";
-  for(map<string,Watch>::const_iterator it=watches.begin();it!=watches.end();++it){
-    const Watch&t((*it).second);
-    std::string name((*it).first);
+
+  std::vector<std::string> names;
+  for(const auto & it : watches) names.push_back(it.first);
+  std::sort(names.begin(),names.end());
+
+  for(const auto & name : names){
+    const Watch&t(watches.find(name)->second);
     os<<name;
     for(unsigned i=name.length();i<40;i++) os<<" ";
     std::sprintf(buffer,"%12u %12.6f %12.6f %12.6f %12.6f\n", t.cycles, double(t.total), double(t.total/t.cycles), double(t.min),double(t.max));
diff --git a/src/tools/Stopwatch.h b/src/tools/Stopwatch.h
index 611cc8d12..f49047c1a 100644
--- a/src/tools/Stopwatch.h
+++ b/src/tools/Stopwatch.h
@@ -23,7 +23,7 @@
 #define __PLUMED_tools_Stopwatch_h
 
 #include <string>
-#include <map>
+#include <unordered_map>
 #include <iosfwd>
 
 namespace PLMD{
@@ -126,7 +126,7 @@ class Stopwatch{
     void stop();
     void pause();
   };
-  std::map<std::string,Watch> watches;
+  std::unordered_map<std::string,Watch> watches;
   std::ostream& log(std::ostream&)const;
 public:
 /// Start timer named "name"
-- 
GitLab