From 105f1f38a969bbe0e0ee1041c3c315a56bdc8c47 Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 19:34:39 +0200 Subject: Add some of MRI's benchmarks --- .gitignore | 4 +- benchmark/bm_so_ackermann.rb | 19 ++++++ benchmark/bm_so_array.rb | 23 +++++++ benchmark/bm_so_binary_trees.rb | 62 +++++++++++++++++ benchmark/bm_so_concatenate.rb | 18 +++++ benchmark/bm_so_exception.rb | 61 +++++++++++++++++ benchmark/bm_so_lists.rb | 2 +- benchmark/bm_so_mandelbrot.rb | 17 ++--- benchmark/bm_so_matrix.rb | 48 +++++++++++++ benchmark/bm_so_nbody.rb | 148 ++++++++++++++++++++++++++++++++++++++++ benchmark/bm_so_nested_loop.rb | 24 +++++++ benchmark/bm_so_nsieve_bits.rb | 43 ++++++++++++ benchmark/bm_so_object.rb | 56 +++++++++++++++ benchmark/bm_so_partial_sums.rb | 31 +++++++++ benchmark/bm_so_random.rb | 20 ++++++ benchmark/bm_so_sieve.rb | 24 +++++++ benchmark/bm_so_spectralnorm.rb | 50 ++++++++++++++ benchmark/plot.gpl | 2 +- 18 files changed, 636 insertions(+), 16 deletions(-) create mode 100644 benchmark/bm_so_ackermann.rb create mode 100644 benchmark/bm_so_array.rb create mode 100644 benchmark/bm_so_binary_trees.rb create mode 100644 benchmark/bm_so_concatenate.rb create mode 100644 benchmark/bm_so_exception.rb create mode 100644 benchmark/bm_so_matrix.rb create mode 100644 benchmark/bm_so_nbody.rb create mode 100644 benchmark/bm_so_nested_loop.rb create mode 100644 benchmark/bm_so_nsieve_bits.rb create mode 100644 benchmark/bm_so_object.rb create mode 100644 benchmark/bm_so_partial_sums.rb create mode 100644 benchmark/bm_so_random.rb create mode 100644 benchmark/bm_so_sieve.rb create mode 100644 benchmark/bm_so_spectralnorm.rb diff --git a/.gitignore b/.gitignore index 3657b105d..e3f434432 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,8 @@ *.bak *.d *.o -benchmark/**/*.dat -benchmark/*.pdf +/benchmark/**/*.dat +/benchmark/*.pdf *.orig *.pdb *.rej diff --git a/benchmark/bm_so_ackermann.rb b/benchmark/bm_so_ackermann.rb new file mode 100644 index 000000000..7db5be905 --- /dev/null +++ b/benchmark/bm_so_ackermann.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- mode: ruby -*- +# $Id: ackermann-ruby.code,v 1.4 2004/11/13 07:40:41 bfulgham Exp $ +# http://www.bagley.org/~doug/shootout/ + +def ack(m, n) + if m == 0 then + n + 1 + elsif n == 0 then + ack(m - 1, 1) + else + ack(m - 1, ack(m, n - 1)) + end +end + +NUM = 9 +ack(3, NUM) + + diff --git a/benchmark/bm_so_array.rb b/benchmark/bm_so_array.rb new file mode 100644 index 000000000..2b8fce8f9 --- /dev/null +++ b/benchmark/bm_so_array.rb @@ -0,0 +1,23 @@ +#!/usr/bin/ruby +# -*- mode: ruby -*- +# $Id: ary-ruby.code,v 1.4 2004/11/13 07:41:27 bfulgham Exp $ +# http://www.bagley.org/~doug/shootout/ +# with help from Paul Brannan and Mark Hubbart + +n = 9000 # Integer(ARGV.shift || 1) + +x = Array.new(n) +y = Array.new(n, 0) + +n.times{|bi| + x[bi] = bi + 1 +} + +(0 .. 999).each do |e| + (n-1).step(0,-1) do |bi| + y[bi] += x.at(bi) + end +end +# puts "#{y.first} #{y.last}" + + diff --git a/benchmark/bm_so_binary_trees.rb b/benchmark/bm_so_binary_trees.rb new file mode 100644 index 000000000..8250fba31 --- /dev/null +++ b/benchmark/bm_so_binary_trees.rb @@ -0,0 +1,62 @@ +# The Computer Language Shootout Benchmarks +# http://shootout.alioth.debian.org +# +# contributed by Jesse Millikan + +# disable output +alias puts_orig puts +def puts str + # disable puts +end + +def item_check(tree) + if tree[0] == nil + tree[1] + else + tree[1] + item_check(tree[0]) - item_check(tree[2]) + end +end + +def bottom_up_tree(item, depth) + if depth > 0 + item_item = 2 * item + depth -= 1 + [bottom_up_tree(item_item - 1, depth), item, bottom_up_tree(item_item, depth)] + else + [nil, item, nil] + end +end + +max_depth = 14 # ARGV[0].to_i +min_depth = 4 + +max_depth = min_depth + 2 if min_depth + 2 > max_depth + +stretch_depth = max_depth + 1 +stretch_tree = bottom_up_tree(0, stretch_depth) + +puts "stretch tree of depth #{stretch_depth}\t check: #{item_check(stretch_tree)}" +stretch_tree = nil + +long_lived_tree = bottom_up_tree(0, max_depth) + +min_depth.step(max_depth + 1, 2) do |depth| + iterations = 2**(max_depth - depth + min_depth) + + check = 0 + + for i in 1..iterations + temp_tree = bottom_up_tree(i, depth) + check += item_check(temp_tree) + + temp_tree = bottom_up_tree(-i, depth) + check += item_check(temp_tree) + end + + puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}" +end + +puts "long lived tree of depth #{max_depth}\t check: #{item_check(long_lived_tree)}" + +undef puts +alias puts puts_orig diff --git a/benchmark/bm_so_concatenate.rb b/benchmark/bm_so_concatenate.rb new file mode 100644 index 000000000..873214de7 --- /dev/null +++ b/benchmark/bm_so_concatenate.rb @@ -0,0 +1,18 @@ +#!/usr/bin/ruby +# -*- mode: ruby -*- +# $Id: strcat-ruby.code,v 1.4 2004/11/13 07:43:28 bfulgham Exp $ +# http://www.bagley.org/~doug/shootout/ +# based on code from Aristarkh A Zagorodnikov and Dat Nguyen + +STUFF = "hello\n" +i = 0 +while i<10 + i += 1 + hello = '' + 4_000_000.times do |e| + hello << STUFF + end +end +# puts hello.length + + diff --git a/benchmark/bm_so_exception.rb b/benchmark/bm_so_exception.rb new file mode 100644 index 000000000..deb003a59 --- /dev/null +++ b/benchmark/bm_so_exception.rb @@ -0,0 +1,61 @@ +#!/usr/bin/ruby +# -*- mode: ruby -*- +# $Id: except-ruby.code,v 1.4 2004/11/13 07:41:33 bfulgham Exp $ +# http://www.bagley.org/~doug/shootout/ + +$HI = 0 +$LO = 0 +NUM = 250000 # Integer(ARGV[0] || 1) + + +class Lo_Exception < Exception + def initialize(num) + @value = num + end +end + +class Hi_Exception < Exception + def initialize(num) + @value = num + end +end + +def some_function(num) + begin + hi_function(num) + rescue + print "We shouldn't get here, exception is: #{$!.type}\n" + end +end + +def hi_function(num) + begin + lo_function(num) + rescue Hi_Exception + $HI = $HI + 1 + end +end + +def lo_function(num) + begin + blowup(num) + rescue Lo_Exception + $LO = $LO + 1 + end +end + +def blowup(num) + if num % 2 == 0 + raise Lo_Exception.new(num) + else + raise Hi_Exception.new(num) + end +end + + +i = 1 +max = NUM+1 +while i < max + i += 1 + some_function(i+1) +end diff --git a/benchmark/bm_so_lists.rb b/benchmark/bm_so_lists.rb index f8d26797a..e8f4a2a5f 100644 --- a/benchmark/bm_so_lists.rb +++ b/benchmark/bm_so_lists.rb @@ -40,7 +40,7 @@ end i = 0 while i> CharExponent][p & LowMask] == 1 + count += 1 + p.step(pmax, p) do |mult| + a = mult >> CharExponent + b = mult & LowMask + items[a] -= masks[b] if items[a][b] != 0 + end + end + end + count +end + +n = 9 # (ARGV[0] || 2).to_i +n.step(n - 2, -1) do |exponent| + break if exponent < 0 + m = 2 ** exponent * 10_000 + count = sieve(m) + printf "Primes up to %8d %8d\n", m, count +end + diff --git a/benchmark/bm_so_object.rb b/benchmark/bm_so_object.rb new file mode 100644 index 000000000..e8607c719 --- /dev/null +++ b/benchmark/bm_so_object.rb @@ -0,0 +1,56 @@ +#!/usr/bin/ruby +# -*- mode: ruby -*- +# $Id: objinst-ruby.code,v 1.4 2004/11/13 07:42:25 bfulgham Exp $ +# http://www.bagley.org/~doug/shootout/ +# with help from Aristarkh Zagorodnikov + +class Toggle + def initialize(start_state) + @bool = start_state + end + + def value + @bool + end + + def activate + @bool = !@bool + self + end +end + +class NthToggle < Toggle + def initialize(start_state, max_counter) + super start_state + @count_max = max_counter + @counter = 0 + end + + def activate + @counter += 1 + if @counter >= @count_max + @bool = !@bool + @counter = 0 + end + self + end +end + +n = 1500000 # (ARGV.shift || 1).to_i + +toggle = Toggle.new 1 +5.times do + toggle.activate.value ? 'true' : 'false' +end +n.times do + toggle = Toggle.new 1 +end + +ntoggle = NthToggle.new 1, 3 +8.times do + ntoggle.activate.value ? 'true' : 'false' +end +n.times do + ntoggle = NthToggle.new 1, 3 +end + diff --git a/benchmark/bm_so_partial_sums.rb b/benchmark/bm_so_partial_sums.rb new file mode 100644 index 000000000..630b45cb8 --- /dev/null +++ b/benchmark/bm_so_partial_sums.rb @@ -0,0 +1,31 @@ +n = 2_500_000 # (ARGV.shift || 1).to_i + +alt = 1.0 ; s0 = s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = 0.0 + +1.upto(n) do |d| + d = d.to_f ; d2 = d * d ; d3 = d2 * d ; ds = Math.sin(d) ; dc = Math.cos(d) + + s0 += (2.0 / 3.0) ** (d - 1.0) + s1 += 1.0 / Math.sqrt(d) + s2 += 1.0 / (d * (d + 1.0)) + s3 += 1.0 / (d3 * ds * ds) + s4 += 1.0 / (d3 * dc * dc) + s5 += 1.0 / d + s6 += 1.0 / d2 + s7 += alt / d + s8 += alt / (2.0 * d - 1.0) + + alt = -alt +end + +if false + printf("%.9f\t(2/3)^k\n", s0) + printf("%.9f\tk^-0.5\n", s1) + printf("%.9f\t1/k(k+1)\n", s2) + printf("%.9f\tFlint Hills\n", s3) + printf("%.9f\tCookson Hills\n", s4) + printf("%.9f\tHarmonic\n", s5) + printf("%.9f\tRiemann Zeta\n", s6) + printf("%.9f\tAlternating Harmonic\n", s7) + printf("%.9f\tGregory\n", s8) +end diff --git a/benchmark/bm_so_random.rb b/benchmark/bm_so_random.rb new file mode 100644 index 000000000..a66b9e8e6 --- /dev/null +++ b/benchmark/bm_so_random.rb @@ -0,0 +1,20 @@ +# from http://www.bagley.org/~doug/shootout/bench/random/random.ruby + +IM = 139968.0 +IA = 3877.0 +IC = 29573.0 + +$last = 42.0 + +def gen_random(max) + (max * ($last = ($last * IA + IC) % IM)) / IM +end + +N = 3_000_000 + +i = 0 +while i