From 90fcdc90d3559d8df98f5b7586dfef0ca45abf61 Mon Sep 17 00:00:00 2001 From: Mickey Rose Date: Tue, 16 Feb 2016 16:27:32 +0100 Subject: [PATCH] benchmarks: fix output formatting snprintf is a better tool for this task, anyway, but here's what was wrong with the stream formatting: s << name << ":" << std::setw(45 - (int)s.tellp()) cannot be used to align columns, tellp() may return any of: 0 name.length() name.length() + strlen(":") because the compiler is allowed to reorder the evaluation of sub-expressions as it likes -- it may evaluate s.tellp() before evaluating (s << name) or in-between (s << name) and (s << ":") try with gcc-4.8 --- benchmark/bench_framework.hpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/benchmark/bench_framework.hpp b/benchmark/bench_framework.hpp index 52088e4e1..366f82706 100644 --- a/benchmark/bench_framework.hpp +++ b/benchmark/bench_framework.hpp @@ -9,7 +9,7 @@ // stl #include -#include +#include // snprintf #include #include #include @@ -96,11 +96,7 @@ int run(T const& test_runner, std::string const& name) { std::chrono::high_resolution_clock::time_point start; std::chrono::high_resolution_clock::duration elapsed; - std::stringstream s; - s << name << ":" - << std::setw(45 - (int)s.tellp()) << std::right - << " t:" << test_runner.threads() - << " i:" << test_runner.iterations(); + if (test_runner.threads() > 0) { using thread_group = std::vector >; @@ -120,9 +116,15 @@ int run(T const& test_runner, std::string const& name) test_runner(); elapsed = std::chrono::high_resolution_clock::now() - start; } - s << std::setw(65 - (int)s.tellp()) << std::right - << std::chrono::duration_cast(elapsed).count() << " milliseconds\n"; - std::clog << s.str(); + + char msg[200]; + std::snprintf(msg, sizeof(msg), + "%-43s %3zu threads %9zu iters %6.0f milliseconds", + name.c_str(), + test_runner.threads(), + test_runner.iterations(), + std::chrono::duration(elapsed).count()); + std::clog << msg << "\n"; } return 0; }