Skip to content

Commit

Permalink
Add another benchmark and a util script
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Nov 25, 2009
1 parent 96907cc commit 599d883
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
90 changes: 90 additions & 0 deletions benchmark/tiers/2/bm_pidigits.rb
@@ -0,0 +1,90 @@
# Profiled: heavy on Bignum ops (+, *, /)

class PiDigitSpigot

def initialize()
@z = Transformation.new 1,0,0,1
@x = Transformation.new 0,0,0,0
@inverse = Transformation.new 0,0,0,0
end

def next!
@y = @z.extract(3)
if safe? @y
@z = produce(@y)
@y
else
@z = consume @x.next!()
next!()
end
end

def safe?(digit)
digit == @z.extract(4)
end

def produce(i)
@inverse.qrst(10,-10*i,0,1).compose(@z)
end

def consume(a)
@z.compose(a)
end
end

class Transformation
attr_reader :q, :r, :s, :t
def initialize (q, r, s, t)
@q,@r,@s,@t,@k = q,r,s,t,0
end

def next!()
@q = @k = @k + 1
@r = 4 * @k + 2
@s = 0
@t = 2 * @k + 1
self
end

def extract(j)
(@q * j + @r) / (@s * j + @t)
end

def compose(a)
self.class.new( @q * a.q,
@q * a.r + r * a.t,
@s * a.q + t * a.s,
@s * a.r + t * a.t
)
end

def qrst *args
initialize *args
self
end


end

WIDTH = 10

def Bench.run
n = 1000
j = 0

digits = PiDigitSpigot.new

while n > 0
if n >= WIDTH
WIDTH.times {digits.next!}
j += WIDTH
else
n.times {digits.next!}
# (WIDTH-n).times {print " "}
j += n
end
# puts "\t:"+j.to_s
n -= WIDTH
end

end
42 changes: 42 additions & 0 deletions benchmark/tiers/percentage.rb
@@ -0,0 +1,42 @@
require 'yaml'

file = ARGV.shift
compare_to = ARGV.shift

data = YAML.load File.read(file)

totals = Hash.new { |h,k| h[k] = 0 }

comp_total = 0

data.each do |run|
file = run['file']
runs = run['runs']

unless comp_run = runs.delete(compare_to)
raise "Unable to find: #{compare_to}"
end

comp = comp_run['median']

comp_total += comp

puts file
puts comp
runs.keys.sort.each do |key|
median = runs[key]['median']
perc = comp.to_f / median
totals[key] += median
puts " #{key} #{perc} #{median}"
end

puts

end

p comp_total
p totals

totals.each do |key, val|
puts "#{key} #{comp_total.to_f / val}"
end

0 comments on commit 599d883

Please sign in to comment.