monobench: don't sort the Bench vector while iterating through it...

We sort to display Bench results in an ascending order, but we're doing this while iterating through the bench vector.  This is probably undefined, and really screws up the sample distribution in practice.  Slow benches run more often.

Instead, copy the results of the benches to a new Result vector to sort and display.  Each bench now gets its fair share of samples.

Change-Id: I4ead0d9d69af271c9971eedb35604a4b3bca0784
Reviewed-on: https://skia-review.googlesource.com/6623
Reviewed-by: Mike Klein <mtklein@chromium.org>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
diff --git a/tools/monobench.cpp b/tools/monobench.cpp
index 722ae91..fa0de8a 100644
--- a/tools/monobench.cpp
+++ b/tools/monobench.cpp
@@ -111,15 +111,22 @@
                 bench.best = std::min(bench.best, elapsed / loops);
                 samples++;
 
-                std::sort(benches.begin(), benches.end(), [](const Bench& a, const Bench& b) {
+                struct Result { const char* name; ns best; };
+                std::vector<Result> sorted(benches.size());
+                for (size_t i = 0; i < benches.size(); i++) {
+                    sorted[i].name = benches[i].name.c_str();
+                    sorted[i].best = benches[i].best;
+                }
+                std::sort(sorted.begin(), sorted.end(), [](const Result& a, const Result& b) {
                     return a.best < b.best;
                 });
+
                 SkDebugf("%s%d", kSkOverwriteLine, samples);
-                for (auto& bench : benches) {
-                    if (benches.size() == 1) {
-                        SkDebugf("  %s %gns" , bench.name.c_str(), bench.best.count());
+                for (auto& result : sorted) {
+                    if (sorted.size() == 1) {
+                        SkDebugf("  %s %gns" , result.name, result.best.count());
                     } else {
-                        SkDebugf("  %s %.3gx", bench.name.c_str(), bench.best / benches[0].best);
+                        SkDebugf("  %s %.3gx", result.name, result.best / sorted[0].best);
                     }
                 }
                 break;