diff options
author | MarcoFalke <falke.marco@gmail.com> | 2017-02-08 04:40:19 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2017-02-08 04:43:13 +0100 |
commit | d304fef3746039183f51b3ac8f4774dcf3a64f59 (patch) | |
tree | 5f9a42f47526e1daa742ac1adeb6617eef37740f | |
parent | b709fe7ffc44ae14bbd2403cdfbfa4fcf94cef6b (diff) | |
parent | 29c53289a9e6bb34a098ea87d923968e3ac6d75d (diff) |
Merge #9712: bench: Fix initialization order in registration
29c5328 bench: Fix initialization order in registration (Wladimir J. van der Laan)
-rw-r--r-- | src/bench/bench.cpp | 16 | ||||
-rw-r--r-- | src/bench/bench.h | 3 |
2 files changed, 10 insertions, 9 deletions
diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index 1bd9d06b80..3c9df4f713 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -9,7 +9,10 @@ #include <iomanip> #include <sys/time.h> -std::map<std::string, benchmark::BenchFunction> benchmark::BenchRunner::benchmarks; +benchmark::BenchRunner::BenchmarkMap &benchmark::BenchRunner::benchmarks() { + static std::map<std::string, benchmark::BenchFunction> benchmarks_map; + return benchmarks_map; +} static double gettimedouble(void) { struct timeval tv; @@ -19,7 +22,7 @@ static double gettimedouble(void) { benchmark::BenchRunner::BenchRunner(std::string name, benchmark::BenchFunction func) { - benchmarks.insert(std::make_pair(name, func)); + benchmarks().insert(std::make_pair(name, func)); } void @@ -29,12 +32,9 @@ benchmark::BenchRunner::RunAll(double elapsedTimeForOne) std::cout << "#Benchmark" << "," << "count" << "," << "min" << "," << "max" << "," << "average" << "," << "min_cycles" << "," << "max_cycles" << "," << "average_cycles" << "\n"; - for (std::map<std::string,benchmark::BenchFunction>::iterator it = benchmarks.begin(); - it != benchmarks.end(); ++it) { - - State state(it->first, elapsedTimeForOne); - benchmark::BenchFunction& func = it->second; - func(state); + for (const auto &p: benchmarks()) { + State state(p.first, elapsedTimeForOne); + p.second(state); } perf_fini(); } diff --git a/src/bench/bench.h b/src/bench/bench.h index 80dad6a8ef..0e7605c726 100644 --- a/src/bench/bench.h +++ b/src/bench/bench.h @@ -63,7 +63,8 @@ namespace benchmark { class BenchRunner { - static std::map<std::string, BenchFunction> benchmarks; + typedef std::map<std::string, BenchFunction> BenchmarkMap; + static BenchmarkMap &benchmarks(); public: BenchRunner(std::string name, BenchFunction func); |