Skip to content

Commit

Permalink
Update benchmark scripts
Browse files Browse the repository at this point in the history
Related to #6.
  • Loading branch information
agarie committed May 24, 2015
1 parent 3981fda commit 438eba8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
31 changes: 18 additions & 13 deletions benchmark/accuracy.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# Comparing the accuracy of different integraiton methods
# text table is required to display the results in a nice table
require 'integration'
require 'text-table'

# put the funtion you want to benchmark here
func = lambda{|x| x}
#put the actual result of the integration here
actual_result = 5/2.0 + 2* Math::sin(1)
METHODS = [:rectangle, :trapezoid, :simpson, :romberg, :adaptive_quadrature,
:gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid,
:milne, :qng, :qag]
TOL = 1e-9

# Function used in the benchmark.
f = -> x { x }

# Correct result of the integral of `f` in [0, 1].
actual_result = 5 / 2.0 + 2 * Math.sin(1)

table = Text::Table.new
table.head = ['Method','Result','Actual Result','Error','Accuracy']
table.head = ['Method', 'Error', 'Accuracy']

METHODS.each do |method|
result = Integration.integrate(0, 1, { method: method }, &f)

for method in [:rectangle,:trapezoid,:simpson,:romberg,:adaptive_quadrature, :gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, :milne,:qng, :qag]
result = Integration.integrate(0,1,{:method=>method},&func)
if result == nil
if result.nil?
puts method
else
error = (actual_result-result).abs
table.rows << [method,result,actual_result,error,100*(1-error/actual_result.to_f)]
error = (actual_result - result).abs
table.rows << [method, error, 100 * (1 - error / actual_result.to_f)]
end
end
puts table.to_s

puts table.to_s
30 changes: 15 additions & 15 deletions benchmark/speed.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Checking the speed of different integraiton methods

require 'benchmark'
require 'integration'

# set the number of iterations
iterations = 100
puts "Benchmarking with #{iterations} iterations"
METHODS = [:rectangle, :trapezoid, :simpson, :romberg, :adaptive_quadrature,
:gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid,
:milne, :qng, :qag]
TOL = 1e-9
ITERATIONS = 100

# Function used in the benchmark.
f = -> x { x }

puts "Benchmarking with #{ITERATIONS} iterations"

# put the function to be benchmarked here
func = lambda{|x| x}
for method in [:rectangle,:trapezoid,:simpson,:romberg, :adaptive_quadrature, :gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, :milne, :qng, :qag]
Benchmark.bm(25) do |bm|
bm.report(method.to_s) do
iterations.times do
Integration.integrate(0,1,{:method=>method},&func)
end
end
end
Benchmark.bmbm(25) do |bm|
METHODS.each do |method|
params = { method: method, tolerance: TOL }
bm.report(method.to_s) { ITERATIONS.times { Integration.integrate(0, 1, params, &f) } }
end
end

0 comments on commit 438eba8

Please sign in to comment.