summaryrefslogtreecommitdiff
path: root/lib/minitest/benchmark.rb
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-02 04:48:43 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-02 04:48:43 +0000
commitf8e5c7c79e720d3b0af3cb96f27d421f08eb7744 (patch)
tree0d950c668cf9141d516152aa7ca117d62afa3676 /lib/minitest/benchmark.rb
parentce3029cd19dc40f4ffe09b8c6633500bd182b324 (diff)
Imported minitest 4.7.4 (r8483)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest/benchmark.rb')
-rw-r--r--lib/minitest/benchmark.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/minitest/benchmark.rb b/lib/minitest/benchmark.rb
index 02121db340..e233282b0a 100644
--- a/lib/minitest/benchmark.rb
+++ b/lib/minitest/benchmark.rb
@@ -163,6 +163,26 @@ class MiniTest::Unit # :nodoc:
##
# Runs the given +work+ and asserts that the times gathered fit to
+ # match a logarithmic curve within a given error +threshold+.
+ #
+ # Fit is calculated by #fit_logarithmic.
+ #
+ # Ranges are specified by ::bench_range.
+ #
+ # Eg:
+ #
+ # def bench_algorithm
+ # assert_performance_logarithmic 0.9999 do |n|
+ # @obj.algorithm(n)
+ # end
+ # end
+
+ def assert_performance_logarithmic threshold = 0.99, &work
+ assert_performance validation_for_fit(:logarithmic, threshold), &work
+ end
+
+ ##
+ # Runs the given +work+ and asserts that the times gathered fit to
# match a straight line within a given error +threshold+.
#
# Fit is calculated by #fit_linear.
@@ -237,6 +257,29 @@ class MiniTest::Unit # :nodoc:
end
##
+ # To fit a functional form: y = a + b*ln(x).
+ #
+ # Takes x and y values and returns [a, b, r^2].
+ #
+ # See: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
+
+ def fit_logarithmic xs, ys
+ n = xs.size
+ xys = xs.zip(ys)
+ slnx2 = sigma(xys) { |x,y| Math.log(x) ** 2 }
+ slnx = sigma(xys) { |x,y| Math.log(x) }
+ sylnx = sigma(xys) { |x,y| y * Math.log(x) }
+ sy = sigma(xys) { |x,y| y }
+
+ c = n * slnx2 - slnx ** 2
+ b = ( n * sylnx - sy * slnx ) / c
+ a = (sy - b * slnx) / n
+
+ return a, b, fit_error(xys) { |x| a + b * Math.log(x) }
+ end
+
+
+ ##
# Fits the functional form: a + bx.
#
# Takes x and y values and returns [a, b, r^2].