From e246699f25f50e80fad71a1352313f5f3d3e21d3 Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 21:18:56 +0200 Subject: [PATCH 01/13] added auto as default value for complexity --- include/benchmark/benchmark_api.h | 2 +- test/complexity_test.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h index 2fe8753d..9c32bace 100644 --- a/include/benchmark/benchmark_api.h +++ b/include/benchmark/benchmark_api.h @@ -501,7 +501,7 @@ public: // Set the asymptotic computational complexity for the benchmark. If called // the asymptotic computational complexity will be shown on the output. - Benchmark* Complexity(BigO complexity); + Benchmark* Complexity(BigO complexity = benchmark::oAuto); // Support for running multiple copies of the same benchmark concurrently // in multiple threads. This may be useful when measuring the scaling diff --git a/test/complexity_test.cc b/test/complexity_test.cc index 6e6ae3c3..35a6fa0c 100644 --- a/test/complexity_test.cc +++ b/test/complexity_test.cc @@ -40,7 +40,7 @@ static void BM_Complexity_O_N(benchmark::State& state) { state.SetComplexityN(state.range_x()); } BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oN); -BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oAuto); +BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(); static void BM_Complexity_O_N_Squared(benchmark::State& state) { std::string s1(state.range_x(), '-'); From 087f0d3f1bc6610ceaa346f8e573dd23236cea08 Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 22:26:57 +0200 Subject: [PATCH 02/13] upgraded leastsq --- src/minimal_leastsq.cc | 98 +++++++++++++++++++++++++----------------- src/minimal_leastsq.h | 19 +++++++- 2 files changed, 76 insertions(+), 41 deletions(-) diff --git a/src/minimal_leastsq.cc b/src/minimal_leastsq.cc index 2a738875..d63e6f6a 100644 --- a/src/minimal_leastsq.cc +++ b/src/minimal_leastsq.cc @@ -20,37 +20,54 @@ #include // Internal function to calculate the different scalability forms -double FittingCurve(double n, benchmark::BigO complexity) { +std::function FittingCurve(benchmark::BigO complexity) { switch (complexity) { - case benchmark::oN: - return n; - case benchmark::oNSquared: - return pow(n, 2); - case benchmark::oNCubed: - return pow(n, 3); - case benchmark::oLogN: - return log2(n); - case benchmark::oNLogN: - return n * log2(n); - case benchmark::o1: - default: - return 1; + case benchmark::oN: + return [](int n) {return n; }; + case benchmark::oNSquared: + return [](int n) {return n*n; }; + case benchmark::oNCubed: + return [](int n) {return n*n*n; }; + case benchmark::oLogN: + return [](int n) {return log2(n); }; + case benchmark::oNLogN: + return [](int n) {return n * log2(n); }; + case benchmark::o1: + default: + return [](int) {return 1; }; } } -// Internal function to find the coefficient for the high-order term in the -// running time, by minimizing the sum of squares of relative error. -// - n : Vector containing the size of the benchmark tests. -// - time : Vector containing the times for the benchmark tests. -// - complexity : Fitting curve. +// Internal function to to return an string for the calculated complexity +std::string GetBigOString(benchmark::BigO complexity) { + switch (complexity) { + case benchmark::oN: + return "* N"; + case benchmark::oNSquared: + return "* N**2"; + case benchmark::oNCubed: + return "* N**3"; + case benchmark::oLogN: + return "* lgN"; + case benchmark::oNLogN: + return "* NlgN"; + case benchmark::o1: + return "* 1"; + default: + return ""; + } +} + +// Find the coefficient for the high-order term in the running time, by minimizing the sum of squares of relative error, for the fitting curve given on the lambda expresion. +// - n : Vector containing the size of the benchmark tests. +// - time : Vector containing the times for the benchmark tests. +// - fitting_curve : lambda expresion (e.g. [](int n) {return n; };). // For a deeper explanation on the algorithm logic, look the README file at // http://github.com/ismaelJimenez/Minimal-Cpp-Least-Squared-Fit -LeastSq CalculateLeastSq(const std::vector& n, - const std::vector& time, - const benchmark::BigO complexity) { - CHECK_NE(complexity, benchmark::oAuto); - +LeastSq CalculateLeastSq(const std::vector& n, + const std::vector& time, + std::function fitting_curve) { double sigma_gn = 0; double sigma_gn_squared = 0; double sigma_time = 0; @@ -58,7 +75,7 @@ LeastSq CalculateLeastSq(const std::vector& n, // Calculate least square fitting parameter for (size_t i = 0; i < n.size(); ++i) { - double gn_i = FittingCurve(n[i], complexity); + double gn_i = fitting_curve(n[i]); sigma_gn += gn_i; sigma_gn_squared += gn_i * gn_i; sigma_time += time[i]; @@ -66,26 +83,19 @@ LeastSq CalculateLeastSq(const std::vector& n, } LeastSq result; - result.complexity = complexity; // Calculate complexity. - // o1 is treated as an special case - if (complexity != benchmark::o1) { - result.coef = sigma_time_gn / sigma_gn_squared; - } else { - result.coef = sigma_time / n.size(); - } + result.coef = sigma_time_gn / sigma_gn_squared; // Calculate RMS double rms = 0; for (size_t i = 0; i < n.size(); ++i) { - double fit = result.coef * FittingCurve(n[i], complexity); + double fit = result.coef * fitting_curve(n[i]); rms += pow((time[i] - fit), 2); } - double mean = sigma_time / n.size(); - // Normalized RMS by the mean of the observed values + double mean = sigma_time / n.size(); result.rms = sqrt(rms / n.size()) / mean; return result; @@ -105,24 +115,32 @@ LeastSq MinimalLeastSq(const std::vector& n, CHECK_GE(n.size(), 2); // Do not compute fitting curve is less than two benchmark runs are given CHECK_NE(complexity, benchmark::oNone); + LeastSq best_fit; + if(complexity == benchmark::oAuto) { std::vector fit_curves = { benchmark::oLogN, benchmark::oN, benchmark::oNLogN, benchmark::oNSquared, benchmark::oNCubed }; // Take o1 as default best fitting curve - LeastSq best_fit = CalculateLeastSq(n, time, benchmark::o1); + best_fit = CalculateLeastSq(n, time, FittingCurve(benchmark::o1)); + best_fit.complexity = benchmark::o1; + best_fit.caption = GetBigOString(benchmark::o1); // Compute all possible fitting curves and stick to the best one for (const auto& fit : fit_curves) { - LeastSq current_fit = CalculateLeastSq(n, time, fit); + LeastSq current_fit = CalculateLeastSq(n, time, FittingCurve(fit)); if (current_fit.rms < best_fit.rms) { best_fit = current_fit; + best_fit.complexity = fit; + best_fit.caption = GetBigOString(fit); } } - - return best_fit; + } else { + best_fit = CalculateLeastSq(n, time, FittingCurve(complexity)); + best_fit.complexity = complexity; + best_fit.caption = GetBigOString(complexity); } - return CalculateLeastSq(n, time, complexity); + return best_fit; } diff --git a/src/minimal_leastsq.h b/src/minimal_leastsq.h index 0dc12b7b..ee49ec91 100644 --- a/src/minimal_leastsq.h +++ b/src/minimal_leastsq.h @@ -21,6 +21,7 @@ #include "benchmark/benchmark_api.h" #include +#include // This data structure will contain the result returned by MinimalLeastSq // - coef : Estimated coeficient for the high-order term as @@ -35,11 +36,13 @@ struct LeastSq { LeastSq() : coef(0), rms(0), - complexity(benchmark::oNone) {} + complexity(benchmark::oNone), + caption("") {} double coef; double rms; benchmark::BigO complexity; + std::string caption; }; // Find the coefficient for the high-order term in the running time, by @@ -48,4 +51,18 @@ LeastSq MinimalLeastSq(const std::vector& n, const std::vector& time, const benchmark::BigO complexity = benchmark::oAuto); +// This interface is currently not used from the oustide, but it has been provided +// for future upgrades. If in the future it is not needed to support Cxx03, then +// all the calculations could be upgraded to use lambdas because they are more +// powerful and provide a cleaner inferface than enumerators, but complete +// implementation with lambdas will not work for Cxx03 (e.g. lack of std::function). +// In case lambdas are implemented, the interface would be like : +// -> Complexity([](int n) {return n;};) +// and any arbitrary and valid equation would be allowed, but the option to calculate +// the best fit to the most common scalability curves will still be kept. +LeastSq CalculateLeastSq(const std::vector& n, + const std::vector& time, + std::function fitting_curve); + + #endif From 2f61f8aee0bc8b09429fce8b7d2718f805ed18ac Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 22:57:52 +0200 Subject: [PATCH 03/13] refactor leastsq into complexity --- include/benchmark/complexity.h | 65 ++++++++++++++++------ src/CMakeLists.txt | 2 +- src/{minimal_leastsq.cc => complexity.cc} | 64 ++++++++++++--------- src/console_reporter.cc | 2 +- src/minimal_leastsq.h | 68 ----------------------- src/reporter.cc | 1 - 6 files changed, 86 insertions(+), 116 deletions(-) rename src/{minimal_leastsq.cc => complexity.cc} (74%) delete mode 100644 src/minimal_leastsq.h diff --git a/include/benchmark/complexity.h b/include/benchmark/complexity.h index 93b26de5..1c9f5917 100644 --- a/include/benchmark/complexity.h +++ b/include/benchmark/complexity.h @@ -1,7 +1,26 @@ +// Copyright 2016 Ismael Jimenez Martinez. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Source project : https://github.com/ismaelJimenez/cpp.leastsq +// Adapted to be used with google benchmark + #ifndef COMPLEXITY_H_ #define COMPLEXITY_H_ #include +#include +#include namespace benchmark { @@ -19,24 +38,34 @@ enum BigO { oAuto }; -inline std::string GetBigO(BigO complexity) { - switch (complexity) { - case oN: - return "* N"; - case oNSquared: - return "* N**2"; - case oNCubed: - return "* N**3"; - case oLogN: - return "* lgN"; - case oNLogN: - return "* NlgN"; - case o1: - return "* 1"; - default: - return ""; - } -} +// This data structure will contain the result returned by MinimalLeastSq +// - coef : Estimated coeficient for the high-order term as +// interpolated from data. +// - rms : Normalized Root Mean Squared Error. +// - complexity : Scalability form (e.g. oN, oNLogN). In case a scalability +// form has been provided to MinimalLeastSq this will return +// the same value. In case BigO::oAuto has been selected, this +// parameter will return the best fitting curve detected. + +struct LeastSq { + LeastSq() : + coef(0), + rms(0), + complexity(benchmark::oNone) {} + + double coef; + double rms; + benchmark::BigO complexity; +}; + +// Function to to return an string for the calculated complexity +std::string GetBigOString(benchmark::BigO complexity); + +// Find the coefficient for the high-order term in the running time, by +// minimizing the sum of squares of relative error. +LeastSq MinimalLeastSq(const std::vector& n, + const std::vector& time, + const benchmark::BigO complexity = benchmark::oAuto); } // end namespace benchmark #endif // COMPLEXITY_H_ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a681b35d..6dab64b7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,7 @@ include_directories(${PROJECT_SOURCE_DIR}/src) set(SOURCE_FILES "benchmark.cc" "colorprint.cc" "commandlineflags.cc" "console_reporter.cc" "csv_reporter.cc" "json_reporter.cc" "log.cc" "reporter.cc" "sleep.cc" "string_util.cc" - "sysinfo.cc" "walltime.cc" "minimal_leastsq.cc") + "sysinfo.cc" "walltime.cc" "complexity.cc") # Determine the correct regular expression engine to use if(HAVE_STD_REGEX) set(RE_FILES "re_std.cc") diff --git a/src/minimal_leastsq.cc b/src/complexity.cc similarity index 74% rename from src/minimal_leastsq.cc rename to src/complexity.cc index d63e6f6a..4d21af81 100644 --- a/src/minimal_leastsq.cc +++ b/src/complexity.cc @@ -15,43 +15,45 @@ // Source project : https://github.com/ismaelJimenez/cpp.leastsq // Adapted to be used with google benchmark -#include "minimal_leastsq.h" +#include "benchmark/complexity.h" #include "check.h" #include +namespace benchmark { + // Internal function to calculate the different scalability forms -std::function FittingCurve(benchmark::BigO complexity) { +std::function FittingCurve(BigO complexity) { switch (complexity) { - case benchmark::oN: + case oN: return [](int n) {return n; }; - case benchmark::oNSquared: + case oNSquared: return [](int n) {return n*n; }; - case benchmark::oNCubed: + case oNCubed: return [](int n) {return n*n*n; }; - case benchmark::oLogN: + case oLogN: return [](int n) {return log2(n); }; - case benchmark::oNLogN: + case oNLogN: return [](int n) {return n * log2(n); }; - case benchmark::o1: + case o1: default: return [](int) {return 1; }; } } -// Internal function to to return an string for the calculated complexity -std::string GetBigOString(benchmark::BigO complexity) { +// Function to to return an string for the calculated complexity +std::string GetBigOString(BigO complexity) { switch (complexity) { - case benchmark::oN: + case oN: return "* N"; - case benchmark::oNSquared: + case oNSquared: return "* N**2"; - case benchmark::oNCubed: + case oNCubed: return "* N**3"; - case benchmark::oLogN: + case oLogN: return "* lgN"; - case benchmark::oNLogN: + case oNLogN: return "* NlgN"; - case benchmark::o1: + case o1: return "* 1"; default: return ""; @@ -65,6 +67,16 @@ std::string GetBigOString(benchmark::BigO complexity) { // For a deeper explanation on the algorithm logic, look the README file at // http://github.com/ismaelJimenez/Minimal-Cpp-Least-Squared-Fit +// This interface is currently not used from the oustide, but it has been provided +// for future upgrades. If in the future it is not needed to support Cxx03, then +// all the calculations could be upgraded to use lambdas because they are more +// powerful and provide a cleaner inferface than enumerators, but complete +// implementation with lambdas will not work for Cxx03 (e.g. lack of std::function). +// In case lambdas are implemented, the interface would be like : +// -> Complexity([](int n) {return n;};) +// and any arbitrary and valid equation would be allowed, but the option to calculate +// the best fit to the most common scalability curves will still be kept. + LeastSq CalculateLeastSq(const std::vector& n, const std::vector& time, std::function fitting_curve) { @@ -110,22 +122,20 @@ LeastSq CalculateLeastSq(const std::vector& n, // fitting curve. LeastSq MinimalLeastSq(const std::vector& n, const std::vector& time, - const benchmark::BigO complexity) { + const BigO complexity) { CHECK_EQ(n.size(), time.size()); CHECK_GE(n.size(), 2); // Do not compute fitting curve is less than two benchmark runs are given - CHECK_NE(complexity, benchmark::oNone); + CHECK_NE(complexity, oNone); LeastSq best_fit; - if(complexity == benchmark::oAuto) { - std::vector fit_curves = { - benchmark::oLogN, benchmark::oN, benchmark::oNLogN, benchmark::oNSquared, - benchmark::oNCubed }; + if(complexity == oAuto) { + std::vector fit_curves = { + oLogN, oN, oNLogN, oNSquared, oNCubed }; // Take o1 as default best fitting curve - best_fit = CalculateLeastSq(n, time, FittingCurve(benchmark::o1)); - best_fit.complexity = benchmark::o1; - best_fit.caption = GetBigOString(benchmark::o1); + best_fit = CalculateLeastSq(n, time, FittingCurve(o1)); + best_fit.complexity = o1; // Compute all possible fitting curves and stick to the best one for (const auto& fit : fit_curves) { @@ -133,14 +143,14 @@ LeastSq MinimalLeastSq(const std::vector& n, if (current_fit.rms < best_fit.rms) { best_fit = current_fit; best_fit.complexity = fit; - best_fit.caption = GetBigOString(fit); } } } else { best_fit = CalculateLeastSq(n, time, FittingCurve(complexity)); best_fit.complexity = complexity; - best_fit.caption = GetBigOString(complexity); } return best_fit; } + +} // end namespace benchmark \ No newline at end of file diff --git a/src/console_reporter.cc b/src/console_reporter.cc index 41c00b94..da075191 100644 --- a/src/console_reporter.cc +++ b/src/console_reporter.cc @@ -117,7 +117,7 @@ void ConsoleReporter::PrintRunData(const Run& result) { name_field_width_, result.benchmark_name.c_str()); if(result.report_big_o) { - std::string big_o = result.report_big_o ? GetBigO(result.complexity) : ""; + std::string big_o = result.report_big_o ? GetBigOString(result.complexity) : ""; ColorPrintf(COLOR_YELLOW, "%10.4f %s %10.4f %s ", result.real_accumulated_time * multiplier, big_o.c_str(), diff --git a/src/minimal_leastsq.h b/src/minimal_leastsq.h deleted file mode 100644 index ee49ec91..00000000 --- a/src/minimal_leastsq.h +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2016 Ismael Jimenez Martinez. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Source project : https://github.com/ismaelJimenez/cpp.leastsq -// Adapted to be used with google benchmark - -#if !defined(MINIMAL_LEASTSQ_H_) -#define MINIMAL_LEASTSQ_H_ - -#include "benchmark/benchmark_api.h" - -#include -#include - -// This data structure will contain the result returned by MinimalLeastSq -// - coef : Estimated coeficient for the high-order term as -// interpolated from data. -// - rms : Normalized Root Mean Squared Error. -// - complexity : Scalability form (e.g. oN, oNLogN). In case a scalability -// form has been provided to MinimalLeastSq this will return -// the same value. In case BigO::oAuto has been selected, this -// parameter will return the best fitting curve detected. - -struct LeastSq { - LeastSq() : - coef(0), - rms(0), - complexity(benchmark::oNone), - caption("") {} - - double coef; - double rms; - benchmark::BigO complexity; - std::string caption; -}; - -// Find the coefficient for the high-order term in the running time, by -// minimizing the sum of squares of relative error. -LeastSq MinimalLeastSq(const std::vector& n, - const std::vector& time, - const benchmark::BigO complexity = benchmark::oAuto); - -// This interface is currently not used from the oustide, but it has been provided -// for future upgrades. If in the future it is not needed to support Cxx03, then -// all the calculations could be upgraded to use lambdas because they are more -// powerful and provide a cleaner inferface than enumerators, but complete -// implementation with lambdas will not work for Cxx03 (e.g. lack of std::function). -// In case lambdas are implemented, the interface would be like : -// -> Complexity([](int n) {return n;};) -// and any arbitrary and valid equation would be allowed, but the option to calculate -// the best fit to the most common scalability curves will still be kept. -LeastSq CalculateLeastSq(const std::vector& n, - const std::vector& time, - std::function fitting_curve); - - -#endif diff --git a/src/reporter.cc b/src/reporter.cc index 2830fa14..4fd0ba5d 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -13,7 +13,6 @@ // limitations under the License. #include "benchmark/reporter.h" -#include "minimal_leastsq.h" #include #include From 90a85080636d0626ed975531d08bc1339a405fa9 Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 23:06:27 +0200 Subject: [PATCH 04/13] Update Readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b03d222b..66706eb5 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ calculated automatically. ```c++ BENCHMARK(BM_StringCompare) - ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oAuto); + ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(); ``` ### Templated benchmarks From 1ee11056c1f1117142af36dd3ac4df2c2e6ce1bb Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 23:13:19 +0200 Subject: [PATCH 05/13] move include from .h into .cc --- include/benchmark/complexity.h | 1 - src/complexity.cc | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/benchmark/complexity.h b/include/benchmark/complexity.h index 1c9f5917..72bd9e48 100644 --- a/include/benchmark/complexity.h +++ b/include/benchmark/complexity.h @@ -20,7 +20,6 @@ #include #include -#include namespace benchmark { diff --git a/src/complexity.cc b/src/complexity.cc index 4d21af81..67d8a050 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -18,6 +18,7 @@ #include "benchmark/complexity.h" #include "check.h" #include +#include namespace benchmark { @@ -153,4 +154,4 @@ LeastSq MinimalLeastSq(const std::vector& n, return best_fit; } -} // end namespace benchmark \ No newline at end of file +} // end namespace benchmark From 290ac9ee0ed445811897c790715e47d490dacd9f Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 23:19:32 +0200 Subject: [PATCH 06/13] updated complexity_test.cc to new interface for auto --- test/complexity_test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/complexity_test.cc b/test/complexity_test.cc index 35a6fa0c..225a1811 100644 --- a/test/complexity_test.cc +++ b/test/complexity_test.cc @@ -93,7 +93,7 @@ static void BM_Complexity_O_N_log_N(benchmark::State& state) { state.SetComplexityN(state.range_x()); } BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oNLogN); -BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oAuto); +BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(); // Test benchmark with no range and check no complexity is calculated. void BM_Extreme_Cases(benchmark::State& state) { @@ -101,6 +101,6 @@ void BM_Extreme_Cases(benchmark::State& state) { } } BENCHMARK(BM_Extreme_Cases) -> Complexity(benchmark::oNLogN); -BENCHMARK(BM_Extreme_Cases) -> Arg(42) -> Complexity(benchmark::oAuto); +BENCHMARK(BM_Extreme_Cases) -> Arg(42) -> Complexity(); BENCHMARK_MAIN() From 340fe557e2995addcb2af24dfdc7d86801487330 Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 23:22:53 +0200 Subject: [PATCH 07/13] indent --- src/complexity.cc | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/complexity.cc b/src/complexity.cc index 67d8a050..ad59ce80 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -25,19 +25,19 @@ namespace benchmark { // Internal function to calculate the different scalability forms std::function FittingCurve(BigO complexity) { switch (complexity) { - case oN: - return [](int n) {return n; }; - case oNSquared: - return [](int n) {return n*n; }; - case oNCubed: - return [](int n) {return n*n*n; }; - case oLogN: - return [](int n) {return log2(n); }; - case oNLogN: - return [](int n) {return n * log2(n); }; - case o1: - default: - return [](int) {return 1; }; + case oN: + return [](int n) {return n; }; + case oNSquared: + return [](int n) {return n*n; }; + case oNCubed: + return [](int n) {return n*n*n; }; + case oLogN: + return [](int n) {return log2(n); }; + case oNLogN: + return [](int n) {return n * log2(n); }; + case o1: + default: + return [](int) {return 1; }; } } From 171588561112744263caa5847847e76e9bbde562 Mon Sep 17 00:00:00 2001 From: Ismael Date: Wed, 25 May 2016 23:33:25 +0200 Subject: [PATCH 08/13] fixed typos --- include/benchmark/complexity.h | 2 +- src/complexity.cc | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/benchmark/complexity.h b/include/benchmark/complexity.h index 72bd9e48..f2eaefa6 100644 --- a/include/benchmark/complexity.h +++ b/include/benchmark/complexity.h @@ -57,7 +57,7 @@ struct LeastSq { benchmark::BigO complexity; }; -// Function to to return an string for the calculated complexity +// Function to return an string for the calculated complexity std::string GetBigOString(benchmark::BigO complexity); // Find the coefficient for the high-order term in the running time, by diff --git a/src/complexity.cc b/src/complexity.cc index ad59ce80..8fa75b37 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -41,7 +41,7 @@ std::function FittingCurve(BigO complexity) { } } -// Function to to return an string for the calculated complexity +// Function to return an string for the calculated complexity std::string GetBigOString(BigO complexity) { switch (complexity) { case oN: @@ -61,22 +61,27 @@ std::string GetBigOString(BigO complexity) { } } -// Find the coefficient for the high-order term in the running time, by minimizing the sum of squares of relative error, for the fitting curve given on the lambda expresion. +// Find the coefficient for the high-order term in the running time, by +// minimizing the sum of squares of relative error, for the fitting curve +// given by the lambda expresion. // - n : Vector containing the size of the benchmark tests. // - time : Vector containing the times for the benchmark tests. // - fitting_curve : lambda expresion (e.g. [](int n) {return n; };). + // For a deeper explanation on the algorithm logic, look the README file at // http://github.com/ismaelJimenez/Minimal-Cpp-Least-Squared-Fit -// This interface is currently not used from the oustide, but it has been provided -// for future upgrades. If in the future it is not needed to support Cxx03, then -// all the calculations could be upgraded to use lambdas because they are more -// powerful and provide a cleaner inferface than enumerators, but complete -// implementation with lambdas will not work for Cxx03 (e.g. lack of std::function). +// This interface is currently not used from the oustide, but it has been +// provided for future upgrades. If in the future it is not needed to support +// Cxx03, then all the calculations could be upgraded to use lambdas because +// they are more powerful and provide a cleaner inferface than enumerators, +// but complete implementation with lambdas will not work for Cxx03 +// (e.g. lack of std::function). // In case lambdas are implemented, the interface would be like : // -> Complexity([](int n) {return n;};) -// and any arbitrary and valid equation would be allowed, but the option to calculate -// the best fit to the most common scalability curves will still be kept. +// and any arbitrary and valid equation would be allowed, but the option to +// calculate the best fit to the most common scalability curves will still +// be kept. LeastSq CalculateLeastSq(const std::vector& n, const std::vector& time, From 37ab858e4b245a49805b01358655fab069474a7c Mon Sep 17 00:00:00 2001 From: Ismael Date: Thu, 26 May 2016 19:44:11 +0200 Subject: [PATCH 09/13] initialized doubles to 0.0 --- include/benchmark/complexity.h | 12 ++++++------ src/complexity.cc | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/benchmark/complexity.h b/include/benchmark/complexity.h index f2eaefa6..76318d8b 100644 --- a/include/benchmark/complexity.h +++ b/include/benchmark/complexity.h @@ -48,23 +48,23 @@ enum BigO { struct LeastSq { LeastSq() : - coef(0), - rms(0), - complexity(benchmark::oNone) {} + coef(0.0), + rms(0.0), + complexity(oNone) {} double coef; double rms; - benchmark::BigO complexity; + BigO complexity; }; // Function to return an string for the calculated complexity -std::string GetBigOString(benchmark::BigO complexity); +std::string GetBigOString(BigO complexity); // Find the coefficient for the high-order term in the running time, by // minimizing the sum of squares of relative error. LeastSq MinimalLeastSq(const std::vector& n, const std::vector& time, - const benchmark::BigO complexity = benchmark::oAuto); + const BigO complexity = oAuto); } // end namespace benchmark #endif // COMPLEXITY_H_ diff --git a/src/complexity.cc b/src/complexity.cc index 8fa75b37..e7233109 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -86,10 +86,10 @@ std::string GetBigOString(BigO complexity) { LeastSq CalculateLeastSq(const std::vector& n, const std::vector& time, std::function fitting_curve) { - double sigma_gn = 0; - double sigma_gn_squared = 0; - double sigma_time = 0; - double sigma_time_gn = 0; + double sigma_gn = 0.0; + double sigma_gn_squared = 0.0; + double sigma_time = 0.0; + double sigma_time_gn = 0.0; // Calculate least square fitting parameter for (size_t i = 0; i < n.size(); ++i) { @@ -106,7 +106,7 @@ LeastSq CalculateLeastSq(const std::vector& n, result.coef = sigma_time_gn / sigma_gn_squared; // Calculate RMS - double rms = 0; + double rms = 0.0; for (size_t i = 0; i < n.size(); ++i) { double fit = result.coef * fitting_curve(n[i]); rms += pow((time[i] - fit), 2); From d82f0c313133c60e3a5db5be6f7d2299cd5ffdd8 Mon Sep 17 00:00:00 2001 From: Ismael Date: Thu, 26 May 2016 20:57:27 +0200 Subject: [PATCH 10/13] added includes --- src/console_reporter.cc | 1 + src/reporter.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/src/console_reporter.cc b/src/console_reporter.cc index da075191..bd5b403f 100644 --- a/src/console_reporter.cc +++ b/src/console_reporter.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "benchmark/reporter.h" +#include "benchmark/complexity.h" #include #include diff --git a/src/reporter.cc b/src/reporter.cc index 4fd0ba5d..9b65c9ba 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "benchmark/reporter.h" +#include "benchmark/complexity.h" #include #include From ac3ec2ded37e4f9e4083f8b65644f7c4b60336d7 Mon Sep 17 00:00:00 2001 From: Ismael Date: Thu, 26 May 2016 21:16:40 +0200 Subject: [PATCH 11/13] moved complexity.h into src and BigO enum into benchmark_api --- include/benchmark/benchmark_api.h | 15 ++++++++++++++- src/complexity.cc | 2 +- {include/benchmark => src}/complexity.h | 22 +++++----------------- src/console_reporter.cc | 2 +- src/reporter.cc | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) rename {include/benchmark => src}/complexity.h (81%) diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h index 9c32bace..29d0f6bb 100644 --- a/include/benchmark/benchmark_api.h +++ b/include/benchmark/benchmark_api.h @@ -154,7 +154,6 @@ BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); #include #include "macros.h" -#include "complexity.h" namespace benchmark { class BenchmarkReporter; @@ -239,6 +238,20 @@ enum TimeUnit { kMillisecond }; +// BigO is passed to a benchmark in order to specify the asymptotic computational +// complexity for the benchmark. In case oAuto is selected, complexity will be +// calculated automatically to the best fit. +enum BigO { + oNone, + o1, + oN, + oNSquared, + oNCubed, + oLogN, + oNLogN, + oAuto +}; + // State is passed to a running Benchmark and contains state for the // benchmark to use. class State { diff --git a/src/complexity.cc b/src/complexity.cc index e7233109..c3dd40e8 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -15,7 +15,7 @@ // Source project : https://github.com/ismaelJimenez/cpp.leastsq // Adapted to be used with google benchmark -#include "benchmark/complexity.h" +#include "complexity.h" #include "check.h" #include #include diff --git a/include/benchmark/complexity.h b/src/complexity.h similarity index 81% rename from include/benchmark/complexity.h rename to src/complexity.h index 76318d8b..9b1ae8dc 100644 --- a/include/benchmark/complexity.h +++ b/src/complexity.h @@ -21,21 +21,9 @@ #include #include -namespace benchmark { +#include "benchmark/benchmark_api.h" -// BigO is passed to a benchmark in order to specify the asymptotic computational -// complexity for the benchmark. In case oAuto is selected, complexity will be -// calculated automatically to the best fit. -enum BigO { - oNone, - o1, - oN, - oNSquared, - oNCubed, - oLogN, - oNLogN, - oAuto -}; +namespace benchmark { // This data structure will contain the result returned by MinimalLeastSq // - coef : Estimated coeficient for the high-order term as @@ -54,17 +42,17 @@ struct LeastSq { double coef; double rms; - BigO complexity; + benchmark::BigO complexity; }; // Function to return an string for the calculated complexity -std::string GetBigOString(BigO complexity); +std::string GetBigOString(benchmark::BigO complexity); // Find the coefficient for the high-order term in the running time, by // minimizing the sum of squares of relative error. LeastSq MinimalLeastSq(const std::vector& n, const std::vector& time, - const BigO complexity = oAuto); + const benchmark::BigO complexity = oAuto); } // end namespace benchmark #endif // COMPLEXITY_H_ diff --git a/src/console_reporter.cc b/src/console_reporter.cc index bd5b403f..62df4c7f 100644 --- a/src/console_reporter.cc +++ b/src/console_reporter.cc @@ -13,7 +13,7 @@ // limitations under the License. #include "benchmark/reporter.h" -#include "benchmark/complexity.h" +#include "complexity.h" #include #include diff --git a/src/reporter.cc b/src/reporter.cc index 9b65c9ba..aa1e69af 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -13,7 +13,7 @@ // limitations under the License. #include "benchmark/reporter.h" -#include "benchmark/complexity.h" +#include "complexity.h" #include #include From 805e8baee9da3744428e3f646f321c29283c4072 Mon Sep 17 00:00:00 2001 From: Ismael Date: Thu, 26 May 2016 21:26:43 +0200 Subject: [PATCH 12/13] small refactor --- src/complexity.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/complexity.h b/src/complexity.h index 9b1ae8dc..dd683621 100644 --- a/src/complexity.h +++ b/src/complexity.h @@ -42,17 +42,17 @@ struct LeastSq { double coef; double rms; - benchmark::BigO complexity; + BigO complexity; }; // Function to return an string for the calculated complexity -std::string GetBigOString(benchmark::BigO complexity); +std::string GetBigOString(BigO complexity); // Find the coefficient for the high-order term in the running time, by // minimizing the sum of squares of relative error. LeastSq MinimalLeastSq(const std::vector& n, const std::vector& time, - const benchmark::BigO complexity = oAuto); + const BigO complexity = oAuto); } // end namespace benchmark #endif // COMPLEXITY_H_ From c1c7d33279b463088550986fe6f311a3ad2faa2e Mon Sep 17 00:00:00 2001 From: Ismael Date: Thu, 26 May 2016 22:39:17 +0200 Subject: [PATCH 13/13] added benchmar_apit to complexity.cc --- src/complexity.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/complexity.cc b/src/complexity.cc index c3dd40e8..1f6fee0c 100644 --- a/src/complexity.cc +++ b/src/complexity.cc @@ -15,6 +15,8 @@ // Source project : https://github.com/ismaelJimenez/cpp.leastsq // Adapted to be used with google benchmark +#include "benchmark/benchmark_api.h" + #include "complexity.h" #include "check.h" #include