diff --git a/mace/core/testing/test_benchmark.cc b/mace/core/testing/test_benchmark.cc index 4d158b25d99a84cc9efd071a3094adcb2ec8ca45..17f56d0cf811d91ea99576f9580789612c103a44 100644 --- a/mace/core/testing/test_benchmark.cc +++ b/mace/core/testing/test_benchmark.cc @@ -37,6 +37,18 @@ Benchmark::Benchmark(const char* name, void (*fn)(int, int, int)) Register(); } +Benchmark* Benchmark::Arg(int x) { + CHECK(num_args_ == 1); + args_.push_back(std::make_pair(x, -1)); + return this; +} + +Benchmark* Benchmark::ArgPair(int x, int y) { + CHECK(num_args_ == 2); + args_.push_back(std::make_pair(x, y)); + return this; +} + // Run all benchmarks void Benchmark::Run() { @@ -44,32 +56,31 @@ void Benchmark::Run() { // Compute name width. int width = 10; - string name; + char name[100]; for (auto b : *all_benchmarks) { - name = b->name_; for (auto arg : b->args_) { - name.resize(b->name_.size()); + strcpy(name, b->name_.c_str()); if (arg.first >= 0) { - name += "/" + arg.first; + sprintf(name, "%s/%d", name, arg.first); if (arg.second >= 0) { - name += "/" + arg.second; + sprintf(name, "%s/%d", name, arg.second); } } - width = std::max(width, name.size()); + width = std::max(width, strlen(name)); } } printf("%-*s %10s %10s\n", width, "Benchmark", "Time(ns)", "Iterations"); printf("%s\n", string(width + 22, '-').c_str()); for (auto b : *all_benchmarks) { - name = b->name_; + for (auto arg : b->args_) { - name.resize(b->name_.size()); + strcpy(name, b->name_.c_str()); if (arg.first >= 0) { - name += "/" + arg.first; + sprintf(name, "%s/%d", name, arg.first); if (arg.second >= 0) { - name += "/" + arg.second; + sprintf(name, "%s/%d", name, arg.second); } } @@ -89,7 +100,7 @@ void Benchmark::Run() { (items_processed * 1e-6) / seconds); full_label += buf; } - printf("%-*s %10.0f %10d\t%s\n", width, name.c_str(), + printf("%-*s %10.0f %10d\t%s\n", width, name, seconds * 1e9 / iters, iters, full_label.c_str()); } } diff --git a/mace/core/testing/test_benchmark.h b/mace/core/testing/test_benchmark.h index 896399415bfd3372df4ef7241a64ce8346fbad2e..44a352f54df4cae609b3955eb5343c9b78d34126 100644 --- a/mace/core/testing/test_benchmark.h +++ b/mace/core/testing/test_benchmark.h @@ -24,6 +24,8 @@ class Benchmark { Benchmark(const char* name, void (*fn)(int)); Benchmark(const char* name, void (*fn)(int, int)); Benchmark(const char* name, void (*fn)(int, int, int)); + Benchmark* Arg(int x); + Benchmark* ArgPair(int x, int y); static void Run(); @@ -40,12 +42,10 @@ class Benchmark { }; void RunBenchmarks(); -void SetLabel(const std::string& label); void BytesProcessed(int64); void ItemsProcessed(int64); void StartTiming(); void StopTiming(); -void UseRealTime(); } // namespace testing } // namespace mace diff --git a/mace/examples/benchmark_example.cc b/mace/examples/benchmark_example.cc index dc3b9c4a2f6e7423b0d27446832e2a3729d89159..106c6c3c4ccdf7dcc091a6c9f9bbc8c0c15d2611 100644 --- a/mace/examples/benchmark_example.cc +++ b/mace/examples/benchmark_example.cc @@ -25,3 +25,21 @@ static void foo(int iters) { BENCHMARK(foo); +static void bar(int iters, int n) { + const int64 tot = static_cast(iters) * n; + mace::testing::ItemsProcessed(tot); + mace::testing::BytesProcessed(tot * (sizeof(float))); + + float* inp = new float[n]; + float* out = new float[n]; + + while (iters--) { + for (int i=0; i < n; i++) { + out[i] = inp[i] * 2.0; + } + } + delete[] inp; + delete[] out; +} + +BENCHMARK(bar)->Arg(32)->Arg(64)->Arg(128);