Skip to content

Commit

Permalink
Merge 07727b8 into 56f5cd6
Browse files Browse the repository at this point in the history
  • Loading branch information
ulvgard authored Nov 24, 2018
2 parents 56f5cd6 + 07727b8 commit cbf1e47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Roman Lebedev <lebedev.ri@gmail.com>
Shuo Chen <chenshuo@chenshuo.com>
Steinar H. Gunderson <sgunderson@bigfoot.com>
Stripe, Inc.
Tobias Ulvgård <tobias@ulvgard.se>
Yixuan Qiu <yixuanq@gmail.com>
Yusuke Suzuki <utatane.tea@gmail.com>
Zbigniew Skowron <zbychs@gmail.com>
24 changes: 17 additions & 7 deletions src/complexity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,41 @@ std::string GetBigOString(BigO complexity) {
// - time : Vector containing the times for the benchmark tests.
// - fitting_curve : lambda expression (e.g. [](int64_t 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 algorithm works like this:
//
// Let the execution times t_i for input sizes n_i be the function
// t_i = f(n_i) = c*g(n_i), where c is a coefficient. The coefficient
// c is compued by assuming that t_i and f(n_i) are the same function
// with different coefficients t_i = c_1*g(n_i) and f(n_i) = c_2*g(n_i).
// Since t_i and f(n_i) are the same function, they only differ in
// proportionality: c_2 = c_1 * g(n_i) / g(n_i)
//
// If, t_i and f(n_i) is not the same function, the Root Mean Square
// (RMS) error will be large and dominated by n.
// Example t_i = c_1*O(n^2) and f(n_i) = c_2*O(n*log(n)):
// ||e|| = sqrt(1/numSamples*sum((t_i - f(n_i))^2))
// ||e|| = sqrt(1/numSamples*sum((c_1*n_i^2 - c_2*n_i*log(n_i))^2))
// ||e||^2*numSamples = sum((c_1*n_i^2 - c_2*n_i*log(n_i)^2)
// which is dominated by the difference (n_i^2 - n_i*log(n_i))^2

LeastSq MinimalLeastSq(const std::vector<int64_t>& n,
const std::vector<double>& time,
BigOFunc* fitting_curve) {
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) {
double gn_i = fitting_curve(n[i]);
sigma_gn += gn_i;
sigma_gn_squared += gn_i * gn_i;
sigma_time += time[i];
sigma_time_gn += time[i] * gn_i;
}

LeastSq result;
result.complexity = oLambda;

// Calculate complexity.
result.coef = sigma_time_gn / sigma_gn_squared;
result.coef = sigma_time / sigma_gn;

// Calculate RMS
double rms = 0.0;
Expand Down

0 comments on commit cbf1e47

Please sign in to comment.