From a96b871c463591b9e8d6f0fa541665dfc2769539 Mon Sep 17 00:00:00 2001 From: furunkel Date: Thu, 23 Apr 2015 22:27:19 +0200 Subject: Add task for running and plotting benchmarks --- .gitignore | 2 ++ Rakefile | 2 ++ benchmark/ao-render.rb | 4 +-- tasks/benchmark.rake | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ tasks/mruby_build.rake | 1 + 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tasks/benchmark.rake diff --git a/.gitignore b/.gitignore index 75f473258..3657b105d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ *.bak *.d *.o +benchmark/**/*.dat +benchmark/*.pdf *.orig *.pdb *.rej diff --git a/Rakefile b/Rakefile index fee43217c..66f54a4e2 100644 --- a/Rakefile +++ b/Rakefile @@ -29,6 +29,8 @@ load "#{MRUBY_ROOT}/tasks/libmruby.rake" load "#{MRUBY_ROOT}/tasks/mrbgems_test.rake" load "#{MRUBY_ROOT}/test/mrbtest.rake" +load "#{MRUBY_ROOT}/tasks/benchmark.rake" + ############################## # generic build targets, rules task :default => :all diff --git a/benchmark/ao-render.rb b/benchmark/ao-render.rb index 37bb4ad31..8212c3a13 100644 --- a/benchmark/ao-render.rb +++ b/benchmark/ao-render.rb @@ -5,8 +5,8 @@ # mruby version by Hideki Miura # -IMAGE_WIDTH = 256 -IMAGE_HEIGHT = 256 +IMAGE_WIDTH = 64 +IMAGE_HEIGHT = 64 NSUBSAMPLES = 2 NAO_SAMPLES = 8 diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake new file mode 100644 index 000000000..f682f8ee1 --- /dev/null +++ b/tasks/benchmark.rake @@ -0,0 +1,82 @@ +module MRuby + BENCHMARK_REPEAT = 2 +end + +$dat_files = [] + +def bm_files + Dir.glob("#{MRUBY_ROOT}/benchmark/*.rb") +end + +def plot_file + File.join(MRUBY_ROOT, 'benchmark', 'bm.pdf') +end + +def plot + opts_file = "#{MRUBY_ROOT}/benchmark/plot.gpl" + opts = File.read(opts_file).each_line.to_a.map(&:strip).join(';') + opts += ';plot ' + + dat_files = $dat_files.group_by {|f| File.dirname(f).split(File::SEPARATOR)[-1]} + + opts += dat_files.keys.map do |data_file| + %Q['-' u 2:3:4:xtic(1) w hist title columnheader(1)] + end.join(',') + opts += ';' + + cmd = %Q{gnuplot -p -e "#{opts}"} + + IO.popen(cmd, 'w') do |p| + dat_files.each do |target_name, bm_files| + p.puts target_name + bm_files.each do |bm_file| + puts target_name + p.write File.read(bm_file) + print File.read(bm_file) + end + p.puts "e" + end + end +end + + +MRuby.each_target do |target| + mruby_path = "#{target.build_dir}/bin/mruby" + if File.file? mruby_path + bm_files.each do |bm_file| + bm_name = File.basename bm_file, ".rb" + + dat_dir = File.join('benchmark', target.name) + dat_file = File.join(dat_dir, "#{bm_name}.dat") + $dat_files << dat_file + + directory dat_dir + + file dat_file => [bm_file, dat_dir] do |task| + print bm_name + puts "..." + + data = (0...MRuby::BENCHMARK_REPEAT).map do |n| + str = %x{(time -f "%e %S %U" #{mruby_path} #{bm_file}) 2>&1 >/dev/null} + str.split(' ').map(&:to_f) + end + + File.open(task.name, "w") do |f| + data = data.map {|_,r,s| (r + s) / 2.0} + min = data.min + max = data.max + avg = data.inject(&:+) / data.size + f.puts "#{bm_name.gsub('_', '-')} #{avg} #{min} #{max}" + end + end + end + end +end + +file plot_file => $dat_files do + plot +end + +task :benchmark => plot_file do + plot +end diff --git a/tasks/mruby_build.rake b/tasks/mruby_build.rake index 8438b6ca4..66608286d 100644 --- a/tasks/mruby_build.rake +++ b/tasks/mruby_build.rake @@ -8,6 +8,7 @@ module MRuby end def each_target(&block) + return to_enum(:each_target) if block.nil? @targets.each do |key, target| target.instance_eval(&block) end -- cgit v1.2.3 From 96d66f6d241374cd5b4423aab8e0a6289fdf697c Mon Sep 17 00:00:00 2001 From: furunkel Date: Thu, 23 Apr 2015 22:34:22 +0200 Subject: Don't echo to stdout --- tasks/benchmark.rake | 2 -- 1 file changed, 2 deletions(-) diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index f682f8ee1..c2f93d82d 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -30,9 +30,7 @@ def plot dat_files.each do |target_name, bm_files| p.puts target_name bm_files.each do |bm_file| - puts target_name p.write File.read(bm_file) - print File.read(bm_file) end p.puts "e" end -- cgit v1.2.3 From 465d7a23c89d385e1c419a7bd1546b0bf41bf097 Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 13:40:54 +0200 Subject: Add build files for benchmarkings; add mandelbrot benchmark --- benchmark/bm_so_mandelbrot.rb | 64 +++++++++++++++++++++++++++++++++++++ benchmark/build_config_boxing.rb | 28 ++++++++++++++++ benchmark/build_config_cc.rb | 13 ++++++++ benchmark/fib.rb | 7 ++++ benchmark/fib39.rb | 8 ----- benchmark/plot.gpl | 5 +++ tasks/benchmark.rake | 69 +++++++++++++++++++++++----------------- 7 files changed, 156 insertions(+), 38 deletions(-) create mode 100644 benchmark/bm_so_mandelbrot.rb create mode 100644 benchmark/build_config_boxing.rb create mode 100644 benchmark/build_config_cc.rb create mode 100644 benchmark/fib.rb delete mode 100644 benchmark/fib39.rb create mode 100644 benchmark/plot.gpl diff --git a/benchmark/bm_so_mandelbrot.rb b/benchmark/bm_so_mandelbrot.rb new file mode 100644 index 000000000..dd1d75ba1 --- /dev/null +++ b/benchmark/bm_so_mandelbrot.rb @@ -0,0 +1,64 @@ +# The Computer Language Benchmarks Game +# http://shootout.alioth.debian.org/ +# +# contributed by Karl von Laudermann +# modified by Jeremy Echols +size = 1000 # ARGV[0].to_i + +puts "P4\n#{size} #{size}" + +ITER = 49 # Iterations - 1 for easy for..in looping +LIMIT_SQUARED = 4.0 # Presquared limit + +byte_acc = 0 +bit_num = 0 + +count_size = size - 1 # Precomputed size for easy for..in looping +def id(x) + x +end + +# For..in loops are faster than .upto, .downto, .times, etc. +for y in 0..count_size + for x in 0..count_size + zr = 0.0 + zi = 0.0 + cr = (2.0*x/size)-1.5 + ci = (2.0*y/size)-1.0 + escape = false + + # To make use of the for..in code, we use a dummy variable, + # like one would in C + for dummy in 0..ITER + tr = zr*zr - zi*zi + cr + ti = 2*zr*zi + ci + zr, zi = tr, ti + + if (zr*zr+zi*zi) > LIMIT_SQUARED + escape = true + break + end + end + +# byte_acc = (byte_acc << 1) | (escape ? 0b0 : 0b1) + byte_acc = (byte_acc * 2) | (escape ? 0b0 : 0b1) +# byte_acc = (byte_acc * 2) + (escape ? 0b0 : 0b1) +# byte_acc = (byte_acc * 2) + 1 + + bit_num += 1 + + # Code is very similar for these cases, but using separate blocks + # ensures we skip the shifting when it's unnecessary, which is most cases. + if (bit_num == 8) +# print byte_acc.chr + byte_acc = 0 + bit_num = 0 + elsif (x == count_size) + byte_acc <<= (8 - bit_num) +# byte_acc = byte_acc << (8 - bit_num) +# print byte_acc.chr + byte_acc = 0 + bit_num = 0 + end + end +end diff --git a/benchmark/build_config_boxing.rb b/benchmark/build_config_boxing.rb new file mode 100644 index 000000000..b478c9005 --- /dev/null +++ b/benchmark/build_config_boxing.rb @@ -0,0 +1,28 @@ +MRuby::Build.new do |conf| + toolchain :gcc +end + +MRuby::Build.new('no_boxing') do |conf| + toolchain :gcc + + conf.gembox 'default' +end + +MRuby::Build.new('word_boxing') do |conf| + toolchain :gcc + + conf.gembox 'default' + conf.compilers.each do |c| + c.defines += %w(MRB_WORD_BOXING) + end +end + +MRuby::Build.new('nan_boxing') do |conf| + toolchain :gcc + + conf.gembox 'default' + conf.compilers.each do |c| + c.defines += %w(MRB_NAN_BOXING) + end +end + diff --git a/benchmark/build_config_cc.rb b/benchmark/build_config_cc.rb new file mode 100644 index 000000000..56d725bc7 --- /dev/null +++ b/benchmark/build_config_cc.rb @@ -0,0 +1,13 @@ +MRuby::Build.new do |conf| + toolchain :gcc +end + +MRuby::Build.new('gcc') do |conf| + toolchain :gcc + conf.gembox 'default' +end + +MRuby::Build.new('clang') do |conf| + toolchain :clang + conf.gembox 'default' +end diff --git a/benchmark/fib.rb b/benchmark/fib.rb new file mode 100644 index 000000000..4b395f9cc --- /dev/null +++ b/benchmark/fib.rb @@ -0,0 +1,7 @@ + +def fib n + return n if n < 2 + fib(n-2) + fib(n-1) +end + +puts fib(37) diff --git a/benchmark/fib39.rb b/benchmark/fib39.rb deleted file mode 100644 index d5565b779..000000000 --- a/benchmark/fib39.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Fib 39 - -def fib n - return n if n < 2 - fib(n-2) + fib(n-1) -end - -puts fib(39) diff --git a/benchmark/plot.gpl b/benchmark/plot.gpl new file mode 100644 index 000000000..639674d68 --- /dev/null +++ b/benchmark/plot.gpl @@ -0,0 +1,5 @@ +set yrange [0:] +set terminal pdf +set xtics rotate by -45 +set style histogram errorbars gap 2 lw 1 +set style fill solid border -1 diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index c2f93d82d..18802708e 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -1,5 +1,5 @@ module MRuby - BENCHMARK_REPEAT = 2 + BENCHMARK_REPEAT = 4 end $dat_files = [] @@ -15,10 +15,19 @@ end def plot opts_file = "#{MRUBY_ROOT}/benchmark/plot.gpl" opts = File.read(opts_file).each_line.to_a.map(&:strip).join(';') - opts += ';plot ' dat_files = $dat_files.group_by {|f| File.dirname(f).split(File::SEPARATOR)[-1]} + build_config_name = if ENV['MRUBY_CONFIG'] + File.basename(ENV['MRUBY_CONFIG'], '.rb').gsub('build_config_', '') + else + "bm" + end + + opts += ";set output '#{File.join(MRUBY_ROOT, 'benchmark', "#{build_config_name}.pdf")}'" + + opts += ';plot ' + opts += dat_files.keys.map do |data_file| %Q['-' u 2:3:4:xtic(1) w hist title columnheader(1)] end.join(',') @@ -28,7 +37,7 @@ def plot IO.popen(cmd, 'w') do |p| dat_files.each do |target_name, bm_files| - p.puts target_name + p.puts target_name.gsub('_', '-') bm_files.each do |bm_file| p.write File.read(bm_file) end @@ -39,33 +48,33 @@ end MRuby.each_target do |target| - mruby_path = "#{target.build_dir}/bin/mruby" - if File.file? mruby_path - bm_files.each do |bm_file| - bm_name = File.basename bm_file, ".rb" - - dat_dir = File.join('benchmark', target.name) - dat_file = File.join(dat_dir, "#{bm_name}.dat") - $dat_files << dat_file - - directory dat_dir - - file dat_file => [bm_file, dat_dir] do |task| - print bm_name - puts "..." - - data = (0...MRuby::BENCHMARK_REPEAT).map do |n| - str = %x{(time -f "%e %S %U" #{mruby_path} #{bm_file}) 2>&1 >/dev/null} - str.split(' ').map(&:to_f) - end - - File.open(task.name, "w") do |f| - data = data.map {|_,r,s| (r + s) / 2.0} - min = data.min - max = data.max - avg = data.inject(&:+) / data.size - f.puts "#{bm_name.gsub('_', '-')} #{avg} #{min} #{max}" - end + next if target.name == 'host' + mruby_bin = "#{target.build_dir}/bin/mruby" + + bm_files.each do |bm_file| + bm_name = File.basename bm_file, ".rb" + + dat_dir = File.join('benchmark', target.name) + dat_file = File.join(dat_dir, "#{bm_name}.dat") + $dat_files << dat_file + + directory dat_dir + + file dat_file => [bm_file, dat_dir, mruby_bin] do |task| + print bm_name + puts "..." + + data = (0...MRuby::BENCHMARK_REPEAT).map do |n| + str = %x{(time -f "%e %S %U" #{mruby_bin} #{bm_file}) 2>&1 >/dev/null} + str.split(' ').map(&:to_f) + end + + File.open(task.name, "w") do |f| + data = data.map {|_,r,s| (r + s) / 2.0} + min = data.min + max = data.max + avg = data.inject(&:+) / data.size + f.puts "#{bm_name.gsub('_', '-')} #{avg} #{min} #{max}" end end end -- cgit v1.2.3 From 8f5ec1a60ff5e93377fdfcfcf3dc141136a8660a Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 14:19:51 +0200 Subject: Let all benchmarks start with bm_ --- benchmark/bm_fib.rb | 7 +++++++ benchmark/fib.rb | 7 ------- tasks/benchmark.rake | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 benchmark/bm_fib.rb delete mode 100644 benchmark/fib.rb diff --git a/benchmark/bm_fib.rb b/benchmark/bm_fib.rb new file mode 100644 index 000000000..4b395f9cc --- /dev/null +++ b/benchmark/bm_fib.rb @@ -0,0 +1,7 @@ + +def fib n + return n if n < 2 + fib(n-2) + fib(n-1) +end + +puts fib(37) diff --git a/benchmark/fib.rb b/benchmark/fib.rb deleted file mode 100644 index 4b395f9cc..000000000 --- a/benchmark/fib.rb +++ /dev/null @@ -1,7 +0,0 @@ - -def fib n - return n if n < 2 - fib(n-2) + fib(n-1) -end - -puts fib(37) diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index 18802708e..3372c900a 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -5,7 +5,7 @@ end $dat_files = [] def bm_files - Dir.glob("#{MRUBY_ROOT}/benchmark/*.rb") + Dir.glob("#{MRUBY_ROOT}/benchmark/bm_*.rb") end def plot_file -- cgit v1.2.3 From 01ee28d89dfae7df819c87afbc2cb80936b1511b Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 14:22:43 +0200 Subject: Rename --- benchmark/ao-render.rb | 314 ---------------------------------------------- benchmark/bm_ao_render.rb | 314 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+), 314 deletions(-) delete mode 100644 benchmark/ao-render.rb create mode 100644 benchmark/bm_ao_render.rb diff --git a/benchmark/ao-render.rb b/benchmark/ao-render.rb deleted file mode 100644 index 8212c3a13..000000000 --- a/benchmark/ao-render.rb +++ /dev/null @@ -1,314 +0,0 @@ -# AO render benchmark -# Original program (C) Syoyo Fujita in Javascript (and other languages) -# https://code.google.com/p/aobench/ -# Ruby(yarv2llvm) version by Hideki Miura -# mruby version by Hideki Miura -# - -IMAGE_WIDTH = 64 -IMAGE_HEIGHT = 64 -NSUBSAMPLES = 2 -NAO_SAMPLES = 8 - -module Rand - # Use xorshift - @@x = 123456789 - @@y = 362436069 - @@z = 521288629 - @@w = 88675123 - BNUM = 1 << 29 - BNUMF = BNUM.to_f - def self.rand - x = @@x - t = x ^ ((x & 0xfffff) << 11) - w = @@w - @@x, @@y, @@z = @@y, @@z, w - w = @@w = (w ^ (w >> 19) ^ (t ^ (t >> 8))) - (w % BNUM) / BNUMF - end -end - -class Vec - def initialize(x, y, z) - @x = x - @y = y - @z = z - end - - def x=(v); @x = v; end - def y=(v); @y = v; end - def z=(v); @z = v; end - def x; @x; end - def y; @y; end - def z; @z; end - - def vadd(b) - Vec.new(@x + b.x, @y + b.y, @z + b.z) - end - - def vsub(b) - Vec.new(@x - b.x, @y - b.y, @z - b.z) - end - - def vcross(b) - Vec.new(@y * b.z - @z * b.y, - @z * b.x - @x * b.z, - @x * b.y - @y * b.x) - end - - def vdot(b) - r = @x * b.x + @y * b.y + @z * b.z - r - end - - def vlength - Math.sqrt(@x * @x + @y * @y + @z * @z) - end - - def vnormalize - len = vlength - v = Vec.new(@x, @y, @z) - if len > 1.0e-17 then - v.x = v.x / len - v.y = v.y / len - v.z = v.z / len - end - v - end -end - - -class Sphere - def initialize(center, radius) - @center = center - @radius = radius - end - - def center; @center; end - def radius; @radius; end - - def intersect(ray, isect) - rs = ray.org.vsub(@center) - b = rs.vdot(ray.dir) - c = rs.vdot(rs) - (@radius * @radius) - d = b * b - c - if d > 0.0 then - t = - b - Math.sqrt(d) - - if t > 0.0 and t < isect.t then - isect.t = t - isect.hit = true - isect.pl = Vec.new(ray.org.x + ray.dir.x * t, - ray.org.y + ray.dir.y * t, - ray.org.z + ray.dir.z * t) - n = isect.pl.vsub(@center) - isect.n = n.vnormalize - end - end - end -end - -class Plane - def initialize(p, n) - @p = p - @n = n - end - - def intersect(ray, isect) - d = -@p.vdot(@n) - v = ray.dir.vdot(@n) - v0 = v - if v < 0.0 then - v0 = -v - end - if v0 < 1.0e-17 then - return - end - - t = -(ray.org.vdot(@n) + d) / v - - if t > 0.0 and t < isect.t then - isect.hit = true - isect.t = t - isect.n = @n - isect.pl = Vec.new(ray.org.x + t * ray.dir.x, - ray.org.y + t * ray.dir.y, - ray.org.z + t * ray.dir.z) - end - end -end - -class Ray - def initialize(org, dir) - @org = org - @dir = dir - end - - def org; @org; end - def org=(v); @org = v; end - def dir; @dir; end - def dir=(v); @dir = v; end -end - -class Isect - def initialize - @t = 10000000.0 - @hit = false - @pl = Vec.new(0.0, 0.0, 0.0) - @n = Vec.new(0.0, 0.0, 0.0) - end - - def t; @t; end - def t=(v); @t = v; end - def hit; @hit; end - def hit=(v); @hit = v; end - def pl; @pl; end - def pl=(v); @pl = v; end - def n; @n; end - def n=(v); @n = v; end -end - -def clamp(f) - i = f * 255.5 - if i > 255.0 then - i = 255.0 - end - if i < 0.0 then - i = 0.0 - end - i.to_i -end - -def otherBasis(basis, n) - basis[2] = Vec.new(n.x, n.y, n.z) - basis[1] = Vec.new(0.0, 0.0, 0.0) - - if n.x < 0.6 and n.x > -0.6 then - basis[1].x = 1.0 - elsif n.y < 0.6 and n.y > -0.6 then - basis[1].y = 1.0 - elsif n.z < 0.6 and n.z > -0.6 then - basis[1].z = 1.0 - else - basis[1].x = 1.0 - end - - basis[0] = basis[1].vcross(basis[2]) - basis[0] = basis[0].vnormalize - - basis[1] = basis[2].vcross(basis[0]) - basis[1] = basis[1].vnormalize -end - -class Scene - def initialize - @spheres = Array.new - @spheres[0] = Sphere.new(Vec.new(-2.0, 0.0, -3.5), 0.5) - @spheres[1] = Sphere.new(Vec.new(-0.5, 0.0, -3.0), 0.5) - @spheres[2] = Sphere.new(Vec.new(1.0, 0.0, -2.2), 0.5) - @plane = Plane.new(Vec.new(0.0, -0.5, 0.0), Vec.new(0.0, 1.0, 0.0)) - end - - def ambient_occlusion(isect) - basis = Array.new(3) - otherBasis(basis, isect.n) - - ntheta = NAO_SAMPLES - nphi = NAO_SAMPLES - eps = 0.0001 - occlusion = 0.0 - - p0 = Vec.new(isect.pl.x + eps * isect.n.x, - isect.pl.y + eps * isect.n.y, - isect.pl.z + eps * isect.n.z) - nphi.times do |j| - ntheta.times do |i| - r = Rand::rand - phi = 2.0 * 3.14159265 * Rand::rand - x = Math.cos(phi) * Math.sqrt(1.0 - r) - y = Math.sin(phi) * Math.sqrt(1.0 - r) - z = Math.sqrt(r) - - rx = x * basis[0].x + y * basis[1].x + z * basis[2].x - ry = x * basis[0].y + y * basis[1].y + z * basis[2].y - rz = x * basis[0].z + y * basis[1].z + z * basis[2].z - - raydir = Vec.new(rx, ry, rz) - ray = Ray.new(p0, raydir) - - occisect = Isect.new - @spheres[0].intersect(ray, occisect) - @spheres[1].intersect(ray, occisect) - @spheres[2].intersect(ray, occisect) - @plane.intersect(ray, occisect) - if occisect.hit then - occlusion = occlusion + 1.0 - else - 0.0 - end - end - end - - occlusion = (ntheta.to_f * nphi.to_f - occlusion) / (ntheta.to_f * nphi.to_f) - Vec.new(occlusion, occlusion, occlusion) - end - - def render(w, h, nsubsamples) - cnt = 0 - nsf = nsubsamples.to_f - h.times do |y| - w.times do |x| - rad = Vec.new(0.0, 0.0, 0.0) - - # Subsmpling - nsubsamples.times do |v| - nsubsamples.times do |u| - cnt = cnt + 1 - wf = w.to_f - hf = h.to_f - xf = x.to_f - yf = y.to_f - uf = u.to_f - vf = v.to_f - - px = (xf + (uf / nsf) - (wf / 2.0)) / (wf / 2.0) - py = -(yf + (vf / nsf) - (hf / 2.0)) / (hf / 2.0) - - eye = Vec.new(px, py, -1.0).vnormalize - - ray = Ray.new(Vec.new(0.0, 0.0, 0.0), eye) - - isect = Isect.new - @spheres[0].intersect(ray, isect) - @spheres[1].intersect(ray, isect) - @spheres[2].intersect(ray, isect) - @plane.intersect(ray, isect) - if isect.hit then - col = ambient_occlusion(isect) - rad.x = rad.x + col.x - rad.y = rad.y + col.y - rad.z = rad.z + col.z - else - 0.0 - end - end - end - - r = rad.x / (nsf * nsf) - g = rad.y / (nsf * nsf) - b = rad.z / (nsf * nsf) - printf("%c", clamp(r)) - printf("%c", clamp(g)) - printf("%c", clamp(b)) - end - end - end -end - -# File.open("ao.ppm", "w") do |fp| - printf("P6\n") - printf("%d %d\n", IMAGE_WIDTH, IMAGE_HEIGHT) - printf("255\n", IMAGE_WIDTH, IMAGE_HEIGHT) - Scene.new.render(IMAGE_WIDTH, IMAGE_HEIGHT, NSUBSAMPLES) -# Scene.new.render(256, 256, 2) -# end diff --git a/benchmark/bm_ao_render.rb b/benchmark/bm_ao_render.rb new file mode 100644 index 000000000..8212c3a13 --- /dev/null +++ b/benchmark/bm_ao_render.rb @@ -0,0 +1,314 @@ +# AO render benchmark +# Original program (C) Syoyo Fujita in Javascript (and other languages) +# https://code.google.com/p/aobench/ +# Ruby(yarv2llvm) version by Hideki Miura +# mruby version by Hideki Miura +# + +IMAGE_WIDTH = 64 +IMAGE_HEIGHT = 64 +NSUBSAMPLES = 2 +NAO_SAMPLES = 8 + +module Rand + # Use xorshift + @@x = 123456789 + @@y = 362436069 + @@z = 521288629 + @@w = 88675123 + BNUM = 1 << 29 + BNUMF = BNUM.to_f + def self.rand + x = @@x + t = x ^ ((x & 0xfffff) << 11) + w = @@w + @@x, @@y, @@z = @@y, @@z, w + w = @@w = (w ^ (w >> 19) ^ (t ^ (t >> 8))) + (w % BNUM) / BNUMF + end +end + +class Vec + def initialize(x, y, z) + @x = x + @y = y + @z = z + end + + def x=(v); @x = v; end + def y=(v); @y = v; end + def z=(v); @z = v; end + def x; @x; end + def y; @y; end + def z; @z; end + + def vadd(b) + Vec.new(@x + b.x, @y + b.y, @z + b.z) + end + + def vsub(b) + Vec.new(@x - b.x, @y - b.y, @z - b.z) + end + + def vcross(b) + Vec.new(@y * b.z - @z * b.y, + @z * b.x - @x * b.z, + @x * b.y - @y * b.x) + end + + def vdot(b) + r = @x * b.x + @y * b.y + @z * b.z + r + end + + def vlength + Math.sqrt(@x * @x + @y * @y + @z * @z) + end + + def vnormalize + len = vlength + v = Vec.new(@x, @y, @z) + if len > 1.0e-17 then + v.x = v.x / len + v.y = v.y / len + v.z = v.z / len + end + v + end +end + + +class Sphere + def initialize(center, radius) + @center = center + @radius = radius + end + + def center; @center; end + def radius; @radius; end + + def intersect(ray, isect) + rs = ray.org.vsub(@center) + b = rs.vdot(ray.dir) + c = rs.vdot(rs) - (@radius * @radius) + d = b * b - c + if d > 0.0 then + t = - b - Math.sqrt(d) + + if t > 0.0 and t < isect.t then + isect.t = t + isect.hit = true + isect.pl = Vec.new(ray.org.x + ray.dir.x * t, + ray.org.y + ray.dir.y * t, + ray.org.z + ray.dir.z * t) + n = isect.pl.vsub(@center) + isect.n = n.vnormalize + end + end + end +end + +class Plane + def initialize(p, n) + @p = p + @n = n + end + + def intersect(ray, isect) + d = -@p.vdot(@n) + v = ray.dir.vdot(@n) + v0 = v + if v < 0.0 then + v0 = -v + end + if v0 < 1.0e-17 then + return + end + + t = -(ray.org.vdot(@n) + d) / v + + if t > 0.0 and t < isect.t then + isect.hit = true + isect.t = t + isect.n = @n + isect.pl = Vec.new(ray.org.x + t * ray.dir.x, + ray.org.y + t * ray.dir.y, + ray.org.z + t * ray.dir.z) + end + end +end + +class Ray + def initialize(org, dir) + @org = org + @dir = dir + end + + def org; @org; end + def org=(v); @org = v; end + def dir; @dir; end + def dir=(v); @dir = v; end +end + +class Isect + def initialize + @t = 10000000.0 + @hit = false + @pl = Vec.new(0.0, 0.0, 0.0) + @n = Vec.new(0.0, 0.0, 0.0) + end + + def t; @t; end + def t=(v); @t = v; end + def hit; @hit; end + def hit=(v); @hit = v; end + def pl; @pl; end + def pl=(v); @pl = v; end + def n; @n; end + def n=(v); @n = v; end +end + +def clamp(f) + i = f * 255.5 + if i > 255.0 then + i = 255.0 + end + if i < 0.0 then + i = 0.0 + end + i.to_i +end + +def otherBasis(basis, n) + basis[2] = Vec.new(n.x, n.y, n.z) + basis[1] = Vec.new(0.0, 0.0, 0.0) + + if n.x < 0.6 and n.x > -0.6 then + basis[1].x = 1.0 + elsif n.y < 0.6 and n.y > -0.6 then + basis[1].y = 1.0 + elsif n.z < 0.6 and n.z > -0.6 then + basis[1].z = 1.0 + else + basis[1].x = 1.0 + end + + basis[0] = basis[1].vcross(basis[2]) + basis[0] = basis[0].vnormalize + + basis[1] = basis[2].vcross(basis[0]) + basis[1] = basis[1].vnormalize +end + +class Scene + def initialize + @spheres = Array.new + @spheres[0] = Sphere.new(Vec.new(-2.0, 0.0, -3.5), 0.5) + @spheres[1] = Sphere.new(Vec.new(-0.5, 0.0, -3.0), 0.5) + @spheres[2] = Sphere.new(Vec.new(1.0, 0.0, -2.2), 0.5) + @plane = Plane.new(Vec.new(0.0, -0.5, 0.0), Vec.new(0.0, 1.0, 0.0)) + end + + def ambient_occlusion(isect) + basis = Array.new(3) + otherBasis(basis, isect.n) + + ntheta = NAO_SAMPLES + nphi = NAO_SAMPLES + eps = 0.0001 + occlusion = 0.0 + + p0 = Vec.new(isect.pl.x + eps * isect.n.x, + isect.pl.y + eps * isect.n.y, + isect.pl.z + eps * isect.n.z) + nphi.times do |j| + ntheta.times do |i| + r = Rand::rand + phi = 2.0 * 3.14159265 * Rand::rand + x = Math.cos(phi) * Math.sqrt(1.0 - r) + y = Math.sin(phi) * Math.sqrt(1.0 - r) + z = Math.sqrt(r) + + rx = x * basis[0].x + y * basis[1].x + z * basis[2].x + ry = x * basis[0].y + y * basis[1].y + z * basis[2].y + rz = x * basis[0].z + y * basis[1].z + z * basis[2].z + + raydir = Vec.new(rx, ry, rz) + ray = Ray.new(p0, raydir) + + occisect = Isect.new + @spheres[0].intersect(ray, occisect) + @spheres[1].intersect(ray, occisect) + @spheres[2].intersect(ray, occisect) + @plane.intersect(ray, occisect) + if occisect.hit then + occlusion = occlusion + 1.0 + else + 0.0 + end + end + end + + occlusion = (ntheta.to_f * nphi.to_f - occlusion) / (ntheta.to_f * nphi.to_f) + Vec.new(occlusion, occlusion, occlusion) + end + + def render(w, h, nsubsamples) + cnt = 0 + nsf = nsubsamples.to_f + h.times do |y| + w.times do |x| + rad = Vec.new(0.0, 0.0, 0.0) + + # Subsmpling + nsubsamples.times do |v| + nsubsamples.times do |u| + cnt = cnt + 1 + wf = w.to_f + hf = h.to_f + xf = x.to_f + yf = y.to_f + uf = u.to_f + vf = v.to_f + + px = (xf + (uf / nsf) - (wf / 2.0)) / (wf / 2.0) + py = -(yf + (vf / nsf) - (hf / 2.0)) / (hf / 2.0) + + eye = Vec.new(px, py, -1.0).vnormalize + + ray = Ray.new(Vec.new(0.0, 0.0, 0.0), eye) + + isect = Isect.new + @spheres[0].intersect(ray, isect) + @spheres[1].intersect(ray, isect) + @spheres[2].intersect(ray, isect) + @plane.intersect(ray, isect) + if isect.hit then + col = ambient_occlusion(isect) + rad.x = rad.x + col.x + rad.y = rad.y + col.y + rad.z = rad.z + col.z + else + 0.0 + end + end + end + + r = rad.x / (nsf * nsf) + g = rad.y / (nsf * nsf) + b = rad.z / (nsf * nsf) + printf("%c", clamp(r)) + printf("%c", clamp(g)) + printf("%c", clamp(b)) + end + end + end +end + +# File.open("ao.ppm", "w") do |fp| + printf("P6\n") + printf("%d %d\n", IMAGE_WIDTH, IMAGE_HEIGHT) + printf("255\n", IMAGE_WIDTH, IMAGE_HEIGHT) + Scene.new.render(IMAGE_WIDTH, IMAGE_HEIGHT, NSUBSAMPLES) +# Scene.new.render(256, 256, 2) +# end -- cgit v1.2.3 From bb1951a8b60cd290b6fff7b4dcc2fe52a69ff6d5 Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 14:54:35 +0200 Subject: Include name of current build config in data directory name --- tasks/benchmark.rake | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index 3372c900a..28e100fbb 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -12,18 +12,20 @@ def plot_file File.join(MRUBY_ROOT, 'benchmark', 'bm.pdf') end +def build_config_name + if ENV['MRUBY_CONFIG'] + File.basename(ENV['MRUBY_CONFIG'], '.rb').gsub('build_config_', '') + else + "build" + end +end + def plot opts_file = "#{MRUBY_ROOT}/benchmark/plot.gpl" opts = File.read(opts_file).each_line.to_a.map(&:strip).join(';') dat_files = $dat_files.group_by {|f| File.dirname(f).split(File::SEPARATOR)[-1]} - build_config_name = if ENV['MRUBY_CONFIG'] - File.basename(ENV['MRUBY_CONFIG'], '.rb').gsub('build_config_', '') - else - "bm" - end - opts += ";set output '#{File.join(MRUBY_ROOT, 'benchmark', "#{build_config_name}.pdf")}'" opts += ';plot ' @@ -54,7 +56,7 @@ MRuby.each_target do |target| bm_files.each do |bm_file| bm_name = File.basename bm_file, ".rb" - dat_dir = File.join('benchmark', target.name) + dat_dir = File.join('benchmark', "#{build_config_name}_#{target.name}") dat_file = File.join(dat_dir, "#{bm_name}.dat") $dat_files << dat_file -- cgit v1.2.3 From 00518e0a4b134c5eb18802ccb9c0d5672923b7b2 Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 16:24:31 +0200 Subject: Use separate build config directory for benchmark files --- tasks/benchmark.rake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index 28e100fbb..97b2e7448 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -37,6 +37,7 @@ def plot cmd = %Q{gnuplot -p -e "#{opts}"} + p cmd IO.popen(cmd, 'w') do |p| dat_files.each do |target_name, bm_files| p.puts target_name.gsub('_', '-') @@ -56,7 +57,7 @@ MRuby.each_target do |target| bm_files.each do |bm_file| bm_name = File.basename bm_file, ".rb" - dat_dir = File.join('benchmark', "#{build_config_name}_#{target.name}") + dat_dir = File.join('benchmark', build_config_name, target.name) dat_file = File.join(dat_dir, "#{bm_name}.dat") $dat_files << dat_file -- cgit v1.2.3 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 Date: Fri, 24 Apr 2015 21:58:27 +0200 Subject: Output PNG instead of PDF --- .gitignore | 1 + benchmark/plot.gpl | 2 +- tasks/benchmark.rake | 11 +++++------ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index e3f434432..1a8c5990c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.o /benchmark/**/*.dat /benchmark/*.pdf +/benchmark/*.png *.orig *.pdb *.rej diff --git a/benchmark/plot.gpl b/benchmark/plot.gpl index 21a55dd3d..725e2ec1c 100644 --- a/benchmark/plot.gpl +++ b/benchmark/plot.gpl @@ -1,5 +1,5 @@ set yrange [0:] -set terminal pdf font 'Sans, 3' +set terminal pngcairo font 'Sans, 8' lw 1 size 1400,1024 set xtics rotate by -45 set style histogram errorbars gap 2 lw 1 set style fill solid border -1 diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index 97b2e7448..84e69ebee 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -8,10 +8,6 @@ def bm_files Dir.glob("#{MRUBY_ROOT}/benchmark/bm_*.rb") end -def plot_file - File.join(MRUBY_ROOT, 'benchmark', 'bm.pdf') -end - def build_config_name if ENV['MRUBY_CONFIG'] File.basename(ENV['MRUBY_CONFIG'], '.rb').gsub('build_config_', '') @@ -20,13 +16,17 @@ def build_config_name end end +def plot_file + File.join(MRUBY_ROOT, 'benchmark', "#{build_config_name}.png") +end + def plot opts_file = "#{MRUBY_ROOT}/benchmark/plot.gpl" opts = File.read(opts_file).each_line.to_a.map(&:strip).join(';') dat_files = $dat_files.group_by {|f| File.dirname(f).split(File::SEPARATOR)[-1]} - opts += ";set output '#{File.join(MRUBY_ROOT, 'benchmark', "#{build_config_name}.pdf")}'" + opts += ";set output '#{plot_file}'" opts += ';plot ' @@ -37,7 +37,6 @@ def plot cmd = %Q{gnuplot -p -e "#{opts}"} - p cmd IO.popen(cmd, 'w') do |p| dat_files.each do |target_name, bm_files| p.puts target_name.gsub('_', '-') -- cgit v1.2.3 From ed03b17bb484db1ade3e1aa9260bd85c7166ce49 Mon Sep 17 00:00:00 2001 From: furunkel Date: Fri, 24 Apr 2015 22:10:11 +0200 Subject: Remove benchmarks not in mruby --- 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_mandelbrot.rb | 57 ---------------- 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 -------------- 15 files changed, 684 deletions(-) delete mode 100644 benchmark/bm_so_ackermann.rb delete mode 100644 benchmark/bm_so_array.rb delete mode 100644 benchmark/bm_so_binary_trees.rb delete mode 100644 benchmark/bm_so_concatenate.rb delete mode 100644 benchmark/bm_so_exception.rb delete mode 100644 benchmark/bm_so_mandelbrot.rb delete mode 100644 benchmark/bm_so_matrix.rb delete mode 100644 benchmark/bm_so_nbody.rb delete mode 100644 benchmark/bm_so_nested_loop.rb delete mode 100644 benchmark/bm_so_nsieve_bits.rb delete mode 100644 benchmark/bm_so_object.rb delete mode 100644 benchmark/bm_so_partial_sums.rb delete mode 100644 benchmark/bm_so_random.rb delete mode 100644 benchmark/bm_so_sieve.rb delete mode 100644 benchmark/bm_so_spectralnorm.rb diff --git a/benchmark/bm_so_ackermann.rb b/benchmark/bm_so_ackermann.rb deleted file mode 100644 index 7db5be905..000000000 --- a/benchmark/bm_so_ackermann.rb +++ /dev/null @@ -1,19 +0,0 @@ -#!/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 deleted file mode 100644 index 2b8fce8f9..000000000 --- a/benchmark/bm_so_array.rb +++ /dev/null @@ -1,23 +0,0 @@ -#!/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 deleted file mode 100644 index 8250fba31..000000000 --- a/benchmark/bm_so_binary_trees.rb +++ /dev/null @@ -1,62 +0,0 @@ -# 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 deleted file mode 100644 index 873214de7..000000000 --- a/benchmark/bm_so_concatenate.rb +++ /dev/null @@ -1,18 +0,0 @@ -#!/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 deleted file mode 100644 index deb003a59..000000000 --- a/benchmark/bm_so_exception.rb +++ /dev/null @@ -1,61 +0,0 @@ -#!/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_mandelbrot.rb b/benchmark/bm_so_mandelbrot.rb deleted file mode 100644 index 76331c64b..000000000 --- a/benchmark/bm_so_mandelbrot.rb +++ /dev/null @@ -1,57 +0,0 @@ -# The Computer Language Benchmarks Game -# http://shootout.alioth.debian.org/ -# -# contributed by Karl von Laudermann -# modified by Jeremy Echols - -size = 600 # ARGV[0].to_i - -puts "P4\n#{size} #{size}" - -ITER = 49 # Iterations - 1 for easy for..in looping -LIMIT_SQUARED = 4.0 # Presquared limit - -byte_acc = 0 -bit_num = 0 - -count_size = size - 1 # Precomputed size for easy for..in looping - -# For..in loops are faster than .upto, .downto, .times, etc. -for y in 0..count_size - for x in 0..count_size - zr = 0.0 - zi = 0.0 - cr = (2.0*x/size)-1.5 - ci = (2.0*y/size)-1.0 - escape = false - - # To make use of the for..in code, we use a dummy variable, - # like one would in C - for dummy in 0..ITER - tr = zr*zr - zi*zi + cr - ti = 2*zr*zi + ci - zr, zi = tr, ti - - if (zr*zr+zi*zi) > LIMIT_SQUARED - escape = true - break - end - end - - byte_acc = (byte_acc << 1) | (escape ? 0b0 : 0b1) - bit_num += 1 - - # Code is very similar for these cases, but using separate blocks - # ensures we skip the shifting when it's unnecessary, which is most cases. - if (bit_num == 8) - print byte_acc.chr - byte_acc = 0 - bit_num = 0 - elsif (x == count_size) - byte_acc <<= (8 - bit_num) - print byte_acc.chr - byte_acc = 0 - bit_num = 0 - end - end -end diff --git a/benchmark/bm_so_matrix.rb b/benchmark/bm_so_matrix.rb deleted file mode 100644 index e2c5c8e55..000000000 --- a/benchmark/bm_so_matrix.rb +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/ruby -# -*- mode: ruby -*- -# $Id: matrix-ruby.code,v 1.4 2004/11/13 07:42:14 bfulgham Exp $ -# http://www.bagley.org/~doug/shootout/ - -n = 60 #Integer(ARGV.shift || 1) - -size = 40 - -def mkmatrix(rows, cols) - count = 1 - mx = Array.new(rows) - (0 .. (rows - 1)).each do |bi| - row = Array.new(cols, 0) - (0 .. (cols - 1)).each do |j| - row[j] = count - count += 1 - end - mx[bi] = row - end - mx -end - -def mmult(rows, cols, m1, m2) - m3 = Array.new(rows) - (0 .. (rows - 1)).each do |bi| - row = Array.new(cols, 0) - (0 .. (cols - 1)).each do |j| - val = 0 - (0 .. (cols - 1)).each do |k| - val += m1.at(bi).at(k) * m2.at(k).at(j) - end - row[j] = val - end - m3[bi] = row - end - m3 -end - -m1 = mkmatrix(size, size) -m2 = mkmatrix(size, size) -mm = Array.new -n.times do - mm = mmult(size, size, m1, m2) -end -# puts "#{mm[0][0]} #{mm[2][3]} #{mm[3][2]} #{mm[4][4]}" - - diff --git a/benchmark/bm_so_nbody.rb b/benchmark/bm_so_nbody.rb deleted file mode 100644 index d6c5bb9e6..000000000 --- a/benchmark/bm_so_nbody.rb +++ /dev/null @@ -1,148 +0,0 @@ -# The Computer Language Shootout -# http://shootout.alioth.debian.org -# -# Optimized for Ruby by Jesse Millikan -# From version ported by Michael Neumann from the C gcc version, -# which was written by Christoph Bauer. - -SOLAR_MASS = 4 * Math::PI**2 -DAYS_PER_YEAR = 365.24 - -def _puts *args -end - -class Planet - attr_accessor :x, :y, :z, :vx, :vy, :vz, :mass - - def initialize(x, y, z, vx, vy, vz, mass) - @x, @y, @z = x, y, z - @vx, @vy, @vz = vx * DAYS_PER_YEAR, vy * DAYS_PER_YEAR, vz * DAYS_PER_YEAR - @mass = mass * SOLAR_MASS - end - - def move_from_i(bodies, nbodies, dt, i) - while i < nbodies - b2 = bodies[i] - dx = @x - b2.x - dy = @y - b2.y - dz = @z - b2.z - - distance = Math.sqrt(dx * dx + dy * dy + dz * dz) - mag = dt / (distance * distance * distance) - b_mass_mag, b2_mass_mag = @mass * mag, b2.mass * mag - - @vx -= dx * b2_mass_mag - @vy -= dy * b2_mass_mag - @vz -= dz * b2_mass_mag - b2.vx += dx * b_mass_mag - b2.vy += dy * b_mass_mag - b2.vz += dz * b_mass_mag - i += 1 - end - - @x += dt * @vx - @y += dt * @vy - @z += dt * @vz - end -end - -def energy(bodies) - e = 0.0 - nbodies = bodies.size - - for i in 0 ... nbodies - b = bodies[i] - e += 0.5 * b.mass * (b.vx * b.vx + b.vy * b.vy + b.vz * b.vz) - for j in (i + 1) ... nbodies - b2 = bodies[j] - dx = b.x - b2.x - dy = b.y - b2.y - dz = b.z - b2.z - distance = Math.sqrt(dx * dx + dy * dy + dz * dz) - e -= (b.mass * b2.mass) / distance - end - end - e -end - -def offset_momentum(bodies) - px, py, pz = 0.0, 0.0, 0.0 - - for b in bodies - m = b.mass - px += b.vx * m - py += b.vy * m - pz += b.vz * m - end - - b = bodies[0] - b.vx = - px / SOLAR_MASS - b.vy = - py / SOLAR_MASS - b.vz = - pz / SOLAR_MASS -end - -BODIES = [ - # sun - Planet.new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0), - - # jupiter - Planet.new( - 4.84143144246472090e+00, - -1.16032004402742839e+00, - -1.03622044471123109e-01, - 1.66007664274403694e-03, - 7.69901118419740425e-03, - -6.90460016972063023e-05, - 9.54791938424326609e-04), - - # saturn - Planet.new( - 8.34336671824457987e+00, - 4.12479856412430479e+00, - -4.03523417114321381e-01, - -2.76742510726862411e-03, - 4.99852801234917238e-03, - 2.30417297573763929e-05, - 2.85885980666130812e-04), - - # uranus - Planet.new( - 1.28943695621391310e+01, - -1.51111514016986312e+01, - -2.23307578892655734e-01, - 2.96460137564761618e-03, - 2.37847173959480950e-03, - -2.96589568540237556e-05, - 4.36624404335156298e-05), - - # neptune - Planet.new( - 1.53796971148509165e+01, - -2.59193146099879641e+01, - 1.79258772950371181e-01, - 2.68067772490389322e-03, - 1.62824170038242295e-03, - -9.51592254519715870e-05, - 5.15138902046611451e-05) -] - -init = 200_000 # ARGV[0] -n = Integer(init) - -offset_momentum(BODIES) - -puts "%.9f" % energy(BODIES) - -nbodies = BODIES.size -dt = 0.01 - -n.times do - i = 0 - while i < nbodies - b = BODIES[i] - b.move_from_i(BODIES, nbodies, dt, i + 1) - i += 1 - end -end - -puts "%.9f" % energy(BODIES) diff --git a/benchmark/bm_so_nested_loop.rb b/benchmark/bm_so_nested_loop.rb deleted file mode 100644 index a0513f8c4..000000000 --- a/benchmark/bm_so_nested_loop.rb +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/ruby -# -*- mode: ruby -*- -# $Id: nestedloop-ruby.code,v 1.4 2004/11/13 07:42:22 bfulgham Exp $ -# http://www.bagley.org/~doug/shootout/ -# from Avi Bryant - -n = 16 # Integer(ARGV.shift || 1) -x = 0 -n.times do - n.times do - n.times do - n.times do - n.times do - n.times do - x += 1 - end - end - end - end - end -end -# puts x - - diff --git a/benchmark/bm_so_nsieve_bits.rb b/benchmark/bm_so_nsieve_bits.rb deleted file mode 100644 index 6f958ee44..000000000 --- a/benchmark/bm_so_nsieve_bits.rb +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/ruby -#coding: us-ascii -# -# The Great Computer Language Shootout -# http://shootout.alioth.debian.org/ -# -# nsieve-bits in Ruby -# Contributed by Glenn Parker, March 2005 - -CharExponent = 3 -BitsPerChar = 1 << CharExponent -LowMask = BitsPerChar - 1 - -def sieve(m) - items = "\xFF" * ((m / BitsPerChar) + 1) - masks = "" - BitsPerChar.times do |b| - masks << (1 << b).chr - end - - count = 0 - pmax = m - 1 - 2.step(pmax, 1) do |p| - if items[p >> 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 deleted file mode 100644 index e8607c719..000000000 --- a/benchmark/bm_so_object.rb +++ /dev/null @@ -1,56 +0,0 @@ -#!/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 deleted file mode 100644 index 630b45cb8..000000000 --- a/benchmark/bm_so_partial_sums.rb +++ /dev/null @@ -1,31 +0,0 @@ -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 deleted file mode 100644 index a66b9e8e6..000000000 --- a/benchmark/bm_so_random.rb +++ /dev/null @@ -1,20 +0,0 @@ -# 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