diff options
author | k0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-07-08 17:36:26 +0000 |
---|---|---|
committer | k0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-07-08 17:36:26 +0000 |
commit | 929982ecea67dc60d4e555f8bacc72dd291b917b (patch) | |
tree | d0dbe8fb5b5431a6447216dd31390962a8b521cc /benchmark | |
parent | d933fe9b85846f3cab5161bc1dfd2f456810808f (diff) | |
download | ruby-929982ecea67dc60d4e555f8bacc72dd291b917b.tar.gz |
benchmark/*.yml: convert from benchmark/bm_*.rb
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63900 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'benchmark')
254 files changed, 3595 insertions, 3200 deletions
diff --git a/benchmark/app_answer.yml b/benchmark/app_answer.yml new file mode 100644 index 0000000000..ad6bf3be16 --- /dev/null +++ b/benchmark/app_answer.yml @@ -0,0 +1,18 @@ +benchmark: + app_answer: | + 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 + + def the_answer_to_life_the_universe_and_everything + (ack(3,7).to_s.split(//).inject(0){|s,x| s+x.to_i}.to_s + "2" ).to_i + end + + answer = the_answer_to_life_the_universe_and_everything +loop_count: 1 diff --git a/benchmark/app_aobench.yml b/benchmark/app_aobench.yml new file mode 100644 index 0000000000..5479d0a421 --- /dev/null +++ b/benchmark/app_aobench.yml @@ -0,0 +1,295 @@ +prelude: | + # 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 +benchmark: + app_aobench: | + # + + IMAGE_WIDTH = 256 + IMAGE_HEIGHT = 256 + NSUBSAMPLES = 2 + NAO_SAMPLES = 8 + + class Vec + def initialize(x, y, z) + @x = x + @y = y + @z = z + end + + attr_accessor :x, :y, :z + + 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) + @x * b.x + @y * b.y + @z * b.z + 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 + + attr_reader :center, :radius + + 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 + else + 0.0 + end + end + nil + 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 + nil + end + end + + class Ray + def initialize(org, dir) + @org = org + @dir = dir + end + + attr_accessor :org, :dir + 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 + + attr_accessor :t, :hit, :pl, :n + 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 + 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 + phi = 2.0 * 3.14159265 * 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) + + # Subsampling + 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 + 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 + nil + end + + nil + end + end + + alias printf_orig printf + def printf *args + 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) + # end + + undef printf + alias printf printf_orig +loop_count: 1 diff --git a/benchmark/app_erb.yml b/benchmark/app_erb.yml new file mode 100644 index 0000000000..df0630585f --- /dev/null +++ b/benchmark/app_erb.yml @@ -0,0 +1,28 @@ +benchmark: + app_erb: | + # + # Create many HTML strings with ERB. + # + + require 'erb' + + data = <<erb + <html> + <head> <%= title %> </head> + <body> + <h1> <%= title %> </h1> + <p> + <%= content %> + </p> + </body> + </html> + erb + + max = 15_000 + title = "hello world!" + content = "hello world!\n" * 10 + + max.times{ + ERB.new(data).result(binding) + } +loop_count: 1 diff --git a/benchmark/app_factorial.yml b/benchmark/app_factorial.yml new file mode 100644 index 0000000000..9db58e1a3c --- /dev/null +++ b/benchmark/app_factorial.yml @@ -0,0 +1,14 @@ +benchmark: + app_factorial: | + def fact(n) + if(n > 1) + n * fact(n-1) + else + 1 + end + end + + 100.times { + fact(5000) + } +loop_count: 1 diff --git a/benchmark/app_fib.yml b/benchmark/app_fib.yml new file mode 100644 index 0000000000..d1c6671e9d --- /dev/null +++ b/benchmark/app_fib.yml @@ -0,0 +1,12 @@ +benchmark: + app_fib: | + def fib n + if n < 3 + 1 + else + fib(n-1) + fib(n-2) + end + end + + fib(34) +loop_count: 1 diff --git a/benchmark/app_lc_fizzbuzz.yml b/benchmark/app_lc_fizzbuzz.yml new file mode 100644 index 0000000000..e17024c87b --- /dev/null +++ b/benchmark/app_lc_fizzbuzz.yml @@ -0,0 +1,55 @@ +benchmark: + app_lc_fizzbuzz: | + # + # FizzBuzz program using only lambda calculus + # + # This program is quoted from + # "Understanding Computation" by Tom Stuart + # http://computationbook.com/ + # + # You can understand why this program works fine by reading this book. + # + + solution = -> k { -> f { -> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> l { -> x { -> g { -> b { b }[-> p { p[-> x { -> y { x } }] }[l]][x][-> y { g[f[-> l { -> p { p[-> x { -> y { y } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][x][g]][-> l { -> p { p[-> x { -> y { x } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][y] }] } } } }][k][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> l { -> x { -> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[l][f[x]] } }] } }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[m][n]][-> x { -> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[f[-> n { -> p { -> x { p[n[p][x]] } } }[m]][n]][m][x] }][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]] } } }][-> p { -> x { p[x] } }][-> p { -> x { p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] } }]][-> n { -> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[x]]]]]]]]]]]]]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]][-> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[x]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]][-> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> n { -> l { -> x { -> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> l { -> x { -> g { -> b { b }[-> p { p[-> x { -> y { x } }] }[l]][x][-> y { g[f[-> l { -> p { p[-> x { -> y { y } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][x][g]][-> l { -> p { p[-> x { -> y { x } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][y] }] } } } }][l][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][x]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }] } }[-> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> x { f[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { -> n { -> p { -> x { p[n[p][x]] } } }[f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n]][x] }][-> p { -> x { x } }] } } }][n][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][x] }]][-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]] } }][n]]]] }] + + FIRST = -> l { LEFT[RIGHT[l]] } + IF = -> b { b } + LEFT = -> p { p[-> x { -> y { x } } ] } + RIGHT = -> p { p[-> x { -> y { y } } ] } + IS_EMPTY = LEFT + REST = -> l { RIGHT[RIGHT[l]] } + + def to_integer(proc) + proc[-> n { n + 1 }][0] + end + + def to_boolean(proc) + IF[proc][true][false] + end + + def to_array(proc) + array = [] + + until to_boolean(IS_EMPTY[proc]) + array.push(FIRST[proc]) + proc = REST[proc] + end + + array + end + + def to_char(c) + '0123456789BFiuz'.slice(to_integer(c)) + end + + def to_string(s) + to_array(s).map { |c| to_char(c) }.join + end + + answer = to_array(solution).map do |p| + to_string(p) + end + + answer_ary = answer.to_a + # puts answer_ary +loop_count: 1 diff --git a/benchmark/app_mandelbrot.yml b/benchmark/app_mandelbrot.yml new file mode 100644 index 0000000000..d0ec9db010 --- /dev/null +++ b/benchmark/app_mandelbrot.yml @@ -0,0 +1,25 @@ +benchmark: + app_mandelbrot: | + require 'complex' + + def mandelbrot? z + i = 0 + while i<100 + i += 1 + z = z * z + return false if z.abs > 2 + end + true + end + + ary = [] + + (0..1000).each{|dx| + (0..1000).each{|dy| + x = dx / 50.0 + y = dy / 50.0 + c = Complex(x, y) + ary << c if mandelbrot?(c) + } + } +loop_count: 1 diff --git a/benchmark/app_pentomino.yml b/benchmark/app_pentomino.yml new file mode 100644 index 0000000000..6f152b1423 --- /dev/null +++ b/benchmark/app_pentomino.yml @@ -0,0 +1,262 @@ +prelude: | + #!/usr/local/bin/ruby + # This program is contributed by Shin Nishiyama +benchmark: + app_pentomino: | + + + # modified by K.Sasada + + NP = 5 + ROW = 8 + NP + COL = 8 + + $p = [] + $b = [] + $no = 0 + + def piece(n, a, nb) + nb.each{|x| + a[n] = x + if n == NP-1 + $p << [a.sort] + else + nbc=nb.dup + [-ROW, -1, 1, ROW].each{|d| + if x+d > 0 and not a.include?(x+d) and not nbc.include?(x+d) + nbc << x+d + end + } + nbc.delete x + piece(n+1,a[0..n],nbc) + end + } + end + + def kikaku(a) + a.collect {|x| x - a[0]} + end + def ud(a) + kikaku(a.collect {|x| ((x+NP)%ROW)-ROW*((x+NP)/ROW) }.sort) + end + def rl(a) + kikaku(a.collect {|x| ROW*((x+NP)/ROW)+ROW-((x+NP)%ROW)}.sort) + end + def xy(a) + kikaku(a.collect {|x| ROW*((x+NP)%ROW) + (x+NP)/ROW }.sort) + end + + def mkpieces + piece(0,[],[0]) + $p.each do |a| + a0 = a[0] + a[1] = ud(a0) + a[2] = rl(a0) + a[3] = ud(rl(a0)) + a[4] = xy(a0) + a[5] = ud(xy(a0)) + a[6] = rl(xy(a0)) + a[7] = ud(rl(xy(a0))) + a.sort! + a.uniq! + end + $p.uniq!.sort! {|x,y| x[0] <=> y[0] } + end + + def mkboard + (0...ROW*COL).each{|i| + if i % ROW >= ROW-NP + $b[i] = -2 + else + $b[i] = -1 + end + $b[3*ROW+3]=$b[3*ROW+4]=$b[4*ROW+3]=$b[4*ROW+4]=-2 + } + end + + def pboard + return # skip print + print "No. #$no\n" + (0...COL).each{|i| + print "|" + (0...ROW-NP).each{|j| + x = $b[i*ROW+j] + if x < 0 + print "..|" + else + printf "%2d|",x+1 + end + } + print "\n" + } + print "\n" + end + + $pnum=[] + def setpiece(a,pos) + if a.length == $p.length then + $no += 1 + pboard + return + end + while $b[pos] != -1 + pos += 1 + end + ($pnum - a).each do |i| + $p[i].each do |x| + f = 0 + x.each{|s| + if $b[pos+s] != -1 + f=1 + break + end + } + if f == 0 then + x.each{|s| + $b[pos+s] = i + } + a << i + setpiece(a.dup, pos) + a.pop + x.each{|s| + $b[pos+s] = -1 + } + end + end + end + end + + mkpieces + mkboard + $p[4] = [$p[4][0]] + $pnum = (0...$p.length).to_a + setpiece([],0) + + + + # original + + NP = 5 + ROW = 8 + NP + COL = 8 + + $p = [] + $b = [] + $no = 0 + + def piece(n,a,nb) + for x in nb + a[n] = x + if n == NP-1 + $p << [a.sort] + else + nbc=nb.dup + for d in [-ROW, -1, 1, ROW] + if x+d > 0 and not a.include?(x+d) and not nbc.include?(x+d) + nbc << x+d + end + end + nbc.delete x + piece(n+1,a[0..n],nbc) + end + end + end + + def kikaku(a) + a.collect {|x| x - a[0]} + end + def ud(a) + kikaku(a.collect {|x| ((x+NP)%ROW)-ROW*((x+NP)/ROW) }.sort) + end + def rl(a) + kikaku(a.collect {|x| ROW*((x+NP)/ROW)+ROW-((x+NP)%ROW)}.sort) + end + def xy(a) + kikaku(a.collect {|x| ROW*((x+NP)%ROW) + (x+NP)/ROW }.sort) + end + + def mkpieces + piece(0,[],[0]) + $p.each do |a| + a0 = a[0] + a[1] = ud(a0) + a[2] = rl(a0) + a[3] = ud(rl(a0)) + a[4] = xy(a0) + a[5] = ud(xy(a0)) + a[6] = rl(xy(a0)) + a[7] = ud(rl(xy(a0))) + a.sort! + a.uniq! + end + $p.uniq!.sort! {|x,y| x[0] <=> y[0] } + end + + def mkboard + for i in 0...ROW*COL + if i % ROW >= ROW-NP + $b[i] = -2 + else + $b[i] = -1 + end + $b[3*ROW+3]=$b[3*ROW+4]=$b[4*ROW+3]=$b[4*ROW+4]=-2 + end + end + + def pboard + print "No. #$no\n" + for i in 0...COL + print "|" + for j in 0...ROW-NP + x = $b[i*ROW+j] + if x < 0 + print "..|" + else + printf "%2d|",x+1 + end + end + print "\n" + end + print "\n" + end + + $pnum=[] + def setpiece(a,pos) + if a.length == $p.length then + $no += 1 + pboard + return + end + while $b[pos] != -1 + pos += 1 + end + ($pnum - a).each do |i| + $p[i].each do |x| + f = 0 + for s in x do + if $b[pos+s] != -1 + f=1 + break + end + end + if f == 0 then + for s in x do + $b[pos+s] = i + end + a << i + setpiece(a.dup, pos) + a.pop + for s in x do + $b[pos+s] = -1 + end + end + end + end + end + + mkpieces + mkboard + $p[4] = [$p[4][0]] + $pnum = (0...$p.length).to_a + setpiece([],0) +loop_count: 1 diff --git a/benchmark/app_raise.yml b/benchmark/app_raise.yml new file mode 100644 index 0000000000..3b7826c34f --- /dev/null +++ b/benchmark/app_raise.yml @@ -0,0 +1,11 @@ +benchmark: + app_raise: | + i = 0 + while i<300000 + i += 1 + begin + raise + rescue + end + end +loop_count: 1 diff --git a/benchmark/app_strconcat.yml b/benchmark/app_strconcat.yml new file mode 100644 index 0000000000..2762bf4aa2 --- /dev/null +++ b/benchmark/app_strconcat.yml @@ -0,0 +1,8 @@ +benchmark: + app_strconcat: | + i = 0 + while i<2_000_000 + "#{1+1} #{1+1} #{1+1}" + i += 1 + end +loop_count: 1 diff --git a/benchmark/app_tak.yml b/benchmark/app_tak.yml new file mode 100644 index 0000000000..0b67558b32 --- /dev/null +++ b/benchmark/app_tak.yml @@ -0,0 +1,15 @@ +benchmark: + app_tak: | + + def tak x, y, z + unless y < x + z + else + tak( tak(x-1, y, z), + tak(y-1, z, x), + tak(z-1, x, y)) + end + end + + tak(18, 9, 0) +loop_count: 1 diff --git a/benchmark/app_tarai.yml b/benchmark/app_tarai.yml new file mode 100644 index 0000000000..1f53db68a0 --- /dev/null +++ b/benchmark/app_tarai.yml @@ -0,0 +1,13 @@ +benchmark: + app_tarai: | + def tarai( x, y, z ) + if x <= y + then y + else tarai(tarai(x-1, y, z), + tarai(y-1, z, x), + tarai(z-1, x, y)) + end + end + + tarai(12, 6, 0) +loop_count: 1 diff --git a/benchmark/app_uri.yml b/benchmark/app_uri.yml new file mode 100644 index 0000000000..1f7e49de51 --- /dev/null +++ b/benchmark/app_uri.yml @@ -0,0 +1,11 @@ +benchmark: + app_uri: | + require 'uri' + + 100_000.times{ + uri = URI.parse('http://www.ruby-lang.org') + uri.scheme + uri.host + uri.port + } +loop_count: 1 diff --git a/benchmark/array_sample_100k_10.yml b/benchmark/array_sample_100k_10.yml new file mode 100644 index 0000000000..043e2fdf1c --- /dev/null +++ b/benchmark/array_sample_100k_10.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k_10: | + arr = [*0...100000] + 10_000.times {arr.sample 10} +loop_count: 1 diff --git a/benchmark/array_sample_100k_11.yml b/benchmark/array_sample_100k_11.yml new file mode 100644 index 0000000000..0072c46930 --- /dev/null +++ b/benchmark/array_sample_100k_11.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k_11: | + arr = [*0...100000] + 10_000.times {arr.sample 11} +loop_count: 1 diff --git a/benchmark/array_sample_100k__100.yml b/benchmark/array_sample_100k__100.yml new file mode 100644 index 0000000000..93d25b3053 --- /dev/null +++ b/benchmark/array_sample_100k__100.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k__100: | + arr = [*0...100000] + 10_000.times {arr.sample 100} +loop_count: 1 diff --git a/benchmark/array_sample_100k__1k.yml b/benchmark/array_sample_100k__1k.yml new file mode 100644 index 0000000000..a6a66ef5dc --- /dev/null +++ b/benchmark/array_sample_100k__1k.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k__1k: | + arr = [*0...100000] + 10_000.times {arr.sample 1000} +loop_count: 1 diff --git a/benchmark/array_sample_100k__6k.yml b/benchmark/array_sample_100k__6k.yml new file mode 100644 index 0000000000..438a345b8f --- /dev/null +++ b/benchmark/array_sample_100k__6k.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k__6k: | + arr = [*0...100000] + 10_000.times {arr.sample 6000} +loop_count: 1 diff --git a/benchmark/array_sample_100k___10k.yml b/benchmark/array_sample_100k___10k.yml new file mode 100644 index 0000000000..13da9ccd83 --- /dev/null +++ b/benchmark/array_sample_100k___10k.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k___10k: | + arr = [*0...100000] + 10_000.times {arr.sample 10_000} +loop_count: 1 diff --git a/benchmark/array_sample_100k___50k.yml b/benchmark/array_sample_100k___50k.yml new file mode 100644 index 0000000000..579312dc3f --- /dev/null +++ b/benchmark/array_sample_100k___50k.yml @@ -0,0 +1,5 @@ +benchmark: + array_sample_100k___50k: | + arr = [*0...100000] + 10_000.times {arr.sample 50_000} +loop_count: 1 diff --git a/benchmark/array_shift.yml b/benchmark/array_shift.yml new file mode 100644 index 0000000000..713c542024 --- /dev/null +++ b/benchmark/array_shift.yml @@ -0,0 +1,17 @@ +benchmark: + array_shift: | + require 'benchmark' + + Benchmark.bm do |x| + [10_000,1_000_000,100_000_000].each do |n| + ary = Array.new(n,0) + GC.start + x.report("#{n}:shift"){ ary.shift } + (0..4).each do |i| + ary = Array.new(n,0) + GC.start + x.report("#{n}:shift(#{i})"){ ary.shift(i) } + end + end + end +loop_count: 1 diff --git a/benchmark/array_small_and.yml b/benchmark/array_small_and.yml new file mode 100644 index 0000000000..34d2e8ff58 --- /dev/null +++ b/benchmark/array_small_and.yml @@ -0,0 +1,20 @@ +benchmark: + array_small_and: | + MIN_SIZE = ENV.fetch('SMALL_ARRAY_MIN', 0).to_i + MAX_SIZE = ENV.fetch('SMALL_ARRAY_MAX', 16).to_i + ITERATIONS = ENV.fetch('SMALL_ARRAY_ITERATIONS', 100).to_i + + ARRAYS = (MIN_SIZE..MAX_SIZE).map do |size1| + (MIN_SIZE..MAX_SIZE).map do |size2| + [Array.new(size1) { rand(MAX_SIZE) }, Array.new(size2) { rand(MAX_SIZE) }] + end + end + + ITERATIONS.times do + ARRAYS.each do |group| + group.each do |arr1, arr2| + arr1 & arr2 + end + end + end +loop_count: 1 diff --git a/benchmark/array_small_diff.yml b/benchmark/array_small_diff.yml new file mode 100644 index 0000000000..64710e96bf --- /dev/null +++ b/benchmark/array_small_diff.yml @@ -0,0 +1,20 @@ +benchmark: + array_small_diff: | + MIN_SIZE = ENV.fetch('SMALL_ARRAY_MIN', 0).to_i + MAX_SIZE = ENV.fetch('SMALL_ARRAY_MAX', 16).to_i + ITERATIONS = ENV.fetch('SMALL_ARRAY_ITERATIONS', 100).to_i + + ARRAYS = (MIN_SIZE..MAX_SIZE).map do |size1| + (MIN_SIZE..MAX_SIZE).map do |size2| + [Array.new(size1) { rand(MAX_SIZE) }, Array.new(size2) { rand(MAX_SIZE) }] + end + end + + ITERATIONS.times do + ARRAYS.each do |group| + group.each do |arr1, arr2| + arr1 - arr2 + end + end + end +loop_count: 1 diff --git a/benchmark/array_small_or.yml b/benchmark/array_small_or.yml new file mode 100644 index 0000000000..9338bd5dbd --- /dev/null +++ b/benchmark/array_small_or.yml @@ -0,0 +1,20 @@ +benchmark: + array_small_or: | + MIN_SIZE = ENV.fetch('SMALL_ARRAY_MIN', 0).to_i + MAX_SIZE = ENV.fetch('SMALL_ARRAY_MAX', 16).to_i + ITERATIONS = ENV.fetch('SMALL_ARRAY_ITERATIONS', 100).to_i + + ARRAYS = (MIN_SIZE..MAX_SIZE).map do |size1| + (MIN_SIZE..MAX_SIZE).map do |size2| + [Array.new(size1) { rand(MAX_SIZE) }, Array.new(size2) { rand(MAX_SIZE) }] + end + end + + ITERATIONS.times do + ARRAYS.each do |group| + group.each do |arr1, arr2| + arr1 | arr2 + end + end + end +loop_count: 1 diff --git a/benchmark/array_sort_block.yml b/benchmark/array_sort_block.yml new file mode 100644 index 0000000000..775db90f4b --- /dev/null +++ b/benchmark/array_sort_block.yml @@ -0,0 +1,5 @@ +benchmark: + array_sort_block: | + ary = Array.new(1000) { rand(1000) } + 10000.times { ary.sort { |a, b| a <=> b } } +loop_count: 1 diff --git a/benchmark/array_sort_float.yml b/benchmark/array_sort_float.yml new file mode 100644 index 0000000000..2aa2bd5fec --- /dev/null +++ b/benchmark/array_sort_float.yml @@ -0,0 +1,5 @@ +benchmark: + array_sort_float: | + arr = Array.new(1000) { rand } + 10000.times { arr.sort } +loop_count: 1 diff --git a/benchmark/array_values_at_int.yml b/benchmark/array_values_at_int.yml new file mode 100644 index 0000000000..48bb0f7492 --- /dev/null +++ b/benchmark/array_values_at_int.yml @@ -0,0 +1,5 @@ +benchmark: + array_values_at_int: | + ary = Array.new(10000) {|i| i} + 100000.times { ary.values_at(500) } +loop_count: 1 diff --git a/benchmark/array_values_at_range.yml b/benchmark/array_values_at_range.yml new file mode 100644 index 0000000000..9de66261bb --- /dev/null +++ b/benchmark/array_values_at_range.yml @@ -0,0 +1,5 @@ +benchmark: + array_values_at_range: | + ary = Array.new(10000) {|i| i} + 100000.times { ary.values_at(1..2000) } +loop_count: 1 diff --git a/benchmark/bighash.yml b/benchmark/bighash.yml new file mode 100644 index 0000000000..65d74049e7 --- /dev/null +++ b/benchmark/bighash.yml @@ -0,0 +1,4 @@ +benchmark: + bighash: | + h = {}; 5000000.times {|n| h[n] = n } +loop_count: 1 diff --git a/benchmark/bm_app_answer.rb b/benchmark/bm_app_answer.rb deleted file mode 100644 index 3cd8a8fd37..0000000000 --- a/benchmark/bm_app_answer.rb +++ /dev/null @@ -1,15 +0,0 @@ -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 - -def the_answer_to_life_the_universe_and_everything - (ack(3,7).to_s.split(//).inject(0){|s,x| s+x.to_i}.to_s + "2" ).to_i -end - -answer = the_answer_to_life_the_universe_and_everything diff --git a/benchmark/bm_app_aobench.rb b/benchmark/bm_app_aobench.rb deleted file mode 100644 index 2bd6acfaf8..0000000000 --- a/benchmark/bm_app_aobench.rb +++ /dev/null @@ -1,291 +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 -# - -IMAGE_WIDTH = 256 -IMAGE_HEIGHT = 256 -NSUBSAMPLES = 2 -NAO_SAMPLES = 8 - -class Vec - def initialize(x, y, z) - @x = x - @y = y - @z = z - end - - attr_accessor :x, :y, :z - - 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) - @x * b.x + @y * b.y + @z * b.z - 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 - - attr_reader :center, :radius - - 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 - else - 0.0 - end - end - nil - 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 - nil - end -end - -class Ray - def initialize(org, dir) - @org = org - @dir = dir - end - - attr_accessor :org, :dir -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 - - attr_accessor :t, :hit, :pl, :n -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 - 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 - phi = 2.0 * 3.14159265 * 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) - - # Subsampling - 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 - 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 - nil - end - - nil - end -end - -alias printf_orig printf -def printf *args -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) -# end - -undef printf -alias printf printf_orig diff --git a/benchmark/bm_app_erb.rb b/benchmark/bm_app_erb.rb deleted file mode 100644 index 9a1cb2707f..0000000000 --- a/benchmark/bm_app_erb.rb +++ /dev/null @@ -1,25 +0,0 @@ -# -# Create many HTML strings with ERB. -# - -require 'erb' - -data = <<erb -<html> - <head> <%= title %> </head> - <body> - <h1> <%= title %> </h1> - <p> - <%= content %> - </p> - </body> -</html> -erb - -max = 15_000 -title = "hello world!" -content = "hello world!\n" * 10 - -max.times{ - ERB.new(data).result(binding) -} diff --git a/benchmark/bm_app_factorial.rb b/benchmark/bm_app_factorial.rb deleted file mode 100644 index 45f471dfdb..0000000000 --- a/benchmark/bm_app_factorial.rb +++ /dev/null @@ -1,11 +0,0 @@ -def fact(n) - if(n > 1) - n * fact(n-1) - else - 1 - end -end - -100.times { - fact(5000) -} diff --git a/benchmark/bm_app_fib.rb b/benchmark/bm_app_fib.rb deleted file mode 100644 index 34a7b2e725..0000000000 --- a/benchmark/bm_app_fib.rb +++ /dev/null @@ -1,10 +0,0 @@ -def fib n - if n < 3 - 1 - else - fib(n-1) + fib(n-2) - end -end - -fib(34) - diff --git a/benchmark/bm_app_lc_fizzbuzz.rb b/benchmark/bm_app_lc_fizzbuzz.rb deleted file mode 100644 index f09574bbeb..0000000000 --- a/benchmark/bm_app_lc_fizzbuzz.rb +++ /dev/null @@ -1,52 +0,0 @@ -# -# FizzBuzz program using only lambda calculus -# -# This program is quoted from -# "Understanding Computation" by Tom Stuart -# http://computationbook.com/ -# -# You can understand why this program works fine by reading this book. -# - -solution = -> k { -> f { -> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> l { -> x { -> g { -> b { b }[-> p { p[-> x { -> y { x } }] }[l]][x][-> y { g[f[-> l { -> p { p[-> x { -> y { y } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][x][g]][-> l { -> p { p[-> x { -> y { x } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][y] }] } } } }][k][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> l { -> x { -> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[l][f[x]] } }] } }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[m][n]][-> x { -> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[f[-> n { -> p { -> x { p[n[p][x]] } } }[m]][n]][m][x] }][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]] } } }][-> p { -> x { p[x] } }][-> p { -> x { p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] } }]][-> n { -> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[x]]]]]]]]]]]]]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]][-> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[x]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]][-> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> n { -> l { -> x { -> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> l { -> x { -> g { -> b { b }[-> p { p[-> x { -> y { x } }] }[l]][x][-> y { g[f[-> l { -> p { p[-> x { -> y { y } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][x][g]][-> l { -> p { p[-> x { -> y { x } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][y] }] } } } }][l][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][x]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }] } }[-> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> x { f[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { -> n { -> p { -> x { p[n[p][x]] } } }[f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n]][x] }][-> p { -> x { x } }] } } }][n][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][x] }]][-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> p { p[-> x { -> y { x } }] }[n[-> p { -> x { -> y { -> f { f[x][y] } } }[-> p { p[-> x { -> y { y } }] }[p]][-> n { -> p { -> x { p[n[p][x]] } } }[-> p { p[-> x { -> y { y } }] }[p]]] }][-> x { -> y { -> f { f[x][y] } } }[-> p { -> x { x } }][-> p { -> x { x } }]]] }][m] } }[m][n]][n][x] }][m] } } }][n][-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]] } }][n]]]] }] - -FIRST = -> l { LEFT[RIGHT[l]] } -IF = -> b { b } -LEFT = -> p { p[-> x { -> y { x } } ] } -RIGHT = -> p { p[-> x { -> y { y } } ] } -IS_EMPTY = LEFT -REST = -> l { RIGHT[RIGHT[l]] } - -def to_integer(proc) - proc[-> n { n + 1 }][0] -end - -def to_boolean(proc) - IF[proc][true][false] -end - -def to_array(proc) - array = [] - - until to_boolean(IS_EMPTY[proc]) - array.push(FIRST[proc]) - proc = REST[proc] - end - - array -end - -def to_char(c) - '0123456789BFiuz'.slice(to_integer(c)) -end - -def to_string(s) - to_array(s).map { |c| to_char(c) }.join -end - -answer = to_array(solution).map do |p| - to_string(p) -end - -answer_ary = answer.to_a -# puts answer_ary diff --git a/benchmark/bm_app_mandelbrot.rb b/benchmark/bm_app_mandelbrot.rb deleted file mode 100644 index 801b75e8e2..0000000000 --- a/benchmark/bm_app_mandelbrot.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'complex' - -def mandelbrot? z - i = 0 - while i<100 - i += 1 - z = z * z - return false if z.abs > 2 - end - true -end - -ary = [] - -(0..1000).each{|dx| - (0..1000).each{|dy| - x = dx / 50.0 - y = dy / 50.0 - c = Complex(x, y) - ary << c if mandelbrot?(c) - } -} - diff --git a/benchmark/bm_app_pentomino.rb b/benchmark/bm_app_pentomino.rb deleted file mode 100644 index 59c63f358e..0000000000 --- a/benchmark/bm_app_pentomino.rb +++ /dev/null @@ -1,259 +0,0 @@ -#!/usr/local/bin/ruby -# This program is contributed by Shin Nishiyama - - -# modified by K.Sasada - -NP = 5 -ROW = 8 + NP -COL = 8 - -$p = [] -$b = [] -$no = 0 - -def piece(n, a, nb) - nb.each{|x| - a[n] = x - if n == NP-1 - $p << [a.sort] - else - nbc=nb.dup - [-ROW, -1, 1, ROW].each{|d| - if x+d > 0 and not a.include?(x+d) and not nbc.include?(x+d) - nbc << x+d - end - } - nbc.delete x - piece(n+1,a[0..n],nbc) - end - } -end - -def kikaku(a) - a.collect {|x| x - a[0]} -end -def ud(a) - kikaku(a.collect {|x| ((x+NP)%ROW)-ROW*((x+NP)/ROW) }.sort) -end -def rl(a) - kikaku(a.collect {|x| ROW*((x+NP)/ROW)+ROW-((x+NP)%ROW)}.sort) -end -def xy(a) - kikaku(a.collect {|x| ROW*((x+NP)%ROW) + (x+NP)/ROW }.sort) -end - -def mkpieces - piece(0,[],[0]) - $p.each do |a| - a0 = a[0] - a[1] = ud(a0) - a[2] = rl(a0) - a[3] = ud(rl(a0)) - a[4] = xy(a0) - a[5] = ud(xy(a0)) - a[6] = rl(xy(a0)) - a[7] = ud(rl(xy(a0))) - a.sort! - a.uniq! - end - $p.uniq!.sort! {|x,y| x[0] <=> y[0] } -end - -def mkboard - (0...ROW*COL).each{|i| - if i % ROW >= ROW-NP - $b[i] = -2 - else - $b[i] = -1 - end - $b[3*ROW+3]=$b[3*ROW+4]=$b[4*ROW+3]=$b[4*ROW+4]=-2 - } -end - -def pboard - return # skip print - print "No. #$no\n" - (0...COL).each{|i| - print "|" - (0...ROW-NP).each{|j| - x = $b[i*ROW+j] - if x < 0 - print "..|" - else - printf "%2d|",x+1 - end - } - print "\n" - } - print "\n" -end - -$pnum=[] -def setpiece(a,pos) - if a.length == $p.length then - $no += 1 - pboard - return - end - while $b[pos] != -1 - pos += 1 - end - ($pnum - a).each do |i| - $p[i].each do |x| - f = 0 - x.each{|s| - if $b[pos+s] != -1 - f=1 - break - end - } - if f == 0 then - x.each{|s| - $b[pos+s] = i - } - a << i - setpiece(a.dup, pos) - a.pop - x.each{|s| - $b[pos+s] = -1 - } - end - end - end -end - -mkpieces -mkboard -$p[4] = [$p[4][0]] -$pnum = (0...$p.length).to_a -setpiece([],0) - - -__END__ - -# original - -NP = 5 -ROW = 8 + NP -COL = 8 - -$p = [] -$b = [] -$no = 0 - -def piece(n,a,nb) - for x in nb - a[n] = x - if n == NP-1 - $p << [a.sort] - else - nbc=nb.dup - for d in [-ROW, -1, 1, ROW] - if x+d > 0 and not a.include?(x+d) and not nbc.include?(x+d) - nbc << x+d - end - end - nbc.delete x - piece(n+1,a[0..n],nbc) - end - end -end - -def kikaku(a) - a.collect {|x| x - a[0]} -end -def ud(a) - kikaku(a.collect {|x| ((x+NP)%ROW)-ROW*((x+NP)/ROW) }.sort) -end -def rl(a) - kikaku(a.collect {|x| ROW*((x+NP)/ROW)+ROW-((x+NP)%ROW)}.sort) -end -def xy(a) - kikaku(a.collect {|x| ROW*((x+NP)%ROW) + (x+NP)/ROW }.sort) -end - -def mkpieces - piece(0,[],[0]) - $p.each do |a| - a0 = a[0] - a[1] = ud(a0) - a[2] = rl(a0) - a[3] = ud(rl(a0)) - a[4] = xy(a0) - a[5] = ud(xy(a0)) - a[6] = rl(xy(a0)) - a[7] = ud(rl(xy(a0))) - a.sort! - a.uniq! - end - $p.uniq!.sort! {|x,y| x[0] <=> y[0] } -end - -def mkboard - for i in 0...ROW*COL - if i % ROW >= ROW-NP - $b[i] = -2 - else - $b[i] = -1 - end - $b[3*ROW+3]=$b[3*ROW+4]=$b[4*ROW+3]=$b[4*ROW+4]=-2 - end -end - -def pboard - print "No. #$no\n" - for i in 0...COL - print "|" - for j in 0...ROW-NP - x = $b[i*ROW+j] - if x < 0 - print "..|" - else - printf "%2d|",x+1 - end - end - print "\n" - end - print "\n" -end - -$pnum=[] -def setpiece(a,pos) - if a.length == $p.length then - $no += 1 - pboard - return - end - while $b[pos] != -1 - pos += 1 - end - ($pnum - a).each do |i| - $p[i].each do |x| - f = 0 - for s in x do - if $b[pos+s] != -1 - f=1 - break - end - end - if f == 0 then - for s in x do - $b[pos+s] = i - end - a << i - setpiece(a.dup, pos) - a.pop - for s in x do - $b[pos+s] = -1 - end - end - end - end -end - -mkpieces -mkboard -$p[4] = [$p[4][0]] -$pnum = (0...$p.length).to_a -setpiece([],0) diff --git a/benchmark/bm_app_raise.rb b/benchmark/bm_app_raise.rb deleted file mode 100644 index 5db8f95d50..0000000000 --- a/benchmark/bm_app_raise.rb +++ /dev/null @@ -1,8 +0,0 @@ -i = 0 -while i<300000 - i += 1 - begin - raise - rescue - end -end diff --git a/benchmark/bm_app_strconcat.rb b/benchmark/bm_app_strconcat.rb deleted file mode 100644 index 7eed7c1aed..0000000000 --- a/benchmark/bm_app_strconcat.rb +++ /dev/null @@ -1,5 +0,0 @@ -i = 0 -while i<2_000_000 - "#{1+1} #{1+1} #{1+1}" - i += 1 -end diff --git a/benchmark/bm_app_tak.rb b/benchmark/bm_app_tak.rb deleted file mode 100644 index efe5380f4e..0000000000 --- a/benchmark/bm_app_tak.rb +++ /dev/null @@ -1,13 +0,0 @@ - -def tak x, y, z - unless y < x - z - else - tak( tak(x-1, y, z), - tak(y-1, z, x), - tak(z-1, x, y)) - end -end - -tak(18, 9, 0) - diff --git a/benchmark/bm_app_tarai.rb b/benchmark/bm_app_tarai.rb deleted file mode 100644 index 4c146f5ccf..0000000000 --- a/benchmark/bm_app_tarai.rb +++ /dev/null @@ -1,10 +0,0 @@ -def tarai( x, y, z ) - if x <= y - then y - else tarai(tarai(x-1, y, z), - tarai(y-1, z, x), - tarai(z-1, x, y)) - end -end - -tarai(12, 6, 0) diff --git a/benchmark/bm_app_uri.rb b/benchmark/bm_app_uri.rb deleted file mode 100644 index 586edfd5dc..0000000000 --- a/benchmark/bm_app_uri.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'uri' - -100_000.times{ - uri = URI.parse('http://www.ruby-lang.org') - uri.scheme - uri.host - uri.port -} diff --git a/benchmark/bm_array_sample_100k_10.rb b/benchmark/bm_array_sample_100k_10.rb deleted file mode 100644 index 5f41ecc32b..0000000000 --- a/benchmark/bm_array_sample_100k_10.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 10} diff --git a/benchmark/bm_array_sample_100k_11.rb b/benchmark/bm_array_sample_100k_11.rb deleted file mode 100644 index 18b1715319..0000000000 --- a/benchmark/bm_array_sample_100k_11.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 11} diff --git a/benchmark/bm_array_sample_100k__100.rb b/benchmark/bm_array_sample_100k__100.rb deleted file mode 100644 index 22863afe89..0000000000 --- a/benchmark/bm_array_sample_100k__100.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 100} diff --git a/benchmark/bm_array_sample_100k__1k.rb b/benchmark/bm_array_sample_100k__1k.rb deleted file mode 100644 index 4cd79e6c67..0000000000 --- a/benchmark/bm_array_sample_100k__1k.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 1000} diff --git a/benchmark/bm_array_sample_100k__6k.rb b/benchmark/bm_array_sample_100k__6k.rb deleted file mode 100644 index b3d264249e..0000000000 --- a/benchmark/bm_array_sample_100k__6k.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 6000} diff --git a/benchmark/bm_array_sample_100k___10k.rb b/benchmark/bm_array_sample_100k___10k.rb deleted file mode 100644 index 5dd55ec058..0000000000 --- a/benchmark/bm_array_sample_100k___10k.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 10_000} diff --git a/benchmark/bm_array_sample_100k___50k.rb b/benchmark/bm_array_sample_100k___50k.rb deleted file mode 100644 index 1506732c3c..0000000000 --- a/benchmark/bm_array_sample_100k___50k.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = [*0...100000] -10_000.times {arr.sample 50_000} diff --git a/benchmark/bm_array_shift.rb b/benchmark/bm_array_shift.rb deleted file mode 100644 index 798bb9e3f4..0000000000 --- a/benchmark/bm_array_shift.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'benchmark' - -Benchmark.bm do |x| - [10_000,1_000_000,100_000_000].each do |n| - ary = Array.new(n,0) - GC.start - x.report("#{n}:shift"){ ary.shift } - (0..4).each do |i| - ary = Array.new(n,0) - GC.start - x.report("#{n}:shift(#{i})"){ ary.shift(i) } - end - end -end diff --git a/benchmark/bm_array_small_and.rb b/benchmark/bm_array_small_and.rb deleted file mode 100644 index e53a6edae6..0000000000 --- a/benchmark/bm_array_small_and.rb +++ /dev/null @@ -1,17 +0,0 @@ -MIN_SIZE = ENV.fetch('SMALL_ARRAY_MIN', 0).to_i -MAX_SIZE = ENV.fetch('SMALL_ARRAY_MAX', 16).to_i -ITERATIONS = ENV.fetch('SMALL_ARRAY_ITERATIONS', 100).to_i - -ARRAYS = (MIN_SIZE..MAX_SIZE).map do |size1| - (MIN_SIZE..MAX_SIZE).map do |size2| - [Array.new(size1) { rand(MAX_SIZE) }, Array.new(size2) { rand(MAX_SIZE) }] - end -end - -ITERATIONS.times do - ARRAYS.each do |group| - group.each do |arr1, arr2| - arr1 & arr2 - end - end -end diff --git a/benchmark/bm_array_small_diff.rb b/benchmark/bm_array_small_diff.rb deleted file mode 100644 index 9661ee48db..0000000000 --- a/benchmark/bm_array_small_diff.rb +++ /dev/null @@ -1,17 +0,0 @@ -MIN_SIZE = ENV.fetch('SMALL_ARRAY_MIN', 0).to_i -MAX_SIZE = ENV.fetch('SMALL_ARRAY_MAX', 16).to_i -ITERATIONS = ENV.fetch('SMALL_ARRAY_ITERATIONS', 100).to_i - -ARRAYS = (MIN_SIZE..MAX_SIZE).map do |size1| - (MIN_SIZE..MAX_SIZE).map do |size2| - [Array.new(size1) { rand(MAX_SIZE) }, Array.new(size2) { rand(MAX_SIZE) }] - end -end - -ITERATIONS.times do - ARRAYS.each do |group| - group.each do |arr1, arr2| - arr1 - arr2 - end - end -end diff --git a/benchmark/bm_array_small_or.rb b/benchmark/bm_array_small_or.rb deleted file mode 100644 index c58b5fd1ff..0000000000 --- a/benchmark/bm_array_small_or.rb +++ /dev/null @@ -1,17 +0,0 @@ -MIN_SIZE = ENV.fetch('SMALL_ARRAY_MIN', 0).to_i -MAX_SIZE = ENV.fetch('SMALL_ARRAY_MAX', 16).to_i -ITERATIONS = ENV.fetch('SMALL_ARRAY_ITERATIONS', 100).to_i - -ARRAYS = (MIN_SIZE..MAX_SIZE).map do |size1| - (MIN_SIZE..MAX_SIZE).map do |size2| - [Array.new(size1) { rand(MAX_SIZE) }, Array.new(size2) { rand(MAX_SIZE) }] - end -end - -ITERATIONS.times do - ARRAYS.each do |group| - group.each do |arr1, arr2| - arr1 | arr2 - end - end -end diff --git a/benchmark/bm_array_sort_block.rb b/benchmark/bm_array_sort_block.rb deleted file mode 100644 index 3579786056..0000000000 --- a/benchmark/bm_array_sort_block.rb +++ /dev/null @@ -1,2 +0,0 @@ -ary = Array.new(1000) { rand(1000) } -10000.times { ary.sort { |a, b| a <=> b } } diff --git a/benchmark/bm_array_sort_float.rb b/benchmark/bm_array_sort_float.rb deleted file mode 100644 index 9a6e2f8bd2..0000000000 --- a/benchmark/bm_array_sort_float.rb +++ /dev/null @@ -1,2 +0,0 @@ -arr = Array.new(1000) { rand } -10000.times { arr.sort } diff --git a/benchmark/bm_array_values_at_int.rb b/benchmark/bm_array_values_at_int.rb deleted file mode 100644 index 6cb394cb9f..0000000000 --- a/benchmark/bm_array_values_at_int.rb +++ /dev/null @@ -1,2 +0,0 @@ -ary = Array.new(10000) {|i| i} -100000.times { ary.values_at(500) } diff --git a/benchmark/bm_array_values_at_range.rb b/benchmark/bm_array_values_at_range.rb deleted file mode 100644 index 5b53806d1c..0000000000 --- a/benchmark/bm_array_values_at_range.rb +++ /dev/null @@ -1,2 +0,0 @@ -ary = Array.new(10000) {|i| i} -100000.times { ary.values_at(1..2000) } diff --git a/benchmark/bm_bighash.rb b/benchmark/bm_bighash.rb deleted file mode 100644 index e2ad5a5c94..0000000000 --- a/benchmark/bm_bighash.rb +++ /dev/null @@ -1 +0,0 @@ -h = {}; 5000000.times {|n| h[n] = n } diff --git a/benchmark/bm_dir_empty_p.rb b/benchmark/bm_dir_empty_p.rb deleted file mode 100644 index 8329c757cf..0000000000 --- a/benchmark/bm_dir_empty_p.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'tmpdir' -max = 100_000 -Dir.mktmpdir('bm_dir_empty_p') do |dir| - max.times { Dir.empty?(dir) } -end diff --git a/benchmark/bm_erb_render.rb b/benchmark/bm_erb_render.rb deleted file mode 100644 index 7dc0816c43..0000000000 --- a/benchmark/bm_erb_render.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'erb' - -data = <<erb -<html> - <head> <%= title %> </head> - <body> - <h1> <%= title %> </h1> - <p> - <%= content %> - </p> - </body> -</html> -erb - -max = 1_500_000 -title = "hello world!" -content = "hello world!\n" * 10 - -src = "def self.render(title, content); #{ERB.new(data).src}; end" -mod = Module.new -mod.instance_eval(src, "(ERB)") - -max.times do - mod.render(title, content) -end diff --git a/benchmark/bm_file_chmod.rb b/benchmark/bm_file_chmod.rb deleted file mode 100644 index 1cd4760c9d..0000000000 --- a/benchmark/bm_file_chmod.rb +++ /dev/null @@ -1,9 +0,0 @@ -# chmod file -require 'tempfile' -max = 200_000 -tmp = Tempfile.new('chmod') -path = tmp.path -max.times do - File.chmod(0777, path) -end -tmp.close! diff --git a/benchmark/bm_file_rename.rb b/benchmark/bm_file_rename.rb deleted file mode 100644 index 3bf6a5ef35..0000000000 --- a/benchmark/bm_file_rename.rb +++ /dev/null @@ -1,11 +0,0 @@ -# rename file -require 'tempfile' - -max = 100_000 -tmp = [ Tempfile.new('rename-a'), Tempfile.new('rename-b') ] -a, b = tmp.map { |x| x.path } -max.times do - File.rename(a, b) - File.rename(b, a) -end -tmp.each { |t| t.close! } diff --git a/benchmark/bm_hash_aref_dsym.rb b/benchmark/bm_hash_aref_dsym.rb deleted file mode 100644 index af4f8c36d4..0000000000 --- a/benchmark/bm_hash_aref_dsym.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {} -syms = ('a'..'z').map { |s| s.to_sym } -syms.each { |s| h[s] = 1 } -200_000.times { syms.each { |s| h[s] } } diff --git a/benchmark/bm_hash_aref_dsym_long.rb b/benchmark/bm_hash_aref_dsym_long.rb deleted file mode 100644 index 9d7759379e..0000000000 --- a/benchmark/bm_hash_aref_dsym_long.rb +++ /dev/null @@ -1,21 +0,0 @@ -# [ruby-core:70129] [Bug #11396] -collection_size = 200000 -sample_size = 10000 - -values = (1..collection_size).to_a.map do |x| - "THIS IS A LONGER STRING THAT IS ALSO UNIQUE #{x}" -end - -symbol_hash = {} - -values.each do |x| - symbol_hash[x.to_sym] = 1 -end - -# use the same samples each time to minimize deviations -rng = Random.new(0) -symbol_sample_array = values.sample(sample_size, random: rng).map(&:to_sym) - -3000.times do - symbol_sample_array.each { |x| symbol_hash[x] } -end diff --git a/benchmark/bm_hash_aref_fix.rb b/benchmark/bm_hash_aref_fix.rb deleted file mode 100644 index 1346890582..0000000000 --- a/benchmark/bm_hash_aref_fix.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {} -nums = (1..26).to_a -nums.each { |i| h[i] = i } -200_000.times { nums.each { |s| h[s] } } diff --git a/benchmark/bm_hash_aref_flo.rb b/benchmark/bm_hash_aref_flo.rb deleted file mode 100644 index 2217274c82..0000000000 --- a/benchmark/bm_hash_aref_flo.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {} -strs = [*1..10000].map! {|i| i.fdiv(10)} -strs.each { |s| h[s] = s } -50.times { strs.each { |s| h[s] } } diff --git a/benchmark/bm_hash_aref_miss.rb b/benchmark/bm_hash_aref_miss.rb deleted file mode 100644 index b0913dd4bb..0000000000 --- a/benchmark/bm_hash_aref_miss.rb +++ /dev/null @@ -1,5 +0,0 @@ -h = {} -strs = ('a'..'z').to_a.map!(&:freeze) -strs.each { |s| h[s] = s } -strs = ('A'..'Z').to_a -200_000.times { strs.each { |s| h[s] } } diff --git a/benchmark/bm_hash_aref_str.rb b/benchmark/bm_hash_aref_str.rb deleted file mode 100644 index 19439b061b..0000000000 --- a/benchmark/bm_hash_aref_str.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {} -strs = ('a'..'z').to_a.map!(&:freeze) -strs.each { |s| h[s] = s } -200_000.times { strs.each { |s| h[s] } } diff --git a/benchmark/bm_hash_aref_sym.rb b/benchmark/bm_hash_aref_sym.rb deleted file mode 100644 index f75d163fe6..0000000000 --- a/benchmark/bm_hash_aref_sym.rb +++ /dev/null @@ -1,9 +0,0 @@ -h = {} -syms = ('a'..'z').to_a -begin - syms = eval("%i[#{syms.join(' ')}]") -rescue SyntaxError # <= 1.9.3 - syms.map!(&:to_sym) -end -syms.each { |s| h[s] = s } -200_000.times { syms.each { |s| h[s] } } diff --git a/benchmark/bm_hash_aref_sym_long.rb b/benchmark/bm_hash_aref_sym_long.rb deleted file mode 100644 index 9dab8df7be..0000000000 --- a/benchmark/bm_hash_aref_sym_long.rb +++ /dev/null @@ -1,13 +0,0 @@ -h = {} -syms = %w[puts warn syswrite write stat bacon lettuce tomato -some symbols in this array may already be interned others should not be -hash browns make good breakfast but not cooked using prime numbers -shift for division entries delete_if keys exist? -] -begin - syms = eval("%i[#{syms.join(' ')}]") -rescue SyntaxError # <= 1.9.3 - syms.map!(&:to_sym) -end -syms.each { |s| h[s] = s } -200_000.times { syms.each { |s| h[s] } } diff --git a/benchmark/bm_hash_flatten.rb b/benchmark/bm_hash_flatten.rb deleted file mode 100644 index e944aae9f2..0000000000 --- a/benchmark/bm_hash_flatten.rb +++ /dev/null @@ -1,9 +0,0 @@ -h = {} - -10000.times do |i| - h[i] = nil -end - -1000.times do - h.flatten -end diff --git a/benchmark/bm_hash_ident_flo.rb b/benchmark/bm_hash_ident_flo.rb deleted file mode 100644 index 0c7edfed3e..0000000000 --- a/benchmark/bm_hash_ident_flo.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {}.compare_by_identity -strs = (1..10000).to_a.map!(&:to_f) -strs.each { |s| h[s] = s } -50.times { strs.each { |s| h[s] } } diff --git a/benchmark/bm_hash_ident_num.rb b/benchmark/bm_hash_ident_num.rb deleted file mode 100644 index b226736c6f..0000000000 --- a/benchmark/bm_hash_ident_num.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {}.compare_by_identity -nums = (1..26).to_a -nums.each { |n| h[n] = n } -200_000.times { nums.each { |n| h[n] } } diff --git a/benchmark/bm_hash_ident_obj.rb b/benchmark/bm_hash_ident_obj.rb deleted file mode 100644 index 4b3b58edec..0000000000 --- a/benchmark/bm_hash_ident_obj.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {}.compare_by_identity -objs = 26.times.map { Object.new } -objs.each { |o| h[o] = o } -200_000.times { objs.each { |o| h[o] } } diff --git a/benchmark/bm_hash_ident_str.rb b/benchmark/bm_hash_ident_str.rb deleted file mode 100644 index 8582b38e31..0000000000 --- a/benchmark/bm_hash_ident_str.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {}.compare_by_identity -strs = ('a'..'z').to_a -strs.each { |s| h[s] = s } -200_000.times { strs.each { |s| h[s] } } diff --git a/benchmark/bm_hash_ident_sym.rb b/benchmark/bm_hash_ident_sym.rb deleted file mode 100644 index 4c81e3d28e..0000000000 --- a/benchmark/bm_hash_ident_sym.rb +++ /dev/null @@ -1,4 +0,0 @@ -h = {}.compare_by_identity -syms = ('a'..'z').to_a.map(&:to_sym) -syms.each { |s| h[s] = s } -200_000.times { syms.each { |s| h[s] } } diff --git a/benchmark/bm_hash_keys.rb b/benchmark/bm_hash_keys.rb deleted file mode 100644 index 6863cd01f9..0000000000 --- a/benchmark/bm_hash_keys.rb +++ /dev/null @@ -1,9 +0,0 @@ -h = {} - -10000.times do |i| - h[i] = nil -end - -5000.times do - h.keys -end diff --git a/benchmark/bm_hash_long.rb b/benchmark/bm_hash_long.rb deleted file mode 100644 index 03d9109602..0000000000 --- a/benchmark/bm_hash_long.rb +++ /dev/null @@ -1,4 +0,0 @@ -k1 = "Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong"; -k2 = "Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping"; -h = {k1 => 0, k2 => 0}; -3000000.times{|i| k = i % 2 ? k2 : k1; h [k] = h[k] + 1} diff --git a/benchmark/bm_hash_shift.rb b/benchmark/bm_hash_shift.rb deleted file mode 100644 index a645671a5b..0000000000 --- a/benchmark/bm_hash_shift.rb +++ /dev/null @@ -1,10 +0,0 @@ -h = {} - -10000.times do |i| - h[i] = nil -end - -50000.times do - k, v = h.shift - h[k] = v -end diff --git a/benchmark/bm_hash_shift_u16.rb b/benchmark/bm_hash_shift_u16.rb deleted file mode 100644 index ec800d0342..0000000000 --- a/benchmark/bm_hash_shift_u16.rb +++ /dev/null @@ -1,10 +0,0 @@ -h = {} - -(16384..65536).each do |i| - h[i] = nil -end - -300000.times do - k, v = h.shift - h[k] = v -end diff --git a/benchmark/bm_hash_shift_u24.rb b/benchmark/bm_hash_shift_u24.rb deleted file mode 100644 index de4e0fa696..0000000000 --- a/benchmark/bm_hash_shift_u24.rb +++ /dev/null @@ -1,10 +0,0 @@ -h = {} - -(0xff4000..0xffffff).each do |i| - h[i] = nil -end - -300000.times do - k, v = h.shift - h[k] = v -end diff --git a/benchmark/bm_hash_shift_u32.rb b/benchmark/bm_hash_shift_u32.rb deleted file mode 100644 index 656aa55583..0000000000 --- a/benchmark/bm_hash_shift_u32.rb +++ /dev/null @@ -1,10 +0,0 @@ -h = {} - -(0xffff4000..0xffffffff).each do |i| - h[i] = nil -end - -300000.times do - k, v = h.shift - h[k] = v -end diff --git a/benchmark/bm_hash_small2.rb b/benchmark/bm_hash_small2.rb deleted file mode 100644 index 45485d9c71..0000000000 --- a/benchmark/bm_hash_small2.rb +++ /dev/null @@ -1 +0,0 @@ -1000000.times.map{|i| a={}; 2.times{|j| a[j]=j}; a} diff --git a/benchmark/bm_hash_small4.rb b/benchmark/bm_hash_small4.rb deleted file mode 100644 index acd4084334..0000000000 --- a/benchmark/bm_hash_small4.rb +++ /dev/null @@ -1 +0,0 @@ -1000000.times.map{|i| a={}; 4.times{|j| a[j]=j}; a} diff --git a/benchmark/bm_hash_small8.rb b/benchmark/bm_hash_small8.rb deleted file mode 100644 index 9cffcc91b6..0000000000 --- a/benchmark/bm_hash_small8.rb +++ /dev/null @@ -1 +0,0 @@ -1000000.times.map{|i| a={}; 8.times{|j| a[j]=j}; a} diff --git a/benchmark/bm_hash_to_proc.rb b/benchmark/bm_hash_to_proc.rb deleted file mode 100644 index 2b675bf509..0000000000 --- a/benchmark/bm_hash_to_proc.rb +++ /dev/null @@ -1,9 +0,0 @@ -h = {} - -10000.times do |i| - h[i] = nil -end - -5000.times do |i| - [i].map(&h) -end diff --git a/benchmark/bm_hash_values.rb b/benchmark/bm_hash_values.rb deleted file mode 100644 index 069441302f..0000000000 --- a/benchmark/bm_hash_values.rb +++ /dev/null @@ -1,9 +0,0 @@ -h = {} - -10000.times do |i| - h[i] = nil -end - -5000.times do - h.values -end diff --git a/benchmark/bm_int_quo.rb b/benchmark/bm_int_quo.rb deleted file mode 100644 index e22a3f8c30..0000000000 --- a/benchmark/bm_int_quo.rb +++ /dev/null @@ -1 +0,0 @@ -5000000.times { 42.quo(3) } diff --git a/benchmark/bm_io_copy_stream_write.rb b/benchmark/bm_io_copy_stream_write.rb deleted file mode 100644 index 3fd87250a4..0000000000 --- a/benchmark/bm_io_copy_stream_write.rb +++ /dev/null @@ -1,24 +0,0 @@ -# The goal of this is to use a synthetic (non-IO) reader -# to trigger the read/write loop of IO.copy_stream, -# bypassing in-kernel mechanisms like sendfile for zero copy, -# so we wrap the /dev/zero IO object: - -class Zero - def initialize - @n = 100000 - @in = File.open('/dev/zero', 'rb') - end - - def read(len, buf) - return if (@n -= 1) == 0 - @in.read(len, buf) - end -end - -begin - src = Zero.new - dst = File.open(IO::NULL, 'wb') - n = IO.copy_stream(src, dst) -rescue Errno::ENOENT - # not *nix -end if IO.respond_to?(:copy_stream) && IO.const_defined?(:NULL) diff --git a/benchmark/bm_io_copy_stream_write_socket.rb b/benchmark/bm_io_copy_stream_write_socket.rb deleted file mode 100644 index 11f369bd0d..0000000000 --- a/benchmark/bm_io_copy_stream_write_socket.rb +++ /dev/null @@ -1,35 +0,0 @@ -# The goal of this is to use a synthetic (non-IO) reader -# to trigger the read/write loop of IO.copy_stream, -# bypassing in-kernel mechanisms like sendfile for zero copy, -# so we wrap the /dev/zero IO object: -class Zero - def initialize - @n = 100000 - @in = File.open('/dev/zero', 'rb') - end - - def read(len, buf) - return if (@n -= 1) == 0 - @in.read(len, buf) - end -end - -begin - require 'socket' - src = Zero.new - rd, wr = UNIXSocket.pair - pid = fork do - wr.close - buf = String.new - while rd.read(16384, buf) - end - end - rd.close - IO.copy_stream(src, wr) -rescue Errno::ENOENT, NotImplementedError, NameError - # not *nix: missing /dev/zero, fork, or UNIXSocket -rescue LoadError # no socket? -ensure - wr.close if wr - Process.waitpid(pid) if pid -end if IO.respond_to?(:copy_stream) diff --git a/benchmark/bm_io_file_create.rb b/benchmark/bm_io_file_create.rb deleted file mode 100644 index 2f205c1333..0000000000 --- a/benchmark/bm_io_file_create.rb +++ /dev/null @@ -1,13 +0,0 @@ -# -# Create files -# - -max = 200_000 -file = './tmpfile_of_bm_io_file_create' - -max.times{ - f = open(file, 'w') - f.close#(true) -} -File.unlink(file) - diff --git a/benchmark/bm_io_file_read.rb b/benchmark/bm_io_file_read.rb deleted file mode 100644 index b9e796ed30..0000000000 --- a/benchmark/bm_io_file_read.rb +++ /dev/null @@ -1,15 +0,0 @@ -# -# Seek and Read file. -# - -require 'tempfile' - -max = 200_000 -str = "Hello world! " * 1000 -f = Tempfile.new('yarv-benchmark') -f.write str - -max.times{ - f.seek 0 - f.read -} diff --git a/benchmark/bm_io_file_write.rb b/benchmark/bm_io_file_write.rb deleted file mode 100644 index aa1be0e5fe..0000000000 --- a/benchmark/bm_io_file_write.rb +++ /dev/null @@ -1,14 +0,0 @@ -# -# Seek and Write file. -# - -require 'tempfile' - -max = 200_000 -str = "Hello world! " * 1000 -f = Tempfile.new('yarv-benchmark') - -max.times{ - f.seek 0 - f.write str -} diff --git a/benchmark/bm_io_nonblock_noex.rb b/benchmark/bm_io_nonblock_noex.rb deleted file mode 100644 index da9357fdc6..0000000000 --- a/benchmark/bm_io_nonblock_noex.rb +++ /dev/null @@ -1,22 +0,0 @@ -nr = 1_000_000 -i = 0 -msg = '.' -buf = '.' -noex = { exception: false } -begin - r, w = IO.pipe - while i < nr - i += 1 - w.write_nonblock(msg, noex) - r.read_nonblock(1, buf, noex) - end -rescue ArgumentError # old Rubies - while i < nr - i += 1 - w.write_nonblock(msg) - r.read_nonblock(1, buf) - end -ensure - r.close - w.close -end diff --git a/benchmark/bm_io_nonblock_noex2.rb b/benchmark/bm_io_nonblock_noex2.rb deleted file mode 100644 index 56819d049b..0000000000 --- a/benchmark/bm_io_nonblock_noex2.rb +++ /dev/null @@ -1,21 +0,0 @@ -nr = 1_000_000 -i = 0 -msg = '.' -buf = '.' -begin - r, w = IO.pipe - while i < nr - i += 1 - w.write_nonblock(msg, exception: false) - r.read_nonblock(1, buf, exception: false) - end -rescue ArgumentError # old Rubies - while i < nr - i += 1 - w.write_nonblock(msg) - r.read_nonblock(1, buf) - end -ensure - r.close - w.close -end diff --git a/benchmark/bm_io_pipe_rw.rb b/benchmark/bm_io_pipe_rw.rb deleted file mode 100644 index 6862a8ae61..0000000000 --- a/benchmark/bm_io_pipe_rw.rb +++ /dev/null @@ -1,13 +0,0 @@ -# Measure uncontended GVL performance via read/write with 1:1 threading -# If we switch to M:N threading, this will benchmark something else... -r, w = IO.pipe -src = '0'.freeze -dst = String.new -i = 0 -while i < 1_000_000 - i += 1 - w.write(src) - r.read(1, dst) -end -w.close -r.close diff --git a/benchmark/bm_io_select.rb b/benchmark/bm_io_select.rb deleted file mode 100644 index 19248daeb1..0000000000 --- a/benchmark/bm_io_select.rb +++ /dev/null @@ -1,9 +0,0 @@ -# IO.select performance - -w = [ IO.pipe[1] ]; - -nr = 1000000 -nr.times { - IO.select nil, w -} - diff --git a/benchmark/bm_io_select2.rb b/benchmark/bm_io_select2.rb deleted file mode 100644 index 10e37d71b2..0000000000 --- a/benchmark/bm_io_select2.rb +++ /dev/null @@ -1,22 +0,0 @@ -# IO.select performance. worst case of single fd. - -ios = [] -nr = 1000000 -if defined?(Process::RLIMIT_NOFILE) - max = Process.getrlimit(Process::RLIMIT_NOFILE)[0] -else - max = 64 -end -puts "max fd: #{max} (results not apparent with <= 1024 max fd)" - -((max / 2) - 10).times do - ios.concat IO.pipe -end - -last = [ ios[-1] ] -puts "last IO: #{last[0].inspect}" - -nr.times do - IO.select nil, last -end - diff --git a/benchmark/bm_io_select3.rb b/benchmark/bm_io_select3.rb deleted file mode 100644 index 7d0ba1f092..0000000000 --- a/benchmark/bm_io_select3.rb +++ /dev/null @@ -1,21 +0,0 @@ -# IO.select performance. a lot of fd - -ios = [] -nr = 100 -if defined?(Process::RLIMIT_NOFILE) - max = Process.getrlimit(Process::RLIMIT_NOFILE)[0] -else - max = 64 -end -puts "max fd: #{max} (results not apparent with <= 1024 max fd)" - -(max - 10).times do - r, w = IO.pipe - r.close - ios.push w -end - -nr.times do - IO.select nil, ios -end - diff --git a/benchmark/bm_loop_for.rb b/benchmark/bm_loop_for.rb deleted file mode 100644 index 0fc4cc1511..0000000000 --- a/benchmark/bm_loop_for.rb +++ /dev/null @@ -1,3 +0,0 @@ -for i in 1..30_000_000 - # -end diff --git a/benchmark/bm_loop_generator.rb b/benchmark/bm_loop_generator.rb deleted file mode 100644 index d3375c744c..0000000000 --- a/benchmark/bm_loop_generator.rb +++ /dev/null @@ -1,14 +0,0 @@ -max = 600000 - -if defined? Fiber - gen = (1..max).each - loop do - gen.next - end -else - require 'generator' - gen = Generator.new((0..max)) - while gen.next? - gen.next - end -end diff --git a/benchmark/bm_loop_times.rb b/benchmark/bm_loop_times.rb deleted file mode 100644 index 521f72ad1a..0000000000 --- a/benchmark/bm_loop_times.rb +++ /dev/null @@ -1 +0,0 @@ -30_000_000.times{|e|} diff --git a/benchmark/bm_loop_whileloop.rb b/benchmark/bm_loop_whileloop.rb deleted file mode 100644 index 0072822c06..0000000000 --- a/benchmark/bm_loop_whileloop.rb +++ /dev/null @@ -1,4 +0,0 @@ -i = 0 -while i<30_000_000 # benchmark loop 1 - i += 1 -end diff --git a/benchmark/bm_loop_whileloop2.rb b/benchmark/bm_loop_whileloop2.rb deleted file mode 100644 index 47d02dffc4..0000000000 --- a/benchmark/bm_loop_whileloop2.rb +++ /dev/null @@ -1,4 +0,0 @@ -i = 0 -while i< 6_000_000 # benchmark loop 2 - i += 1 -end diff --git a/benchmark/bm_marshal_dump_flo.rb b/benchmark/bm_marshal_dump_flo.rb deleted file mode 100644 index 9b8d0c6afb..0000000000 --- a/benchmark/bm_marshal_dump_flo.rb +++ /dev/null @@ -1,2 +0,0 @@ -bug10761 = 10000.times.map { |x| x.to_f } -100.times { Marshal.dump(bug10761) } diff --git a/benchmark/bm_marshal_dump_load_geniv.rb b/benchmark/bm_marshal_dump_load_geniv.rb deleted file mode 100644 index 8252ad90fa..0000000000 --- a/benchmark/bm_marshal_dump_load_geniv.rb +++ /dev/null @@ -1,10 +0,0 @@ -a = '' -a.instance_eval do - @a = :a - @b = :b - @c = :c -end -100000.times do - a = Marshal.load(Marshal.dump(a)) -end -#p(a.instance_eval { @a == :a && @b == :b && @c == :c }) diff --git a/benchmark/bm_marshal_dump_load_time.rb b/benchmark/bm_marshal_dump_load_time.rb deleted file mode 100644 index e29743b791..0000000000 --- a/benchmark/bm_marshal_dump_load_time.rb +++ /dev/null @@ -1 +0,0 @@ -100000.times { Marshal.load(Marshal.dump(Time.now)) } diff --git a/benchmark/bm_securerandom.rb b/benchmark/bm_securerandom.rb deleted file mode 100644 index a082ea6d5b..0000000000 --- a/benchmark/bm_securerandom.rb +++ /dev/null @@ -1,5 +0,0 @@ -require "securerandom" - -20_0000.times do - SecureRandom.random_number(100) -end diff --git a/benchmark/bm_so_ackermann.rb b/benchmark/bm_so_ackermann.rb deleted file mode 100644 index 7db5be9050..0000000000 --- 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 2b8fce8f99..0000000000 --- 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 b1693e4109..0000000000 --- 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 = 16 # 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 873214de7c..0000000000 --- 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 deb003a594..0000000000 --- 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_fannkuch.rb b/benchmark/bm_so_fannkuch.rb deleted file mode 100644 index bac5ecd44c..0000000000 --- a/benchmark/bm_so_fannkuch.rb +++ /dev/null @@ -1,45 +0,0 @@ -# The Computer Language Shootout -# http://shootout.alioth.debian.org/ -# Contributed by Sokolov Yura -# Modified by Ryan Williams - -def fannkuch(n) - maxFlips, m, r, check = 0, n-1, n, 0 - count = (1..n).to_a - perm = (1..n).to_a - - while true - if check < 30 - puts "#{perm}" - check += 1 - end - - while r != 1 - count[r-1] = r - r -= 1 - end - - if perm[0] != 1 and perm[m] != n - perml = perm.clone #.dup - flips = 0 - while (k = perml.first ) != 1 - perml = perml.slice!(0, k).reverse + perml - flips += 1 - end - maxFlips = flips if flips > maxFlips - end - while true - if r==n then return maxFlips end - perm.insert r,perm.shift - break if (count[r] -= 1) > 0 - r += 1 - end - end -end - -def puts *args -end - -N = 9 # (ARGV[0] || 1).to_i -puts "Pfannkuchen(#{N}) = #{fannkuch(N)}" - diff --git a/benchmark/bm_so_fasta.rb b/benchmark/bm_so_fasta.rb deleted file mode 100644 index dcc6b39507..0000000000 --- a/benchmark/bm_so_fasta.rb +++ /dev/null @@ -1,81 +0,0 @@ -# The Computer Language Shootout -# http://shootout.alioth.debian.org/ -# Contributed by Sokolov Yura - -$last = 42.0 -def gen_random(max, im=139968, ia=3877, ic=29573) - (max * ($last = ($last * ia + ic) % im)) / im -end - -alu = - "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"+ - "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"+ - "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"+ - "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"+ - "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"+ - "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"+ - "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA" - -iub = [ - ["a", 0.27], - ["c", 0.12], - ["g", 0.12], - ["t", 0.27], - - ["B", 0.02], - ["D", 0.02], - ["H", 0.02], - ["K", 0.02], - ["M", 0.02], - ["N", 0.02], - ["R", 0.02], - ["S", 0.02], - ["V", 0.02], - ["W", 0.02], - ["Y", 0.02], -] -homosapiens = [ - ["a", 0.3029549426680], - ["c", 0.1979883004921], - ["g", 0.1975473066391], - ["t", 0.3015094502008], -] - -def make_repeat_fasta(id, desc, src, n) - puts ">#{id} #{desc}" - v = nil - width = 60 - l = src.length - s = src * ((n / l) + 1) - s.slice!(n, l) - puts(s.scan(/.{1,#{width}}/).join("\n")) -end - -def make_random_fasta(id, desc, table, n) - puts ">#{id} #{desc}" - rand, v = nil,nil - width = 60 - chunk = 1 * width - prob = 0.0 - table.each{|v| v[1]= (prob += v[1])} - for i in 1..(n/width) - puts((1..width).collect{ - rand = gen_random(1.0) - table.find{|v| v[1]>rand}[0] - }.join) - end - if n%width != 0 - puts((1..(n%width)).collect{ - rand = gen_random(1.0) - table.find{|v| v[1]>rand}[0] - }.join) - end -end - - -n = (ARGV[0] or 250_000).to_i - -make_repeat_fasta('ONE', 'Homo sapiens alu', alu, n*2) -make_random_fasta('TWO', 'IUB ambiguity codes', iub, n*3) -make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, n*5) - diff --git a/benchmark/bm_so_lists.rb b/benchmark/bm_so_lists.rb deleted file mode 100644 index e8f4a2a5f7..0000000000 --- a/benchmark/bm_so_lists.rb +++ /dev/null @@ -1,47 +0,0 @@ -#from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby - -NUM = 300 -SIZE = 10000 - -def test_lists() - # create a list of integers (Li1) from 1 to SIZE - li1 = (1..SIZE).to_a - # copy the list to li2 (not by individual items) - li2 = li1.dup - # remove each individual item from left side of li2 and - # append to right side of li3 (preserving order) - li3 = Array.new - while (not li2.empty?) - li3.push(li2.shift) - end - # li2 must now be empty - # remove each individual item from right side of li3 and - # append to right side of li2 (reversing list) - while (not li3.empty?) - li2.push(li3.pop) - end - # li3 must now be empty - # reverse li1 in place - li1.reverse! - # check that first item is now SIZE - if li1[0] != SIZE then - p "not SIZE" - 0 - else - # compare li1 and li2 for equality - if li1 != li2 then - return(0) - else - # return the length of the list - li1.length - end - end -end - -i = 0 -while i<NUM - i += 1 - result = test_lists() -end - -result diff --git a/benchmark/bm_so_mandelbrot.rb b/benchmark/bm_so_mandelbrot.rb deleted file mode 100644 index 76331c64b8..0000000000 --- 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 e2c5c8e559..0000000000 --- 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_meteor_contest.rb b/benchmark/bm_so_meteor_contest.rb deleted file mode 100755 index 8c136baa6c..0000000000 --- a/benchmark/bm_so_meteor_contest.rb +++ /dev/null @@ -1,563 +0,0 @@ -#!/usr/bin/env ruby -# -# The Computer Language Shootout -# http://shootout.alioth.debian.org -# contributed by Kevin Barnes (Ruby novice) - -# PROGRAM: the main body is at the bottom. -# 1) read about the problem here: http://www-128.ibm.com/developerworks/java/library/j-javaopt/ -# 2) see how I represent a board as a bitmask by reading the blank_board comments -# 3) read as your mental paths take you - -def print *args -end - -# class to represent all information about a particular rotation of a particular piece -class Rotation - # an array (by location) containing a bit mask for how the piece maps at the given location. - # if the rotation is invalid at that location the mask will contain false - attr_reader :start_masks - - # maps a direction to a relative location. these differ depending on whether it is an even or - # odd row being mapped from - @@rotation_even_adder = { :west => -1, :east => 1, :nw => -7, :ne => -6, :sw => 5, :se => 6 } - @@rotation_odd_adder = { :west => -1, :east => 1, :nw => -6, :ne => -5, :sw => 6, :se => 7 } - - def initialize( directions ) - @even_offsets, @odd_offsets = normalize_offsets( get_values( directions )) - - @even_mask = mask_for_offsets( @even_offsets) - @odd_mask = mask_for_offsets( @odd_offsets) - - @start_masks = Array.new(60) - - # create the rotational masks by placing the base mask at the location and seeing if - # 1) it overlaps the boundaries and 2) it produces a prunable board. if either of these - # is true the piece cannot be placed - 0.upto(59) do | offset | - mask = is_even(offset) ? (@even_mask << offset) : (@odd_mask << offset) - if (blank_board & mask == 0 && !prunable(blank_board | mask, 0, true)) then - imask = compute_required( mask, offset) - @start_masks[offset] = [ mask, imask, imask | mask ] - else - @start_masks[offset] = false - end - end - end - - def compute_required( mask, offset ) - board = blank_board - 0.upto(offset) { | i | board |= 1 << i } - board |= mask - return 0 if (!prunable(board | mask, offset)) - board = flood_fill(board,58) - count = 0 - imask = 0 - 0.upto(59) do | i | - if (board[i] == 0) then - imask |= (1 << i) - count += 1 - end - end - (count > 0 && count < 5) ? imask : 0 - end - - def flood_fill( board, location) - return board if (board[location] == 1) - board |= 1 << location - row, col = location.divmod(6) - board = flood_fill( board, location - 1) if (col > 0) - board = flood_fill( board, location + 1) if (col < 4) - if (row % 2 == 0) then - board = flood_fill( board, location - 7) if (col > 0 && row > 0) - board = flood_fill( board, location - 6) if (row > 0) - board = flood_fill( board, location + 6) if (row < 9) - board = flood_fill( board, location + 5) if (col > 0 && row < 9) - else - board = flood_fill( board, location - 5) if (col < 4 && row > 0) - board = flood_fill( board, location - 6) if (row > 0) - board = flood_fill( board, location + 6) if (row < 9) - board = flood_fill( board, location + 7) if (col < 4 && row < 9) - end - board - end - - # given a location, produces a list of relative locations covered by the piece at this rotation - def offsets( location) - if is_even( location) then - @even_offsets.collect { | value | value + location } - else - @odd_offsets.collect { | value | value + location } - end - end - - # returns a set of offsets relative to the top-left most piece of the rotation (by even or odd rows) - # this is hard to explain. imagine we have this partial board: - # 0 0 0 0 0 x [positions 0-5] - # 0 0 1 1 0 x [positions 6-11] - # 0 0 1 0 0 x [positions 12-17] - # 0 1 0 0 0 x [positions 18-23] - # 0 1 0 0 0 x [positions 24-29] - # 0 0 0 0 0 x [positions 30-35] - # ... - # The top-left of the piece is at position 8, the - # board would be passed as a set of positions (values array) containing [8,9,14,19,25] not necessarily in that - # sorted order. Since that array starts on an odd row, the offsets for an odd row are: [0,1,6,11,17] obtained - # by subtracting 8 from everything. Now imagine the piece shifted up and to the right so it's on an even row: - # 0 0 0 1 1 x [positions 0-5] - # 0 0 1 0 0 x [positions 6-11] - # 0 0 1 0 0 x [positions 12-17] - # 0 1 0 0 0 x [positions 18-23] - # 0 0 0 0 0 x [positions 24-29] - # 0 0 0 0 0 x [positions 30-35] - # ... - # Now the positions are [3,4,8,14,19] which after subtracting the lowest value (3) gives [0,1,5,11,16] thus, the - # offsets for this particular piece are (in even, odd order) [0,1,5,11,16],[0,1,6,11,17] which is what - # this function would return - def normalize_offsets( values) - min = values.min - even_min = is_even(min) - other_min = even_min ? min + 6 : min + 7 - other_values = values.collect do | value | - if is_even(value) then - value + 6 - other_min - else - value + 7 - other_min - end - end - values.collect! { | value | value - min } - - if even_min then - [values, other_values] - else - [other_values, values] - end - end - - # produce a bitmask representation of an array of offset locations - def mask_for_offsets( offsets ) - mask = 0 - offsets.each { | value | mask = mask + ( 1 << value ) } - mask - end - - # finds a "safe" position that a position as described by a list of directions can be placed - # without falling off any edge of the board. the values returned a location to place the first piece - # at so it will fit after making the described moves - def start_adjust( directions ) - south = east = 0; - directions.each do | direction | - east += 1 if ( direction == :sw || direction == :nw || direction == :west ) - south += 1 if ( direction == :nw || direction == :ne ) - end - south * 6 + east - end - - # given a set of directions places the piece (as defined by a set of directions) on the board at - # a location that will not take it off the edge - def get_values( directions ) - start = start_adjust(directions) - values = [ start ] - directions.each do | direction | - if (start % 12 >= 6) then - start += @@rotation_odd_adder[direction] - else - start += @@rotation_even_adder[direction] - end - values += [ start ] - end - - # some moves take you back to an existing location, we'll strip duplicates - values.uniq - end -end - -# describes a piece and caches information about its rotations to as to be efficient for iteration -# ATTRIBUTES: -# rotations -- all the rotations of the piece -# type -- a numeic "name" of the piece -# masks -- an array by location of all legal rotational masks (a n inner array) for that location -# placed -- the mask that this piece was last placed at (not a location, but the actual mask used) -class Piece - attr_reader :rotations, :type, :masks - attr_accessor :placed - - # transform hashes that change one direction into another when you either flip or rotate a set of directions - @@flip_converter = { :west => :west, :east => :east, :nw => :sw, :ne => :se, :sw => :nw, :se => :ne } - @@rotate_converter = { :west => :nw, :east => :se, :nw => :ne, :ne => :east, :sw => :west, :se => :sw } - - def initialize( directions, type ) - @type = type - @rotations = Array.new(); - @map = {} - - generate_rotations( directions ) - directions.collect! { | value | @@flip_converter[value] } - generate_rotations( directions ) - - # creates the masks AND a map that returns [location, rotation] for any given mask - # this is used when a board is found and we want to draw it, otherwise the map is unused - @masks = Array.new(); - 0.upto(59) do | i | - even = true - @masks[i] = @rotations.collect do | rotation | - mask = rotation.start_masks[i] - @map[mask[0]] = [ i, rotation ] if (mask) - mask || nil - end - @masks[i].compact! - end - end - - # rotates a set of directions through all six angles and adds a Rotation to the list for each one - def generate_rotations( directions ) - 6.times do - rotations.push( Rotation.new(directions)) - directions.collect! { | value | @@rotate_converter[value] } - end - end - - # given a board string, adds this piece to the board at whatever location/rotation - # important: the outbound board string is 5 wide, the normal location notation is six wide (padded) - def fill_string( board_string) - location, rotation = @map[@placed] - rotation.offsets(location).each do | offset | - row, col = offset.divmod(6) - board_string[ row*5 + col, 1 ] = @type.to_s - end - end -end - -# a blank bit board having this form: -# -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 0 0 0 0 0 1 -# 1 1 1 1 1 1 -# -# where left lest significant bit is the top left and the most significant is the lower right -# the actual board only consists of the 0 places, the 1 places are blockers to keep things from running -# off the edges or bottom -def blank_board - 0b111111100000100000100000100000100000100000100000100000100000100000 -end - -def full_board - 0b111111111111111111111111111111111111111111111111111111111111111111 -end - -# determines if a location (bit position) is in an even row -def is_even( location) - (location % 12) < 6 -end - -# support function that create three utility maps: -# $converter -- for each row an array that maps a five bit row (via array mapping) -# to the a five bit representation of the bits below it -# $bit_count -- maps a five bit row (via array mapping) to the number of 1s in the row -# @@new_regions -- maps a five bit row (via array mapping) to an array of "region" arrays -# a region array has three values the first is a mask of bits in the region, -# the second is the count of those bits and the third is identical to the first -# examples: -# 0b10010 => [ 0b01100, 2, 0b01100 ], [ 0b00001, 1, 0b00001] -# 0b01010 => [ 0b10000, 1, 0b10000 ], [ 0b00100, 1, 0b00100 ], [ 0b00001, 1, 0b00001] -# 0b10001 => [ 0b01110, 3, 0b01110 ] -def create_collector_support - odd_map = [0b11, 0b110, 0b1100, 0b11000, 0b10000] - even_map = [0b1, 0b11, 0b110, 0b1100, 0b11000] - - all_odds = Array.new(0b100000) - all_evens = Array.new(0b100000) - bit_counts = Array.new(0b100000) - new_regions = Array.new(0b100000) - 0.upto(0b11111) do | i | - bit_count = odd = even = 0 - 0.upto(4) do | bit | - if (i[bit] == 1) then - bit_count += 1 - odd |= odd_map[bit] - even |= even_map[bit] - end - end - all_odds[i] = odd - all_evens[i] = even - bit_counts[i] = bit_count - new_regions[i] = create_regions( i) - end - - $converter = [] - 10.times { | row | $converter.push((row % 2 == 0) ? all_evens : all_odds) } - $bit_counts = bit_counts - $regions = new_regions.collect { | set | set.collect { | value | [ value, bit_counts[value], value] } } -end - -# determines if a board is punable, meaning that there is no possibility that it -# can be filled up with pieces. A board is prunable if there is a grouping of unfilled spaces -# that are not a multiple of five. The following board is an example of a prunable board: -# 0 0 1 0 0 -# 0 1 0 0 0 -# 1 1 0 0 0 -# 0 1 0 0 0 -# 0 0 0 0 0 -# ... -# -# This board is prunable because the top left corner is only 3 bits in area, no piece will ever fit it -# parameters: -# board -- an initial bit board (6 bit padded rows, see blank_board for format) -# location -- starting location, everything above and to the left is already full -# slotting -- set to true only when testing initial pieces, when filling normally -# additional assumptions are possible -# -# Algorithm: -# The algorithm starts at the top row (as determined by location) and iterates a row at a time -# maintainng counts of active open areas (kept in the collector array) each collector contains -# three values at the start of an iteration: -# 0: mask of bits that would be adjacent to the collector in this row -# 1: the number of bits collected so far -# 2: a scratch space starting as zero, but used during the computation to represent -# the empty bits in the new row that are adjacent (position 0) -# The exact procedure is described in-code -def prunable( board, location, slotting = false) - collectors = [] - # loop across the rows - (location / 6).to_i.upto(9) do | row_on | - # obtain a set of regions representing the bits of the current row. - regions = $regions[(board >> (row_on * 6)) & 0b11111] - converter = $converter[row_on] - - # track the number of collectors at the start of the cycle so that - # we don't compute against newly created collectors, only existing collectors - initial_collector_count = collectors.length - - # loop against the regions. For each region of the row - # we will see if it connects to one or more existing collectors. - # if it connects to 1 collector, the bits from the region are added to the - # bits of the collector and the mask is placed in collector[2] - # If the region overlaps more than one collector then all the collectors - # it overlaps with are merged into the first one (the others are set to nil in the array) - # if NO collectors are found then the region is copied as a new collector - regions.each do | region | - collector_found = nil - region_mask = region[2] - initial_collector_count.times do | collector_num | - collector = collectors[collector_num] - if (collector) then - collector_mask = collector[0] - if (collector_mask & region_mask != 0) then - if (collector_found) then - collector_found[0] |= collector_mask - collector_found[1] += collector[1] - collector_found[2] |= collector[2] - collectors[collector_num] = nil - else - collector_found = collector - collector[1] += region[1] - collector[2] |= region_mask - end - end - end - end - if (collector_found == nil) then - collectors.push(Array.new(region)) - end - end - - # check the existing collectors, if any collector overlapped no bits in the region its [2] value will - # be zero. The size of any such reaason is tested if it is not a multiple of five true is returned since - # the board is prunable. if it is a multiple of five it is removed. - # Collector that are still active have a new adjacent value [0] set based n the matched bits - # and have [2] cleared out for the next cycle. - collectors.length.times do | collector_num | - collector = collectors[collector_num] - if (collector) then - if (collector[2] == 0) then - return true if (collector[1] % 5 != 0) - collectors[collector_num] = nil - else - # if a collector matches all bits in the row then we can return unprunable early for the - # following reasons: - # 1) there can be no more unavailable bits bince we fill from the top left downward - # 2) all previous regions have been closed or joined so only this region can fail - # 3) this region must be good since there can never be only 1 region that is nuot - # a multiple of five - # this rule only applies when filling normally, so we ignore the rule if we are "slotting" - # in pieces to see what configurations work for them (the only other time this algorithm is used). - return false if (collector[2] == 0b11111 && !slotting) - collector[0] = converter[collector[2]] - collector[2] = 0 - end - end - end - - # get rid of all the empty converters for the next round - collectors.compact! - end - return false if (collectors.length <= 1) # 1 collector or less and the region is fine - collectors.any? { | collector | (collector[1] % 5) != 0 } # more than 1 and we test them all for bad size -end - -# creates a region given a row mask. see prunable for what a "region" is -def create_regions( value ) - regions = [] - cur_region = 0 - 5.times do | bit | - if (value[bit] == 0) then - cur_region |= 1 << bit - else - if (cur_region != 0 ) then - regions.push( cur_region) - cur_region = 0; - end - end - end - regions.push(cur_region) if (cur_region != 0) - regions -end - -# find up to the counted number of solutions (or all solutions) and prints the final result -def find_all - find_top( 1) - find_top( 0) - print_results -end - -# show the board -def print_results - print "#{@boards_found} solutions found\n\n" - print_full_board( @min_board) - print "\n" - print_full_board( @max_board) - print "\n" -end - -# finds solutions. This special version of the main function is only used for the top level -# the reason for it is basically to force a particular ordering on how the rotations are tested for -# the first piece. It is called twice, first looking for placements of the odd rotations and then -# looking for placements of the even locations. -# -# WHY? -# Since any found solution has an inverse we want to maximize finding solutions that are not already found -# as an inverse. The inverse will ALWAYS be 3 one of the piece configurations that is exactly 3 rotations away -# (an odd number). Checking even vs odd then produces a higher probability of finding more pieces earlier -# in the cycle. We still need to keep checking all the permutations, but our probability of finding one will -# diminsh over time. Since we are TOLD how many to search for this lets us exit before checking all pieces -# this bennifit is very great when seeking small numbers of solutions and is 0 when looking for more than the -# maximum number -def find_top( rotation_skip) - board = blank_board - (@pieces.length-1).times do - piece = @pieces.shift - piece.masks[0].each do | mask, imask, cmask | - if ((rotation_skip += 1) % 2 == 0) then - piece.placed = mask - find( 1, 1, board | mask) - end - end - @pieces.push(piece) - end - piece = @pieces.shift - @pieces.push(piece) -end - -# the normail find routine, iterates through the available pieces, checks all rotations at the current location -# and adds any boards found. depth is achieved via recursion. the overall approach is described -# here: http://www-128.ibm.com/developerworks/java/library/j-javaopt/ -# parameters: -# start_location -- where to start looking for place for the next piece at -# placed -- number of pieces placed -# board -- current state of the board -# -# see in-code comments -def find( start_location, placed, board) - # find the next location to place a piece by looking for an empty bit - while board[start_location] == 1 - start_location += 1 - end - - @pieces.length.times do - piece = @pieces.shift - piece.masks[start_location].each do | mask, imask, cmask | - if ( board & cmask == imask) then - piece.placed = mask - if (placed == 9) then - add_board - else - find( start_location + 1, placed + 1, board | mask) - end - end - end - @pieces.push(piece) - end -end - -# print the board -def print_full_board( board_string) - 10.times do | row | - print " " if (row % 2 == 1) - 5.times do | col | - print "#{board_string[row*5 + col,1]} " - end - print "\n" - end -end - -# when a board is found we "draw it" into a string and then flip that string, adding both to -# the list (hash) of solutions if they are unique. -def add_board - board_string = "99999999999999999999999999999999999999999999999999" - @all_pieces.each { | piece | piece.fill_string( board_string ) } - save( board_string) - save( board_string.reverse) -end - -# adds a board string to the list (if new) and updates the current best/worst board -def save( board_string) - if (@all_boards[board_string] == nil) then - @min_board = board_string if (board_string < @min_board) - @max_board = board_string if (board_string > @max_board) - @all_boards.store(board_string,true) - @boards_found += 1 - - # the exit motif is a time saver. Ideally the function should return, but those tests - # take noticeable time (performance). - if (@boards_found == @stop_count) then - print_results - exit(0) - end - end -end - - -## -## MAIN BODY :) -## -create_collector_support -@pieces = [ - Piece.new( [ :nw, :ne, :east, :east ], 2), - Piece.new( [ :ne, :se, :east, :ne ], 7), - Piece.new( [ :ne, :east, :ne, :nw ], 1), - Piece.new( [ :east, :sw, :sw, :se ], 6), - Piece.new( [ :east, :ne, :se, :ne ], 5), - Piece.new( [ :east, :east, :east, :se ], 0), - Piece.new( [ :ne, :nw, :se, :east, :se ], 4), - Piece.new( [ :se, :se, :se, :west ], 9), - Piece.new( [ :se, :se, :east, :se ], 8), - Piece.new( [ :east, :east, :sw, :se ], 3) - ]; - -@all_pieces = Array.new( @pieces) - -@min_board = "99999999999999999999999999999999999999999999999999" -@max_board = "00000000000000000000000000000000000000000000000000" -@stop_count = ARGV[0].to_i || 2089 -@all_boards = {} -@boards_found = 0 - -find_all ######## DO IT!!! diff --git a/benchmark/bm_so_nbody.rb b/benchmark/bm_so_nbody.rb deleted file mode 100644 index d6c5bb9e61..0000000000 --- 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 a0513f8c47..0000000000 --- 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.rb b/benchmark/bm_so_nsieve.rb deleted file mode 100644 index a65cc78233..0000000000 --- a/benchmark/bm_so_nsieve.rb +++ /dev/null @@ -1,35 +0,0 @@ -# The Computer Language Shootout -# http://shootout.alioth.debian.org/ -# -# contributed by Glenn Parker, March 2005 -# modified by Evan Phoenix, Sept 2006 - -def sieve(m) - flags = Flags.dup[0,m] - count = 0 - pmax = m - 1 - p = 2 - while p <= pmax - unless flags[p].zero? - count += 1 - mult = p - while mult <= pmax - flags[mult] = 0 - mult += p - end - end - p += 1 - end - count -end - -n = 9 # (ARGV[0] || 2).to_i -Flags = ("\x1" * ( 2 ** n * 10_000)).unpack("c*") - -n.downto(n-2) do |exponent| - break if exponent < 0 - m = (1 << exponent) * 10_000 - # m = (2 ** exponent) * 10_000 - count = sieve(m) - printf "Primes up to %8d %8d\n", m, count -end diff --git a/benchmark/bm_so_nsieve_bits.rb b/benchmark/bm_so_nsieve_bits.rb deleted file mode 100644 index 6f958ee44e..0000000000 --- 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 e8607c7199..0000000000 --- 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 630b45cb8d..0000000000 --- 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_pidigits.rb b/benchmark/bm_so_pidigits.rb deleted file mode 100644 index 9a537b2d1c..0000000000 --- a/benchmark/bm_so_pidigits.rb +++ /dev/null @@ -1,92 +0,0 @@ -# The Great Computer Language Shootout -# http://shootout.alioth.debian.org/ -# -# contributed by Gabriele Renzi - -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 -n = 2_500 # Integer(ARGV[0]) -j = 0 - -digits = PiDigitSpigot.new - -while n > 0 - if n >= WIDTH - WIDTH.times {print digits.next!} - j += WIDTH - else - n.times {print digits.next!} - (WIDTH-n).times {print " "} - j += n - end - puts "\t:"+j.to_s - n -= WIDTH -end - diff --git a/benchmark/bm_so_random.rb b/benchmark/bm_so_random.rb deleted file mode 100644 index a66b9e8e63..0000000000 --- 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<N - i +=1 - gen_random(100.0) -end -# "%.9f" % gen_random(100.0) diff --git a/benchmark/bm_so_sieve.rb b/benchmark/bm_so_sieve.rb deleted file mode 100644 index 43dc302648..0000000000 --- a/benchmark/bm_so_sieve.rb +++ /dev/null @@ -1,24 +0,0 @@ -# from http://www.bagley.org/~doug/shootout/bench/sieve/sieve.ruby -num = 500 -count = i = j = 0 -flags0 = Array.new(8192,1) -k = 0 -while k < num - k += 1 - count = 0 - flags = flags0.dup - i = 2 - while i<8192 - i += 1 - if flags[i] - # remove all multiples of prime: i - j = i*i - while j < 8192 - j += i - flags[j] = nil - end - count += 1 - end - end -end -count diff --git a/benchmark/bm_so_spectralnorm.rb b/benchmark/bm_so_spectralnorm.rb deleted file mode 100644 index 6b97206689..0000000000 --- a/benchmark/bm_so_spectralnorm.rb +++ /dev/null @@ -1,50 +0,0 @@ -# The Computer Language Shootout -# http://shootout.alioth.debian.org/ -# Contributed by Sokolov Yura - -def eval_A(i,j) - return 1.0/((i+j)*(i+j+1)/2+i+1) -end - -def eval_A_times_u(u) - v, i = nil, nil - (0..u.length-1).collect { |i| - v = 0 - for j in 0..u.length-1 - v += eval_A(i,j)*u[j] - end - v - } -end - -def eval_At_times_u(u) - v, i = nil, nil - (0..u.length-1).collect{|i| - v = 0 - for j in 0..u.length-1 - v += eval_A(j,i)*u[j] - end - v - } -end - -def eval_AtA_times_u(u) - return eval_At_times_u(eval_A_times_u(u)) -end - -n = 500 # ARGV[0].to_i - -u=[1]*n -for i in 1..10 - v=eval_AtA_times_u(u) - u=eval_AtA_times_u(v) -end -vBv=0 -vv=0 -for i in 0..n-1 - vBv += u[i]*v[i] - vv += v[i]*v[i] -end - -str = "%0.9f" % (Math.sqrt(vBv/vv)), "\n" -# print str diff --git a/benchmark/bm_string_index.rb b/benchmark/bm_string_index.rb deleted file mode 100644 index 7783111082..0000000000 --- a/benchmark/bm_string_index.rb +++ /dev/null @@ -1,3 +0,0 @@ -str1 = "あ" * 1024 + "い" # not single byte optimizable -str2 = "い" -100_000.times { str1.index(str2) } diff --git a/benchmark/bm_string_scan_re.rb b/benchmark/bm_string_scan_re.rb deleted file mode 100644 index b0d60201a9..0000000000 --- a/benchmark/bm_string_scan_re.rb +++ /dev/null @@ -1,2 +0,0 @@ -str = Array.new(1_000, 'abc').join(',') -1_000.times { str.scan(/abc/) } diff --git a/benchmark/bm_string_scan_str.rb b/benchmark/bm_string_scan_str.rb deleted file mode 100644 index 42440bd948..0000000000 --- a/benchmark/bm_string_scan_str.rb +++ /dev/null @@ -1,2 +0,0 @@ -str = Array.new(1_000, 'abc').join(',') -1_000.times { str.scan('abc') } diff --git a/benchmark/bm_time_subsec.rb b/benchmark/bm_time_subsec.rb deleted file mode 100644 index 505021c701..0000000000 --- a/benchmark/bm_time_subsec.rb +++ /dev/null @@ -1,2 +0,0 @@ -t = Time.now -4000000.times { t.subsec } diff --git a/benchmark/bm_vm3_backtrace.rb b/benchmark/bm_vm3_backtrace.rb deleted file mode 100644 index 0fbf73e1ca..0000000000 --- a/benchmark/bm_vm3_backtrace.rb +++ /dev/null @@ -1,22 +0,0 @@ -# get last backtrace - -begin - caller(0, 0) -rescue ArgumentError - alias caller_orig caller - def caller lev, n - caller_orig(lev)[0..n] - end -end - -def rec n - if n < 0 - 100_000.times{ - caller(0, 1) - } - else - rec(n-1) - end -end - -rec 50 diff --git a/benchmark/bm_vm3_clearmethodcache.rb b/benchmark/bm_vm3_clearmethodcache.rb deleted file mode 100644 index 9661323cd2..0000000000 --- a/benchmark/bm_vm3_clearmethodcache.rb +++ /dev/null @@ -1,8 +0,0 @@ -i = 0 -while i<200_000 - i += 1 - - Class.new{ - def m; end - } -end diff --git a/benchmark/bm_vm3_gc.rb b/benchmark/bm_vm3_gc.rb deleted file mode 100644 index e668026915..0000000000 --- a/benchmark/bm_vm3_gc.rb +++ /dev/null @@ -1,6 +0,0 @@ -5000.times do - 100.times do - {"xxxx"=>"yyyy"} - end - GC.start -end diff --git a/benchmark/bm_vm3_gc_old_full.rb b/benchmark/bm_vm3_gc_old_full.rb deleted file mode 100644 index cfdfc8c5a5..0000000000 --- a/benchmark/bm_vm3_gc_old_full.rb +++ /dev/null @@ -1,4 +0,0 @@ -old_object = Array.new(1_000_000){''} -100.times do - GC.start -end diff --git a/benchmark/bm_vm3_gc_old_immediate.rb b/benchmark/bm_vm3_gc_old_immediate.rb deleted file mode 100644 index ad22feb655..0000000000 --- a/benchmark/bm_vm3_gc_old_immediate.rb +++ /dev/null @@ -1,4 +0,0 @@ -old_object = Array.new(1_000_000){''} -30_000.times do - GC.start(full_mark: false, immediate_sweep: true) -end diff --git a/benchmark/bm_vm3_gc_old_lazy.rb b/benchmark/bm_vm3_gc_old_lazy.rb deleted file mode 100644 index b74d44baf1..0000000000 --- a/benchmark/bm_vm3_gc_old_lazy.rb +++ /dev/null @@ -1,4 +0,0 @@ -old_object = Array.new(1_000_000){''} -30_000.times do - GC.start(full_mark: false, immediate_sweep: false) -end diff --git a/benchmark/bm_vm_symbol_block_pass.rb b/benchmark/bm_vm_symbol_block_pass.rb deleted file mode 100644 index 1d433353e1..0000000000 --- a/benchmark/bm_vm_symbol_block_pass.rb +++ /dev/null @@ -1,13 +0,0 @@ -class C - 1000.times {|i| - eval("def i#{i};end") - } -end - -c = C.new -m = C.instance_methods(false) -5_000.times do - m.each do |n| - c.tap(&n) - end -end diff --git a/benchmark/bm_vm_thread_alive_check1.rb b/benchmark/bm_vm_thread_alive_check1.rb deleted file mode 100644 index c993accdda..0000000000 --- a/benchmark/bm_vm_thread_alive_check1.rb +++ /dev/null @@ -1,6 +0,0 @@ -5_000.times{ - t = Thread.new{} - while t.alive? - Thread.pass - end -} diff --git a/benchmark/bm_vm_thread_close.rb b/benchmark/bm_vm_thread_close.rb deleted file mode 100644 index 3e9a265ce8..0000000000 --- a/benchmark/bm_vm_thread_close.rb +++ /dev/null @@ -1,6 +0,0 @@ -1000.times { Thread.new { sleep } } -i = 0 -while i<100_000 # benchmark loop 3 - i += 1 - IO.pipe.each(&:close) -end diff --git a/benchmark/bm_vm_thread_condvar1.rb b/benchmark/bm_vm_thread_condvar1.rb deleted file mode 100644 index cf5706b23e..0000000000 --- a/benchmark/bm_vm_thread_condvar1.rb +++ /dev/null @@ -1,28 +0,0 @@ -# two threads, two mutex, two condvar ping-pong -require 'thread' -m1 = Mutex.new -m2 = Mutex.new -cv1 = ConditionVariable.new -cv2 = ConditionVariable.new -max = 100000 -i = 0 -wait = nil -m2.synchronize do - wait = Thread.new do - m1.synchronize do - m2.synchronize { cv2.signal } - while (i += 1) < max - cv1.wait(m1) - cv2.signal - end - end - end - cv2.wait(m2) -end -m1.synchronize do - while i < max - cv1.signal - cv2.wait(m1) - end -end -wait.join diff --git a/benchmark/bm_vm_thread_condvar2.rb b/benchmark/bm_vm_thread_condvar2.rb deleted file mode 100644 index 7c8dc19481..0000000000 --- a/benchmark/bm_vm_thread_condvar2.rb +++ /dev/null @@ -1,35 +0,0 @@ -# many threads, one mutex, many condvars -require 'thread' -m = Mutex.new -cv1 = ConditionVariable.new -cv2 = ConditionVariable.new -max = 1000 -n = 100 -waiting = 0 -scvs = [] -waiters = n.times.map do |i| - start_cv = ConditionVariable.new - scvs << start_cv - start_mtx = Mutex.new - start_mtx.synchronize do - th = Thread.new(start_mtx, start_cv) do |sm, scv| - m.synchronize do - sm.synchronize { scv.signal } - max.times do - cv2.signal if (waiting += 1) == n - cv1.wait(m) - end - end - end - start_cv.wait(start_mtx) - th - end -end -m.synchronize do - max.times do - cv2.wait(m) until waiting == n - waiting = 0 - cv1.broadcast - end -end -waiters.each(&:join) diff --git a/benchmark/bm_vm_thread_create_join.rb b/benchmark/bm_vm_thread_create_join.rb deleted file mode 100644 index 393cd45df9..0000000000 --- a/benchmark/bm_vm_thread_create_join.rb +++ /dev/null @@ -1,6 +0,0 @@ -i = 0 -while i<100_000 # benchmark loop 3 - i += 1 - Thread.new{ - }.join -end diff --git a/benchmark/bm_vm_thread_mutex1.rb b/benchmark/bm_vm_thread_mutex1.rb deleted file mode 100644 index 66e42c85e1..0000000000 --- a/benchmark/bm_vm_thread_mutex1.rb +++ /dev/null @@ -1,21 +0,0 @@ -# one thread, one mutex (no contention) - -require 'thread' -m = Thread::Mutex.new -r = 0 -max = 2000 -lmax = max * max -(1..1).map{ - Thread.new{ - i = 0 - while i<lmax - i += 1 - m.synchronize{ - r += 1 - } - end - } -}.each{|e| - e.join -} -raise r.to_s if r != max * max diff --git a/benchmark/bm_vm_thread_mutex2.rb b/benchmark/bm_vm_thread_mutex2.rb deleted file mode 100644 index 6e6c804c31..0000000000 --- a/benchmark/bm_vm_thread_mutex2.rb +++ /dev/null @@ -1,21 +0,0 @@ -# two threads, one mutex - -require 'thread' -m = Thread::Mutex.new -r = 0 -max = 2000 -lmax = (max * max)/2 -(1..2).map{ - Thread.new{ - i = 0 - while i<lmax - i += 1 - m.synchronize{ - r += 1 - } - end - } -}.each{|e| - e.join -} -raise r.to_s if r != max * max diff --git a/benchmark/bm_vm_thread_mutex3.rb b/benchmark/bm_vm_thread_mutex3.rb deleted file mode 100644 index c750dc542a..0000000000 --- a/benchmark/bm_vm_thread_mutex3.rb +++ /dev/null @@ -1,20 +0,0 @@ -# 1000 threads, one mutex - -require 'thread' -m = Thread::Mutex.new -r = 0 -max = 2000 -(1..max).map{ - Thread.new{ - i = 0 - while i<max - i += 1 - m.synchronize{ - r += 1 - } - end - } -}.each{|e| - e.join -} -raise r.to_s if r != max * max diff --git a/benchmark/bm_vm_thread_pass.rb b/benchmark/bm_vm_thread_pass.rb deleted file mode 100644 index b5b3c0bc85..0000000000 --- a/benchmark/bm_vm_thread_pass.rb +++ /dev/null @@ -1,15 +0,0 @@ -# Plenty Thtread.pass -# A performance may depend on GVL implementation. - -tmax = (ARGV.shift || 2).to_i -lmax = 200_000 / tmax - -(1..tmax).map{ - Thread.new{ - lmax.times{ - Thread.pass - } - } -}.each{|t| t.join} - - diff --git a/benchmark/bm_vm_thread_pass_flood.rb b/benchmark/bm_vm_thread_pass_flood.rb deleted file mode 100644 index a660aafc18..0000000000 --- a/benchmark/bm_vm_thread_pass_flood.rb +++ /dev/null @@ -1,10 +0,0 @@ -# n.b. this is a good test for GVL when pinned to a single CPU - -1000.times{ - Thread.new{loop{Thread.pass}} -} - -i = 0 -while i<10000 - i += 1 -end diff --git a/benchmark/bm_vm_thread_pipe.rb b/benchmark/bm_vm_thread_pipe.rb deleted file mode 100644 index 112a621905..0000000000 --- a/benchmark/bm_vm_thread_pipe.rb +++ /dev/null @@ -1,17 +0,0 @@ -# Measure small and plenty pipe read/write. -# A performance may depend on GVL implementation. - -lmax = 100_000 -r, w = IO.pipe -[Thread.new{ - lmax.times{ - w.write('a') - } - p "w:exit" -}, Thread.new{ - lmax.times{ - r.read(1) - } - p "r:exit" -}].each{|t| t.join} - diff --git a/benchmark/bm_vm_thread_queue.rb b/benchmark/bm_vm_thread_queue.rb deleted file mode 100644 index 274ceda366..0000000000 --- a/benchmark/bm_vm_thread_queue.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'thread' - -n = 1_000_000 -q = Thread::Queue.new -consumer = Thread.new{ - while q.pop - # consuming - end -} - -producer = Thread.new{ - n.times{ - q.push true - } - q.push nil -} - -consumer.join diff --git a/benchmark/bm_vm_thread_sized_queue.rb b/benchmark/bm_vm_thread_sized_queue.rb deleted file mode 100644 index 7b9af5482b..0000000000 --- a/benchmark/bm_vm_thread_sized_queue.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'thread' -# on producer, one consumer - -n = 1_000_000 -q = Thread::SizedQueue.new(100) -consumer = Thread.new{ - while q.pop - # consuming - end -} - -producer = Thread.new{ - while n > 0 - q.push true - n -= 1 - end - q.push nil -} - -consumer.join diff --git a/benchmark/bm_vm_thread_sized_queue2.rb b/benchmark/bm_vm_thread_sized_queue2.rb deleted file mode 100644 index de9f55e978..0000000000 --- a/benchmark/bm_vm_thread_sized_queue2.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'thread' -# one producer, many consumers -n = 1_000_000 -m = 10 -q = Thread::SizedQueue.new(100) -consumers = m.times.map do - Thread.new do - while q.pop - # consuming - end - end -end - -producer = Thread.new do - while n > 0 - q.push true - n -= 1 - end - m.times { q.push nil } -end - -producer.join -consumers.each(&:join) diff --git a/benchmark/bm_vm_thread_sized_queue3.rb b/benchmark/bm_vm_thread_sized_queue3.rb deleted file mode 100644 index ce5f1796d8..0000000000 --- a/benchmark/bm_vm_thread_sized_queue3.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'thread' -# many producers, one consumer -n = 1_000_000 -m = 10 -q = Thread::SizedQueue.new(100) -consumer = Thread.new do - while q.pop - # consuming - end -end - -producers = m.times.map do - Thread.new do - while n > 0 - q.push true - n -= 1 - end - end -end -producers.each(&:join) -q.push nil -consumer.join diff --git a/benchmark/bm_vm_thread_sized_queue4.rb b/benchmark/bm_vm_thread_sized_queue4.rb deleted file mode 100644 index a9b7d80ec0..0000000000 --- a/benchmark/bm_vm_thread_sized_queue4.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'thread' -# many producers, many consumers -nr = 1_000_000 -n = 10 -m = 10 -q = Thread::SizedQueue.new(100) -consumers = n.times.map do - Thread.new do - while q.pop - # consuming - end - end -end - -producers = m.times.map do - Thread.new do - while nr > 0 - q.push true - nr -= 1 - end - end -end - -producers.each(&:join) -n.times { q.push nil } -consumers.each(&:join) diff --git a/benchmark/dir_empty_p.yml b/benchmark/dir_empty_p.yml new file mode 100644 index 0000000000..73e59bec98 --- /dev/null +++ b/benchmark/dir_empty_p.yml @@ -0,0 +1,8 @@ +benchmark: + dir_empty_p: | + require 'tmpdir' + max = 100_000 + Dir.mktmpdir('bm_dir_empty_p') do |dir| + max.times { Dir.empty?(dir) } + end +loop_count: 1 diff --git a/benchmark/erb_render.yml b/benchmark/erb_render.yml new file mode 100644 index 0000000000..33f23b2761 --- /dev/null +++ b/benchmark/erb_render.yml @@ -0,0 +1,28 @@ +benchmark: + erb_render: | + require 'erb' + + data = <<erb + <html> + <head> <%= title %> </head> + <body> + <h1> <%= title %> </h1> + <p> + <%= content %> + </p> + </body> + </html> + erb + + max = 1_500_000 + title = "hello world!" + content = "hello world!\n" * 10 + + src = "def self.render(title, content); #{ERB.new(data).src}; end" + mod = Module.new + mod.instance_eval(src, "(ERB)") + + max.times do + mod.render(title, content) + end +loop_count: 1 diff --git a/benchmark/file_chmod.yml b/benchmark/file_chmod.yml new file mode 100644 index 0000000000..baa43ddd42 --- /dev/null +++ b/benchmark/file_chmod.yml @@ -0,0 +1,13 @@ +prelude: | + # chmod file +benchmark: + file_chmod: | + require 'tempfile' + max = 200_000 + tmp = Tempfile.new('chmod') + path = tmp.path + max.times do + File.chmod(0777, path) + end + tmp.close! +loop_count: 1 diff --git a/benchmark/file_rename.yml b/benchmark/file_rename.yml new file mode 100644 index 0000000000..36a94d0b9b --- /dev/null +++ b/benchmark/file_rename.yml @@ -0,0 +1,15 @@ +prelude: | + # rename file +benchmark: + file_rename: | + require 'tempfile' + + max = 100_000 + tmp = [ Tempfile.new('rename-a'), Tempfile.new('rename-b') ] + a, b = tmp.map { |x| x.path } + max.times do + File.rename(a, b) + File.rename(b, a) + end + tmp.each { |t| t.close! } +loop_count: 1 diff --git a/benchmark/hash_aref_dsym.yml b/benchmark/hash_aref_dsym.yml new file mode 100644 index 0000000000..e862b5689c --- /dev/null +++ b/benchmark/hash_aref_dsym.yml @@ -0,0 +1,7 @@ +benchmark: + hash_aref_dsym: | + h = {} + syms = ('a'..'z').map { |s| s.to_sym } + syms.each { |s| h[s] = 1 } + 200_000.times { syms.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_aref_dsym_long.yml b/benchmark/hash_aref_dsym_long.yml new file mode 100644 index 0000000000..5488e052cd --- /dev/null +++ b/benchmark/hash_aref_dsym_long.yml @@ -0,0 +1,25 @@ +prelude: | + # [ruby-core:70129] [Bug #11396] +benchmark: + hash_aref_dsym_long: | + collection_size = 200000 + sample_size = 10000 + + values = (1..collection_size).to_a.map do |x| + "THIS IS A LONGER STRING THAT IS ALSO UNIQUE #{x}" + end + + symbol_hash = {} + + values.each do |x| + symbol_hash[x.to_sym] = 1 + end + + # use the same samples each time to minimize deviations + rng = Random.new(0) + symbol_sample_array = values.sample(sample_size, random: rng).map(&:to_sym) + + 3000.times do + symbol_sample_array.each { |x| symbol_hash[x] } + end +loop_count: 1 diff --git a/benchmark/hash_aref_fix.yml b/benchmark/hash_aref_fix.yml new file mode 100644 index 0000000000..5469d2a2db --- /dev/null +++ b/benchmark/hash_aref_fix.yml @@ -0,0 +1,7 @@ +benchmark: + hash_aref_fix: | + h = {} + nums = (1..26).to_a + nums.each { |i| h[i] = i } + 200_000.times { nums.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_aref_flo.yml b/benchmark/hash_aref_flo.yml new file mode 100644 index 0000000000..2e3d8c887b --- /dev/null +++ b/benchmark/hash_aref_flo.yml @@ -0,0 +1,7 @@ +benchmark: + hash_aref_flo: | + h = {} + strs = [*1..10000].map! {|i| i.fdiv(10)} + strs.each { |s| h[s] = s } + 50.times { strs.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_aref_miss.yml b/benchmark/hash_aref_miss.yml new file mode 100644 index 0000000000..4bd541e2ff --- /dev/null +++ b/benchmark/hash_aref_miss.yml @@ -0,0 +1,8 @@ +benchmark: + hash_aref_miss: | + h = {} + strs = ('a'..'z').to_a.map!(&:freeze) + strs.each { |s| h[s] = s } + strs = ('A'..'Z').to_a + 200_000.times { strs.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_aref_str.yml b/benchmark/hash_aref_str.yml new file mode 100644 index 0000000000..0790320ec5 --- /dev/null +++ b/benchmark/hash_aref_str.yml @@ -0,0 +1,7 @@ +benchmark: + hash_aref_str: | + h = {} + strs = ('a'..'z').to_a.map!(&:freeze) + strs.each { |s| h[s] = s } + 200_000.times { strs.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_aref_sym.yml b/benchmark/hash_aref_sym.yml new file mode 100644 index 0000000000..e289d4aff3 --- /dev/null +++ b/benchmark/hash_aref_sym.yml @@ -0,0 +1,12 @@ +benchmark: + hash_aref_sym: | + h = {} + syms = ('a'..'z').to_a + begin + syms = eval("%i[#{syms.join(' ')}]") + rescue SyntaxError # <= 1.9.3 + syms.map!(&:to_sym) + end + syms.each { |s| h[s] = s } + 200_000.times { syms.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_aref_sym_long.yml b/benchmark/hash_aref_sym_long.yml new file mode 100644 index 0000000000..4edccc12dd --- /dev/null +++ b/benchmark/hash_aref_sym_long.yml @@ -0,0 +1,16 @@ +benchmark: + hash_aref_sym_long: | + h = {} + syms = %w[puts warn syswrite write stat bacon lettuce tomato + some symbols in this array may already be interned others should not be + hash browns make good breakfast but not cooked using prime numbers + shift for division entries delete_if keys exist? + ] + begin + syms = eval("%i[#{syms.join(' ')}]") + rescue SyntaxError # <= 1.9.3 + syms.map!(&:to_sym) + end + syms.each { |s| h[s] = s } + 200_000.times { syms.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_flatten.yml b/benchmark/hash_flatten.yml new file mode 100644 index 0000000000..0412388417 --- /dev/null +++ b/benchmark/hash_flatten.yml @@ -0,0 +1,12 @@ +benchmark: + hash_flatten: | + h = {} + + 10000.times do |i| + h[i] = nil + end + + 1000.times do + h.flatten + end +loop_count: 1 diff --git a/benchmark/hash_ident_flo.yml b/benchmark/hash_ident_flo.yml new file mode 100644 index 0000000000..ee2a78db44 --- /dev/null +++ b/benchmark/hash_ident_flo.yml @@ -0,0 +1,7 @@ +benchmark: + hash_ident_flo: | + h = {}.compare_by_identity + strs = (1..10000).to_a.map!(&:to_f) + strs.each { |s| h[s] = s } + 50.times { strs.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_ident_num.yml b/benchmark/hash_ident_num.yml new file mode 100644 index 0000000000..c9b58bd0c6 --- /dev/null +++ b/benchmark/hash_ident_num.yml @@ -0,0 +1,7 @@ +benchmark: + hash_ident_num: | + h = {}.compare_by_identity + nums = (1..26).to_a + nums.each { |n| h[n] = n } + 200_000.times { nums.each { |n| h[n] } } +loop_count: 1 diff --git a/benchmark/hash_ident_obj.yml b/benchmark/hash_ident_obj.yml new file mode 100644 index 0000000000..95c310985f --- /dev/null +++ b/benchmark/hash_ident_obj.yml @@ -0,0 +1,7 @@ +benchmark: + hash_ident_obj: | + h = {}.compare_by_identity + objs = 26.times.map { Object.new } + objs.each { |o| h[o] = o } + 200_000.times { objs.each { |o| h[o] } } +loop_count: 1 diff --git a/benchmark/hash_ident_str.yml b/benchmark/hash_ident_str.yml new file mode 100644 index 0000000000..c2423778f7 --- /dev/null +++ b/benchmark/hash_ident_str.yml @@ -0,0 +1,7 @@ +benchmark: + hash_ident_str: | + h = {}.compare_by_identity + strs = ('a'..'z').to_a + strs.each { |s| h[s] = s } + 200_000.times { strs.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_ident_sym.yml b/benchmark/hash_ident_sym.yml new file mode 100644 index 0000000000..e49bdba7f4 --- /dev/null +++ b/benchmark/hash_ident_sym.yml @@ -0,0 +1,7 @@ +benchmark: + hash_ident_sym: | + h = {}.compare_by_identity + syms = ('a'..'z').to_a.map(&:to_sym) + syms.each { |s| h[s] = s } + 200_000.times { syms.each { |s| h[s] } } +loop_count: 1 diff --git a/benchmark/hash_keys.yml b/benchmark/hash_keys.yml new file mode 100644 index 0000000000..18e9078640 --- /dev/null +++ b/benchmark/hash_keys.yml @@ -0,0 +1,12 @@ +benchmark: + hash_keys: | + h = {} + + 10000.times do |i| + h[i] = nil + end + + 5000.times do + h.keys + end +loop_count: 1 diff --git a/benchmark/hash_long.yml b/benchmark/hash_long.yml new file mode 100644 index 0000000000..33c0c38f49 --- /dev/null +++ b/benchmark/hash_long.yml @@ -0,0 +1,7 @@ +benchmark: + hash_long: | + k1 = "Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong"; + k2 = "Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping Pong Ping"; + h = {k1 => 0, k2 => 0}; + 3000000.times{|i| k = i % 2 ? k2 : k1; h [k] = h[k] + 1} +loop_count: 1 diff --git a/benchmark/hash_shift.yml b/benchmark/hash_shift.yml new file mode 100644 index 0000000000..954c1eb6fe --- /dev/null +++ b/benchmark/hash_shift.yml @@ -0,0 +1,13 @@ +benchmark: + hash_shift: | + h = {} + + 10000.times do |i| + h[i] = nil + end + + 50000.times do + k, v = h.shift + h[k] = v + end +loop_count: 1 diff --git a/benchmark/hash_shift_u16.yml b/benchmark/hash_shift_u16.yml new file mode 100644 index 0000000000..b8e1ae75f4 --- /dev/null +++ b/benchmark/hash_shift_u16.yml @@ -0,0 +1,13 @@ +benchmark: + hash_shift_u16: | + h = {} + + (16384..65536).each do |i| + h[i] = nil + end + + 300000.times do + k, v = h.shift + h[k] = v + end +loop_count: 1 diff --git a/benchmark/hash_shift_u24.yml b/benchmark/hash_shift_u24.yml new file mode 100644 index 0000000000..743d3cefc3 --- /dev/null +++ b/benchmark/hash_shift_u24.yml @@ -0,0 +1,13 @@ +benchmark: + hash_shift_u24: | + h = {} + + (0xff4000..0xffffff).each do |i| + h[i] = nil + end + + 300000.times do + k, v = h.shift + h[k] = v + end +loop_count: 1 diff --git a/benchmark/hash_shift_u32.yml b/benchmark/hash_shift_u32.yml new file mode 100644 index 0000000000..96e9753dde --- /dev/null +++ b/benchmark/hash_shift_u32.yml @@ -0,0 +1,13 @@ +benchmark: + hash_shift_u32: | + h = {} + + (0xffff4000..0xffffffff).each do |i| + h[i] = nil + end + + 300000.times do + k, v = h.shift + h[k] = v + end +loop_count: 1 diff --git a/benchmark/hash_small2.yml b/benchmark/hash_small2.yml new file mode 100644 index 0000000000..0bf7ab6a1b --- /dev/null +++ b/benchmark/hash_small2.yml @@ -0,0 +1,4 @@ +benchmark: + hash_small2: | + 1000000.times.map{|i| a={}; 2.times{|j| a[j]=j}; a} +loop_count: 1 diff --git a/benchmark/hash_small4.yml b/benchmark/hash_small4.yml new file mode 100644 index 0000000000..24bc334c63 --- /dev/null +++ b/benchmark/hash_small4.yml @@ -0,0 +1,4 @@ +benchmark: + hash_small4: | + 1000000.times.map{|i| a={}; 4.times{|j| a[j]=j}; a} +loop_count: 1 diff --git a/benchmark/hash_small8.yml b/benchmark/hash_small8.yml new file mode 100644 index 0000000000..30b50a789c --- /dev/null +++ b/benchmark/hash_small8.yml @@ -0,0 +1,4 @@ +benchmark: + hash_small8: | + 1000000.times.map{|i| a={}; 8.times{|j| a[j]=j}; a} +loop_count: 1 diff --git a/benchmark/hash_to_proc.yml b/benchmark/hash_to_proc.yml new file mode 100644 index 0000000000..2ae8588b27 --- /dev/null +++ b/benchmark/hash_to_proc.yml @@ -0,0 +1,12 @@ +benchmark: + hash_to_proc: | + h = {} + + 10000.times do |i| + h[i] = nil + end + + 5000.times do |i| + [i].map(&h) + end +loop_count: 1 diff --git a/benchmark/hash_values.yml b/benchmark/hash_values.yml new file mode 100644 index 0000000000..b645bcf4f8 --- /dev/null +++ b/benchmark/hash_values.yml @@ -0,0 +1,12 @@ +benchmark: + hash_values: | + h = {} + + 10000.times do |i| + h[i] = nil + end + + 5000.times do + h.values + end +loop_count: 1 diff --git a/benchmark/int_quo.yml b/benchmark/int_quo.yml new file mode 100644 index 0000000000..1527de3dfc --- /dev/null +++ b/benchmark/int_quo.yml @@ -0,0 +1,4 @@ +benchmark: + int_quo: | + 5000000.times { 42.quo(3) } +loop_count: 1 diff --git a/benchmark/io_copy_stream_write.yml b/benchmark/io_copy_stream_write.yml new file mode 100644 index 0000000000..32fa920951 --- /dev/null +++ b/benchmark/io_copy_stream_write.yml @@ -0,0 +1,28 @@ +prelude: | + # The goal of this is to use a synthetic (non-IO) reader + # to trigger the read/write loop of IO.copy_stream, + # bypassing in-kernel mechanisms like sendfile for zero copy, + # so we wrap the /dev/zero IO object: +benchmark: + io_copy_stream_write: | + + class Zero + def initialize + @n = 100000 + @in = File.open('/dev/zero', 'rb') + end + + def read(len, buf) + return if (@n -= 1) == 0 + @in.read(len, buf) + end + end + + begin + src = Zero.new + dst = File.open(IO::NULL, 'wb') + n = IO.copy_stream(src, dst) + rescue Errno::ENOENT + # not *nix + end if IO.respond_to?(:copy_stream) && IO.const_defined?(:NULL) +loop_count: 1 diff --git a/benchmark/io_copy_stream_write_socket.yml b/benchmark/io_copy_stream_write_socket.yml new file mode 100644 index 0000000000..2207eca8c6 --- /dev/null +++ b/benchmark/io_copy_stream_write_socket.yml @@ -0,0 +1,39 @@ +prelude: | + # The goal of this is to use a synthetic (non-IO) reader + # to trigger the read/write loop of IO.copy_stream, + # bypassing in-kernel mechanisms like sendfile for zero copy, + # so we wrap the /dev/zero IO object: +benchmark: + io_copy_stream_write_socket: | + class Zero + def initialize + @n = 100000 + @in = File.open('/dev/zero', 'rb') + end + + def read(len, buf) + return if (@n -= 1) == 0 + @in.read(len, buf) + end + end + + begin + require 'socket' + src = Zero.new + rd, wr = UNIXSocket.pair + pid = fork do + wr.close + buf = String.new + while rd.read(16384, buf) + end + end + rd.close + IO.copy_stream(src, wr) + rescue Errno::ENOENT, NotImplementedError, NameError + # not *nix: missing /dev/zero, fork, or UNIXSocket + rescue LoadError # no socket? + ensure + wr.close if wr + Process.waitpid(pid) if pid + end if IO.respond_to?(:copy_stream) +loop_count: 1 diff --git a/benchmark/io_file_create.yml b/benchmark/io_file_create.yml new file mode 100644 index 0000000000..ece11cc332 --- /dev/null +++ b/benchmark/io_file_create.yml @@ -0,0 +1,15 @@ +benchmark: + io_file_create: | + # + # Create files + # + + max = 200_000 + file = './tmpfile_of_bm_io_file_create' + + max.times{ + f = open(file, 'w') + f.close#(true) + } + File.unlink(file) +loop_count: 1 diff --git a/benchmark/io_file_read.yml b/benchmark/io_file_read.yml new file mode 100644 index 0000000000..f8f0c1c871 --- /dev/null +++ b/benchmark/io_file_read.yml @@ -0,0 +1,18 @@ +benchmark: + io_file_read: | + # + # Seek and Read file. + # + + require 'tempfile' + + max = 200_000 + str = "Hello world! " * 1000 + f = Tempfile.new('yarv-benchmark') + f.write str + + max.times{ + f.seek 0 + f.read + } +loop_count: 1 diff --git a/benchmark/io_file_write.yml b/benchmark/io_file_write.yml new file mode 100644 index 0000000000..68af6d588c --- /dev/null +++ b/benchmark/io_file_write.yml @@ -0,0 +1,17 @@ +benchmark: + io_file_write: | + # + # Seek and Write file. + # + + require 'tempfile' + + max = 200_000 + str = "Hello world! " * 1000 + f = Tempfile.new('yarv-benchmark') + + max.times{ + f.seek 0 + f.write str + } +loop_count: 1 diff --git a/benchmark/io_nonblock_noex.yml b/benchmark/io_nonblock_noex.yml new file mode 100644 index 0000000000..6e29240bd7 --- /dev/null +++ b/benchmark/io_nonblock_noex.yml @@ -0,0 +1,25 @@ +benchmark: + io_nonblock_noex: | + nr = 1_000_000 + i = 0 + msg = '.' + buf = '.' + noex = { exception: false } + begin + r, w = IO.pipe + while i < nr + i += 1 + w.write_nonblock(msg, noex) + r.read_nonblock(1, buf, noex) + end + rescue ArgumentError # old Rubies + while i < nr + i += 1 + w.write_nonblock(msg) + r.read_nonblock(1, buf) + end + ensure + r.close + w.close + end +loop_count: 1 diff --git a/benchmark/io_nonblock_noex2.yml b/benchmark/io_nonblock_noex2.yml new file mode 100644 index 0000000000..eda8687028 --- /dev/null +++ b/benchmark/io_nonblock_noex2.yml @@ -0,0 +1,24 @@ +benchmark: + io_nonblock_noex2: | + nr = 1_000_000 + i = 0 + msg = '.' + buf = '.' + begin + r, w = IO.pipe + while i < nr + i += 1 + w.write_nonblock(msg, exception: false) + r.read_nonblock(1, buf, exception: false) + end + rescue ArgumentError # old Rubies + while i < nr + i += 1 + w.write_nonblock(msg) + r.read_nonblock(1, buf) + end + ensure + r.close + w.close + end +loop_count: 1 diff --git a/benchmark/io_pipe_rw.yml b/benchmark/io_pipe_rw.yml new file mode 100644 index 0000000000..cd9e6c3826 --- /dev/null +++ b/benchmark/io_pipe_rw.yml @@ -0,0 +1,17 @@ +prelude: | + # Measure uncontended GVL performance via read/write with 1:1 threading + # If we switch to M:N threading, this will benchmark something else... +benchmark: + io_pipe_rw: | + r, w = IO.pipe + src = '0'.freeze + dst = String.new + i = 0 + while i < 1_000_000 + i += 1 + w.write(src) + r.read(1, dst) + end + w.close + r.close +loop_count: 1 diff --git a/benchmark/io_select.yml b/benchmark/io_select.yml new file mode 100644 index 0000000000..bcea6b9a70 --- /dev/null +++ b/benchmark/io_select.yml @@ -0,0 +1,12 @@ +prelude: | + # IO.select performance +benchmark: + io_select: | + + w = [ IO.pipe[1] ]; + + nr = 1000000 + nr.times { + IO.select nil, w + } +loop_count: 1 diff --git a/benchmark/io_select2.yml b/benchmark/io_select2.yml new file mode 100644 index 0000000000..3c8078dc11 --- /dev/null +++ b/benchmark/io_select2.yml @@ -0,0 +1,25 @@ +prelude: | + # IO.select performance. worst case of single fd. +benchmark: + io_select2: | + + ios = [] + nr = 1000000 + if defined?(Process::RLIMIT_NOFILE) + max = Process.getrlimit(Process::RLIMIT_NOFILE)[0] + else + max = 64 + end + puts "max fd: #{max} (results not apparent with <= 1024 max fd)" + + ((max / 2) - 10).times do + ios.concat IO.pipe + end + + last = [ ios[-1] ] + puts "last IO: #{last[0].inspect}" + + nr.times do + IO.select nil, last + end +loop_count: 1 diff --git a/benchmark/io_select3.yml b/benchmark/io_select3.yml new file mode 100644 index 0000000000..c97d0e0cd1 --- /dev/null +++ b/benchmark/io_select3.yml @@ -0,0 +1,24 @@ +prelude: | + # IO.select performance. a lot of fd +benchmark: + io_select3: | + + ios = [] + nr = 100 + if defined?(Process::RLIMIT_NOFILE) + max = Process.getrlimit(Process::RLIMIT_NOFILE)[0] + else + max = 64 + end + puts "max fd: #{max} (results not apparent with <= 1024 max fd)" + + (max - 10).times do + r, w = IO.pipe + r.close + ios.push w + end + + nr.times do + IO.select nil, ios + end +loop_count: 1 diff --git a/benchmark/loop_for.yml b/benchmark/loop_for.yml new file mode 100644 index 0000000000..99279f014a --- /dev/null +++ b/benchmark/loop_for.yml @@ -0,0 +1,6 @@ +benchmark: + loop_for: | + for i in 1..30_000_000 + # + end +loop_count: 1 diff --git a/benchmark/loop_generator.yml b/benchmark/loop_generator.yml new file mode 100644 index 0000000000..a5a39c085a --- /dev/null +++ b/benchmark/loop_generator.yml @@ -0,0 +1,17 @@ +benchmark: + loop_generator: | + max = 600000 + + if defined? Fiber + gen = (1..max).each + loop do + gen.next + end + else + require 'generator' + gen = Generator.new((0..max)) + while gen.next? + gen.next + end + end +loop_count: 1 diff --git a/benchmark/loop_times.yml b/benchmark/loop_times.yml new file mode 100644 index 0000000000..8d50d6edda --- /dev/null +++ b/benchmark/loop_times.yml @@ -0,0 +1,4 @@ +benchmark: + loop_times: | + 30_000_000.times{|e|} +loop_count: 1 diff --git a/benchmark/loop_whileloop.yml b/benchmark/loop_whileloop.yml new file mode 100644 index 0000000000..0fa797aed4 --- /dev/null +++ b/benchmark/loop_whileloop.yml @@ -0,0 +1,7 @@ +benchmark: + loop_whileloop: | + i = 0 + while i<30_000_000 # benchmark loop 1 + i += 1 + end +loop_count: 1 diff --git a/benchmark/loop_whileloop2.yml b/benchmark/loop_whileloop2.yml new file mode 100644 index 0000000000..2cf83908b3 --- /dev/null +++ b/benchmark/loop_whileloop2.yml @@ -0,0 +1,7 @@ +benchmark: + loop_whileloop2: | + i = 0 + while i< 6_000_000 # benchmark loop 2 + i += 1 + end +loop_count: 1 diff --git a/benchmark/marshal_dump_flo.yml b/benchmark/marshal_dump_flo.yml new file mode 100644 index 0000000000..0e3cc1394a --- /dev/null +++ b/benchmark/marshal_dump_flo.yml @@ -0,0 +1,5 @@ +benchmark: + marshal_dump_flo: | + bug10761 = 10000.times.map { |x| x.to_f } + 100.times { Marshal.dump(bug10761) } +loop_count: 1 diff --git a/benchmark/marshal_dump_load_geniv.yml b/benchmark/marshal_dump_load_geniv.yml new file mode 100644 index 0000000000..6395c1d1cd --- /dev/null +++ b/benchmark/marshal_dump_load_geniv.yml @@ -0,0 +1,13 @@ +benchmark: + marshal_dump_load_geniv: | + a = '' + a.instance_eval do + @a = :a + @b = :b + @c = :c + end + 100000.times do + a = Marshal.load(Marshal.dump(a)) + end + #p(a.instance_eval { @a == :a && @b == :b && @c == :c }) +loop_count: 1 diff --git a/benchmark/marshal_dump_load_time.yml b/benchmark/marshal_dump_load_time.yml new file mode 100644 index 0000000000..0c25c2d677 --- /dev/null +++ b/benchmark/marshal_dump_load_time.yml @@ -0,0 +1,4 @@ +benchmark: + marshal_dump_load_time: | + 100000.times { Marshal.load(Marshal.dump(Time.now)) } +loop_count: 1 diff --git a/benchmark/securerandom.yml b/benchmark/securerandom.yml new file mode 100644 index 0000000000..187ca2e294 --- /dev/null +++ b/benchmark/securerandom.yml @@ -0,0 +1,8 @@ +benchmark: + securerandom: | + require "securerandom" + + 20_0000.times do + SecureRandom.random_number(100) + end +loop_count: 1 diff --git a/benchmark/so_ackermann.yml b/benchmark/so_ackermann.yml new file mode 100644 index 0000000000..4082169f5e --- /dev/null +++ b/benchmark/so_ackermann.yml @@ -0,0 +1,21 @@ +prelude: | + #!/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/ +benchmark: + so_ackermann: | + + 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) +loop_count: 1 diff --git a/benchmark/so_array.yml b/benchmark/so_array.yml new file mode 100644 index 0000000000..e68643879e --- /dev/null +++ b/benchmark/so_array.yml @@ -0,0 +1,25 @@ +prelude: | + #!/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 +benchmark: + so_array: | + + 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}" +loop_count: 1 diff --git a/benchmark/so_binary_trees.yml b/benchmark/so_binary_trees.yml new file mode 100644 index 0000000000..9e943d50ff --- /dev/null +++ b/benchmark/so_binary_trees.yml @@ -0,0 +1,66 @@ +prelude: | + # The Computer Language Shootout Benchmarks + # http://shootout.alioth.debian.org +benchmark: + so_binary_trees: | + # + # 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 = 16 # 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 +loop_count: 1 diff --git a/benchmark/so_concatenate.yml b/benchmark/so_concatenate.yml new file mode 100644 index 0000000000..13d6559745 --- /dev/null +++ b/benchmark/so_concatenate.yml @@ -0,0 +1,20 @@ +prelude: | + #!/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 +benchmark: + so_concatenate: | + + STUFF = "hello\n" + i = 0 + while i<10 + i += 1 + hello = '' + 4_000_000.times do |e| + hello << STUFF + end + end + # puts hello.length +loop_count: 1 diff --git a/benchmark/so_exception.yml b/benchmark/so_exception.yml new file mode 100644 index 0000000000..0bfe3718eb --- /dev/null +++ b/benchmark/so_exception.yml @@ -0,0 +1,65 @@ +prelude: | + #!/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/ +benchmark: + so_exception: | + + $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 +loop_count: 1 diff --git a/benchmark/so_fannkuch.yml b/benchmark/so_fannkuch.yml new file mode 100644 index 0000000000..3d7a2b43bd --- /dev/null +++ b/benchmark/so_fannkuch.yml @@ -0,0 +1,48 @@ +prelude: | + # The Computer Language Shootout + # http://shootout.alioth.debian.org/ + # Contributed by Sokolov Yura + # Modified by Ryan Williams +benchmark: + so_fannkuch: | + + def fannkuch(n) + maxFlips, m, r, check = 0, n-1, n, 0 + count = (1..n).to_a + perm = (1..n).to_a + + while true + if check < 30 + puts "#{perm}" + check += 1 + end + + while r != 1 + count[r-1] = r + r -= 1 + end + + if perm[0] != 1 and perm[m] != n + perml = perm.clone #.dup + flips = 0 + while (k = perml.first ) != 1 + perml = perml.slice!(0, k).reverse + perml + flips += 1 + end + maxFlips = flips if flips > maxFlips + end + while true + if r==n then return maxFlips end + perm.insert r,perm.shift + break if (count[r] -= 1) > 0 + r += 1 + end + end + end + + def puts *args + end + + N = 9 # (ARGV[0] || 1).to_i + puts "Pfannkuchen(#{N}) = #{fannkuch(N)}" +loop_count: 1 diff --git a/benchmark/so_fasta.yml b/benchmark/so_fasta.yml new file mode 100644 index 0000000000..c7c6a92958 --- /dev/null +++ b/benchmark/so_fasta.yml @@ -0,0 +1,84 @@ +prelude: | + # The Computer Language Shootout + # http://shootout.alioth.debian.org/ + # Contributed by Sokolov Yura +benchmark: + so_fasta: | + + $last = 42.0 + def gen_random(max, im=139968, ia=3877, ic=29573) + (max * ($last = ($last * ia + ic) % im)) / im + end + + alu = + "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"+ + "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"+ + "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"+ + "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"+ + "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"+ + "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"+ + "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA" + + iub = [ + ["a", 0.27], + ["c", 0.12], + ["g", 0.12], + ["t", 0.27], + + ["B", 0.02], + ["D", 0.02], + ["H", 0.02], + ["K", 0.02], + ["M", 0.02], + ["N", 0.02], + ["R", 0.02], + ["S", 0.02], + ["V", 0.02], + ["W", 0.02], + ["Y", 0.02], + ] + homosapiens = [ + ["a", 0.3029549426680], + ["c", 0.1979883004921], + ["g", 0.1975473066391], + ["t", 0.3015094502008], + ] + + def make_repeat_fasta(id, desc, src, n) + puts ">#{id} #{desc}" + v = nil + width = 60 + l = src.length + s = src * ((n / l) + 1) + s.slice!(n, l) + puts(s.scan(/.{1,#{width}}/).join("\n")) + end + + def make_random_fasta(id, desc, table, n) + puts ">#{id} #{desc}" + rand, v = nil,nil + width = 60 + chunk = 1 * width + prob = 0.0 + table.each{|v| v[1]= (prob += v[1])} + for i in 1..(n/width) + puts((1..width).collect{ + rand = gen_random(1.0) + table.find{|v| v[1]>rand}[0] + }.join) + end + if n%width != 0 + puts((1..(n%width)).collect{ + rand = gen_random(1.0) + table.find{|v| v[1]>rand}[0] + }.join) + end + end + + + n = (ARGV[0] or 250_000).to_i + + make_repeat_fasta('ONE', 'Homo sapiens alu', alu, n*2) + make_random_fasta('TWO', 'IUB ambiguity codes', iub, n*3) + make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, n*5) +loop_count: 1 diff --git a/benchmark/so_lists.yml b/benchmark/so_lists.yml new file mode 100644 index 0000000000..44a7495a2f --- /dev/null +++ b/benchmark/so_lists.yml @@ -0,0 +1,51 @@ +prelude: | + #from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby +benchmark: + so_lists: | + + NUM = 300 + SIZE = 10000 + + def test_lists() + # create a list of integers (Li1) from 1 to SIZE + li1 = (1..SIZE).to_a + # copy the list to li2 (not by individual items) + li2 = li1.dup + # remove each individual item from left side of li2 and + # append to right side of li3 (preserving order) + li3 = Array.new + while (not li2.empty?) + li3.push(li2.shift) + end + # li2 must now be empty + # remove each individual item from right side of li3 and + # append to right side of li2 (reversing list) + while (not li3.empty?) + li2.push(li3.pop) + end + # li3 must now be empty + # reverse li1 in place + li1.reverse! + # check that first item is now SIZE + if li1[0] != SIZE then + p "not SIZE" + 0 + else + # compare li1 and li2 for equality + if li1 != li2 then + return(0) + else + # return the length of the list + li1.length + end + end + end + + i = 0 + while i<NUM + i += 1 + result = test_lists() + end + + result +loop_count: 1 diff --git a/benchmark/so_mandelbrot.yml b/benchmark/so_mandelbrot.yml new file mode 100644 index 0000000000..daa58a0133 --- /dev/null +++ b/benchmark/so_mandelbrot.yml @@ -0,0 +1,61 @@ +prelude: | + # The Computer Language Benchmarks Game + # http://shootout.alioth.debian.org/ +benchmark: + so_mandelbrot: | + # + # 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 +loop_count: 1 diff --git a/benchmark/so_matrix.yml b/benchmark/so_matrix.yml new file mode 100644 index 0000000000..9a534471d9 --- /dev/null +++ b/benchmark/so_matrix.yml @@ -0,0 +1,50 @@ +prelude: | + #!/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/ +benchmark: + so_matrix: | + + 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]}" +loop_count: 1 diff --git a/benchmark/so_meteor_contest.yml b/benchmark/so_meteor_contest.yml new file mode 100644 index 0000000000..c35df18f68 --- /dev/null +++ b/benchmark/so_meteor_contest.yml @@ -0,0 +1,567 @@ +prelude: | + #!/usr/bin/env ruby +benchmark: + so_meteor_contest: | + # + # The Computer Language Shootout + # http://shootout.alioth.debian.org + # contributed by Kevin Barnes (Ruby novice) + + # PROGRAM: the main body is at the bottom. + # 1) read about the problem here: http://www-128.ibm.com/developerworks/java/library/j-javaopt/ + # 2) see how I represent a board as a bitmask by reading the blank_board comments + # 3) read as your mental paths take you + + def print *args + end + + # class to represent all information about a particular rotation of a particular piece + class Rotation + # an array (by location) containing a bit mask for how the piece maps at the given location. + # if the rotation is invalid at that location the mask will contain false + attr_reader :start_masks + + # maps a direction to a relative location. these differ depending on whether it is an even or + # odd row being mapped from + @@rotation_even_adder = { :west => -1, :east => 1, :nw => -7, :ne => -6, :sw => 5, :se => 6 } + @@rotation_odd_adder = { :west => -1, :east => 1, :nw => -6, :ne => -5, :sw => 6, :se => 7 } + + def initialize( directions ) + @even_offsets, @odd_offsets = normalize_offsets( get_values( directions )) + + @even_mask = mask_for_offsets( @even_offsets) + @odd_mask = mask_for_offsets( @odd_offsets) + + @start_masks = Array.new(60) + + # create the rotational masks by placing the base mask at the location and seeing if + # 1) it overlaps the boundaries and 2) it produces a prunable board. if either of these + # is true the piece cannot be placed + 0.upto(59) do | offset | + mask = is_even(offset) ? (@even_mask << offset) : (@odd_mask << offset) + if (blank_board & mask == 0 && !prunable(blank_board | mask, 0, true)) then + imask = compute_required( mask, offset) + @start_masks[offset] = [ mask, imask, imask | mask ] + else + @start_masks[offset] = false + end + end + end + + def compute_required( mask, offset ) + board = blank_board + 0.upto(offset) { | i | board |= 1 << i } + board |= mask + return 0 if (!prunable(board | mask, offset)) + board = flood_fill(board,58) + count = 0 + imask = 0 + 0.upto(59) do | i | + if (board[i] == 0) then + imask |= (1 << i) + count += 1 + end + end + (count > 0 && count < 5) ? imask : 0 + end + + def flood_fill( board, location) + return board if (board[location] == 1) + board |= 1 << location + row, col = location.divmod(6) + board = flood_fill( board, location - 1) if (col > 0) + board = flood_fill( board, location + 1) if (col < 4) + if (row % 2 == 0) then + board = flood_fill( board, location - 7) if (col > 0 && row > 0) + board = flood_fill( board, location - 6) if (row > 0) + board = flood_fill( board, location + 6) if (row < 9) + board = flood_fill( board, location + 5) if (col > 0 && row < 9) + else + board = flood_fill( board, location - 5) if (col < 4 && row > 0) + board = flood_fill( board, location - 6) if (row > 0) + board = flood_fill( board, location + 6) if (row < 9) + board = flood_fill( board, location + 7) if (col < 4 && row < 9) + end + board + end + + # given a location, produces a list of relative locations covered by the piece at this rotation + def offsets( location) + if is_even( location) then + @even_offsets.collect { | value | value + location } + else + @odd_offsets.collect { | value | value + location } + end + end + + # returns a set of offsets relative to the top-left most piece of the rotation (by even or odd rows) + # this is hard to explain. imagine we have this partial board: + # 0 0 0 0 0 x [positions 0-5] + # 0 0 1 1 0 x [positions 6-11] + # 0 0 1 0 0 x [positions 12-17] + # 0 1 0 0 0 x [positions 18-23] + # 0 1 0 0 0 x [positions 24-29] + # 0 0 0 0 0 x [positions 30-35] + # ... + # The top-left of the piece is at position 8, the + # board would be passed as a set of positions (values array) containing [8,9,14,19,25] not necessarily in that + # sorted order. Since that array starts on an odd row, the offsets for an odd row are: [0,1,6,11,17] obtained + # by subtracting 8 from everything. Now imagine the piece shifted up and to the right so it's on an even row: + # 0 0 0 1 1 x [positions 0-5] + # 0 0 1 0 0 x [positions 6-11] + # 0 0 1 0 0 x [positions 12-17] + # 0 1 0 0 0 x [positions 18-23] + # 0 0 0 0 0 x [positions 24-29] + # 0 0 0 0 0 x [positions 30-35] + # ... + # Now the positions are [3,4,8,14,19] which after subtracting the lowest value (3) gives [0,1,5,11,16] thus, the + # offsets for this particular piece are (in even, odd order) [0,1,5,11,16],[0,1,6,11,17] which is what + # this function would return + def normalize_offsets( values) + min = values.min + even_min = is_even(min) + other_min = even_min ? min + 6 : min + 7 + other_values = values.collect do | value | + if is_even(value) then + value + 6 - other_min + else + value + 7 - other_min + end + end + values.collect! { | value | value - min } + + if even_min then + [values, other_values] + else + [other_values, values] + end + end + + # produce a bitmask representation of an array of offset locations + def mask_for_offsets( offsets ) + mask = 0 + offsets.each { | value | mask = mask + ( 1 << value ) } + mask + end + + # finds a "safe" position that a position as described by a list of directions can be placed + # without falling off any edge of the board. the values returned a location to place the first piece + # at so it will fit after making the described moves + def start_adjust( directions ) + south = east = 0; + directions.each do | direction | + east += 1 if ( direction == :sw || direction == :nw || direction == :west ) + south += 1 if ( direction == :nw || direction == :ne ) + end + south * 6 + east + end + + # given a set of directions places the piece (as defined by a set of directions) on the board at + # a location that will not take it off the edge + def get_values( directions ) + start = start_adjust(directions) + values = [ start ] + directions.each do | direction | + if (start % 12 >= 6) then + start += @@rotation_odd_adder[direction] + else + start += @@rotation_even_adder[direction] + end + values += [ start ] + end + + # some moves take you back to an existing location, we'll strip duplicates + values.uniq + end + end + + # describes a piece and caches information about its rotations to as to be efficient for iteration + # ATTRIBUTES: + # rotations -- all the rotations of the piece + # type -- a numeic "name" of the piece + # masks -- an array by location of all legal rotational masks (a n inner array) for that location + # placed -- the mask that this piece was last placed at (not a location, but the actual mask used) + class Piece + attr_reader :rotations, :type, :masks + attr_accessor :placed + + # transform hashes that change one direction into another when you either flip or rotate a set of directions + @@flip_converter = { :west => :west, :east => :east, :nw => :sw, :ne => :se, :sw => :nw, :se => :ne } + @@rotate_converter = { :west => :nw, :east => :se, :nw => :ne, :ne => :east, :sw => :west, :se => :sw } + + def initialize( directions, type ) + @type = type + @rotations = Array.new(); + @map = {} + + generate_rotations( directions ) + directions.collect! { | value | @@flip_converter[value] } + generate_rotations( directions ) + + # creates the masks AND a map that returns [location, rotation] for any given mask + # this is used when a board is found and we want to draw it, otherwise the map is unused + @masks = Array.new(); + 0.upto(59) do | i | + even = true + @masks[i] = @rotations.collect do | rotation | + mask = rotation.start_masks[i] + @map[mask[0]] = [ i, rotation ] if (mask) + mask || nil + end + @masks[i].compact! + end + end + + # rotates a set of directions through all six angles and adds a Rotation to the list for each one + def generate_rotations( directions ) + 6.times do + rotations.push( Rotation.new(directions)) + directions.collect! { | value | @@rotate_converter[value] } + end + end + + # given a board string, adds this piece to the board at whatever location/rotation + # important: the outbound board string is 5 wide, the normal location notation is six wide (padded) + def fill_string( board_string) + location, rotation = @map[@placed] + rotation.offsets(location).each do | offset | + row, col = offset.divmod(6) + board_string[ row*5 + col, 1 ] = @type.to_s + end + end + end + + # a blank bit board having this form: + # + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 0 0 0 0 0 1 + # 1 1 1 1 1 1 + # + # where left lest significant bit is the top left and the most significant is the lower right + # the actual board only consists of the 0 places, the 1 places are blockers to keep things from running + # off the edges or bottom + def blank_board + 0b111111100000100000100000100000100000100000100000100000100000100000 + end + + def full_board + 0b111111111111111111111111111111111111111111111111111111111111111111 + end + + # determines if a location (bit position) is in an even row + def is_even( location) + (location % 12) < 6 + end + + # support function that create three utility maps: + # $converter -- for each row an array that maps a five bit row (via array mapping) + # to the a five bit representation of the bits below it + # $bit_count -- maps a five bit row (via array mapping) to the number of 1s in the row + # @@new_regions -- maps a five bit row (via array mapping) to an array of "region" arrays + # a region array has three values the first is a mask of bits in the region, + # the second is the count of those bits and the third is identical to the first + # examples: + # 0b10010 => [ 0b01100, 2, 0b01100 ], [ 0b00001, 1, 0b00001] + # 0b01010 => [ 0b10000, 1, 0b10000 ], [ 0b00100, 1, 0b00100 ], [ 0b00001, 1, 0b00001] + # 0b10001 => [ 0b01110, 3, 0b01110 ] + def create_collector_support + odd_map = [0b11, 0b110, 0b1100, 0b11000, 0b10000] + even_map = [0b1, 0b11, 0b110, 0b1100, 0b11000] + + all_odds = Array.new(0b100000) + all_evens = Array.new(0b100000) + bit_counts = Array.new(0b100000) + new_regions = Array.new(0b100000) + 0.upto(0b11111) do | i | + bit_count = odd = even = 0 + 0.upto(4) do | bit | + if (i[bit] == 1) then + bit_count += 1 + odd |= odd_map[bit] + even |= even_map[bit] + end + end + all_odds[i] = odd + all_evens[i] = even + bit_counts[i] = bit_count + new_regions[i] = create_regions( i) + end + + $converter = [] + 10.times { | row | $converter.push((row % 2 == 0) ? all_evens : all_odds) } + $bit_counts = bit_counts + $regions = new_regions.collect { | set | set.collect { | value | [ value, bit_counts[value], value] } } + end + + # determines if a board is punable, meaning that there is no possibility that it + # can be filled up with pieces. A board is prunable if there is a grouping of unfilled spaces + # that are not a multiple of five. The following board is an example of a prunable board: + # 0 0 1 0 0 + # 0 1 0 0 0 + # 1 1 0 0 0 + # 0 1 0 0 0 + # 0 0 0 0 0 + # ... + # + # This board is prunable because the top left corner is only 3 bits in area, no piece will ever fit it + # parameters: + # board -- an initial bit board (6 bit padded rows, see blank_board for format) + # location -- starting location, everything above and to the left is already full + # slotting -- set to true only when testing initial pieces, when filling normally + # additional assumptions are possible + # + # Algorithm: + # The algorithm starts at the top row (as determined by location) and iterates a row at a time + # maintainng counts of active open areas (kept in the collector array) each collector contains + # three values at the start of an iteration: + # 0: mask of bits that would be adjacent to the collector in this row + # 1: the number of bits collected so far + # 2: a scratch space starting as zero, but used during the computation to represent + # the empty bits in the new row that are adjacent (position 0) + # The exact procedure is described in-code + def prunable( board, location, slotting = false) + collectors = [] + # loop across the rows + (location / 6).to_i.upto(9) do | row_on | + # obtain a set of regions representing the bits of the current row. + regions = $regions[(board >> (row_on * 6)) & 0b11111] + converter = $converter[row_on] + + # track the number of collectors at the start of the cycle so that + # we don't compute against newly created collectors, only existing collectors + initial_collector_count = collectors.length + + # loop against the regions. For each region of the row + # we will see if it connects to one or more existing collectors. + # if it connects to 1 collector, the bits from the region are added to the + # bits of the collector and the mask is placed in collector[2] + # If the region overlaps more than one collector then all the collectors + # it overlaps with are merged into the first one (the others are set to nil in the array) + # if NO collectors are found then the region is copied as a new collector + regions.each do | region | + collector_found = nil + region_mask = region[2] + initial_collector_count.times do | collector_num | + collector = collectors[collector_num] + if (collector) then + collector_mask = collector[0] + if (collector_mask & region_mask != 0) then + if (collector_found) then + collector_found[0] |= collector_mask + collector_found[1] += collector[1] + collector_found[2] |= collector[2] + collectors[collector_num] = nil + else + collector_found = collector + collector[1] += region[1] + collector[2] |= region_mask + end + end + end + end + if (collector_found == nil) then + collectors.push(Array.new(region)) + end + end + + # check the existing collectors, if any collector overlapped no bits in the region its [2] value will + # be zero. The size of any such reaason is tested if it is not a multiple of five true is returned since + # the board is prunable. if it is a multiple of five it is removed. + # Collector that are still active have a new adjacent value [0] set based n the matched bits + # and have [2] cleared out for the next cycle. + collectors.length.times do | collector_num | + collector = collectors[collector_num] + if (collector) then + if (collector[2] == 0) then + return true if (collector[1] % 5 != 0) + collectors[collector_num] = nil + else + # if a collector matches all bits in the row then we can return unprunable early for the + # following reasons: + # 1) there can be no more unavailable bits bince we fill from the top left downward + # 2) all previous regions have been closed or joined so only this region can fail + # 3) this region must be good since there can never be only 1 region that is nuot + # a multiple of five + # this rule only applies when filling normally, so we ignore the rule if we are "slotting" + # in pieces to see what configurations work for them (the only other time this algorithm is used). + return false if (collector[2] == 0b11111 && !slotting) + collector[0] = converter[collector[2]] + collector[2] = 0 + end + end + end + + # get rid of all the empty converters for the next round + collectors.compact! + end + return false if (collectors.length <= 1) # 1 collector or less and the region is fine + collectors.any? { | collector | (collector[1] % 5) != 0 } # more than 1 and we test them all for bad size + end + + # creates a region given a row mask. see prunable for what a "region" is + def create_regions( value ) + regions = [] + cur_region = 0 + 5.times do | bit | + if (value[bit] == 0) then + cur_region |= 1 << bit + else + if (cur_region != 0 ) then + regions.push( cur_region) + cur_region = 0; + end + end + end + regions.push(cur_region) if (cur_region != 0) + regions + end + + # find up to the counted number of solutions (or all solutions) and prints the final result + def find_all + find_top( 1) + find_top( 0) + print_results + end + + # show the board + def print_results + print "#{@boards_found} solutions found\n\n" + print_full_board( @min_board) + print "\n" + print_full_board( @max_board) + print "\n" + end + + # finds solutions. This special version of the main function is only used for the top level + # the reason for it is basically to force a particular ordering on how the rotations are tested for + # the first piece. It is called twice, first looking for placements of the odd rotations and then + # looking for placements of the even locations. + # + # WHY? + # Since any found solution has an inverse we want to maximize finding solutions that are not already found + # as an inverse. The inverse will ALWAYS be 3 one of the piece configurations that is exactly 3 rotations away + # (an odd number). Checking even vs odd then produces a higher probability of finding more pieces earlier + # in the cycle. We still need to keep checking all the permutations, but our probability of finding one will + # diminsh over time. Since we are TOLD how many to search for this lets us exit before checking all pieces + # this bennifit is very great when seeking small numbers of solutions and is 0 when looking for more than the + # maximum number + def find_top( rotation_skip) + board = blank_board + (@pieces.length-1).times do + piece = @pieces.shift + piece.masks[0].each do | mask, imask, cmask | + if ((rotation_skip += 1) % 2 == 0) then + piece.placed = mask + find( 1, 1, board | mask) + end + end + @pieces.push(piece) + end + piece = @pieces.shift + @pieces.push(piece) + end + + # the normail find routine, iterates through the available pieces, checks all rotations at the current location + # and adds any boards found. depth is achieved via recursion. the overall approach is described + # here: http://www-128.ibm.com/developerworks/java/library/j-javaopt/ + # parameters: + # start_location -- where to start looking for place for the next piece at + # placed -- number of pieces placed + # board -- current state of the board + # + # see in-code comments + def find( start_location, placed, board) + # find the next location to place a piece by looking for an empty bit + while board[start_location] == 1 + start_location += 1 + end + + @pieces.length.times do + piece = @pieces.shift + piece.masks[start_location].each do | mask, imask, cmask | + if ( board & cmask == imask) then + piece.placed = mask + if (placed == 9) then + add_board + else + find( start_location + 1, placed + 1, board | mask) + end + end + end + @pieces.push(piece) + end + end + + # print the board + def print_full_board( board_string) + 10.times do | row | + print " " if (row % 2 == 1) + 5.times do | col | + print "#{board_string[row*5 + col,1]} " + end + print "\n" + end + end + + # when a board is found we "draw it" into a string and then flip that string, adding both to + # the list (hash) of solutions if they are unique. + def add_board + board_string = "99999999999999999999999999999999999999999999999999" + @all_pieces.each { | piece | piece.fill_string( board_string ) } + save( board_string) + save( board_string.reverse) + end + + # adds a board string to the list (if new) and updates the current best/worst board + def save( board_string) + if (@all_boards[board_string] == nil) then + @min_board = board_string if (board_string < @min_board) + @max_board = board_string if (board_string > @max_board) + @all_boards.store(board_string,true) + @boards_found += 1 + + # the exit motif is a time saver. Ideally the function should return, but those tests + # take noticeable time (performance). + if (@boards_found == @stop_count) then + print_results + exit(0) + end + end + end + + + ## + ## MAIN BODY :) + ## + create_collector_support + @pieces = [ + Piece.new( [ :nw, :ne, :east, :east ], 2), + Piece.new( [ :ne, :se, :east, :ne ], 7), + Piece.new( [ :ne, :east, :ne, :nw ], 1), + Piece.new( [ :east, :sw, :sw, :se ], 6), + Piece.new( [ :east, :ne, :se, :ne ], 5), + Piece.new( [ :east, :east, :east, :se ], 0), + Piece.new( [ :ne, :nw, :se, :east, :se ], 4), + Piece.new( [ :se, :se, :se, :west ], 9), + Piece.new( [ :se, :se, :east, :se ], 8), + Piece.new( [ :east, :east, :sw, :se ], 3) + ]; + + @all_pieces = Array.new( @pieces) + + @min_board = "99999999999999999999999999999999999999999999999999" + @max_board = "00000000000000000000000000000000000000000000000000" + @stop_count = ARGV[0].to_i || 2089 + @all_boards = {} + @boards_found = 0 + + find_all ######## DO IT!!! +loop_count: 1 diff --git a/benchmark/so_nbody.yml b/benchmark/so_nbody.yml new file mode 100644 index 0000000000..e4d111cd38 --- /dev/null +++ b/benchmark/so_nbody.yml @@ -0,0 +1,152 @@ +prelude: | + # The Computer Language Shootout + # http://shootout.alioth.debian.org +benchmark: + so_nbody: | + # + # 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) +loop_count: 1 diff --git a/benchmark/so_nested_loop.yml b/benchmark/so_nested_loop.yml new file mode 100644 index 0000000000..64a6901eac --- /dev/null +++ b/benchmark/so_nested_loop.yml @@ -0,0 +1,26 @@ +prelude: | + #!/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 +benchmark: + so_nested_loop: | + + 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 +loop_count: 1 diff --git a/benchmark/so_nsieve.yml b/benchmark/so_nsieve.yml new file mode 100644 index 0000000000..0799cff7a7 --- /dev/null +++ b/benchmark/so_nsieve.yml @@ -0,0 +1,39 @@ +prelude: | + # The Computer Language Shootout + # http://shootout.alioth.debian.org/ +benchmark: + so_nsieve: | + # + # contributed by Glenn Parker, March 2005 + # modified by Evan Phoenix, Sept 2006 + + def sieve(m) + flags = Flags.dup[0,m] + count = 0 + pmax = m - 1 + p = 2 + while p <= pmax + unless flags[p].zero? + count += 1 + mult = p + while mult <= pmax + flags[mult] = 0 + mult += p + end + end + p += 1 + end + count + end + + n = 9 # (ARGV[0] || 2).to_i + Flags = ("\x1" * ( 2 ** n * 10_000)).unpack("c*") + + n.downto(n-2) do |exponent| + break if exponent < 0 + m = (1 << exponent) * 10_000 + # m = (2 ** exponent) * 10_000 + count = sieve(m) + printf "Primes up to %8d %8d\n", m, count + end +loop_count: 1 diff --git a/benchmark/so_nsieve_bits.yml b/benchmark/so_nsieve_bits.yml new file mode 100644 index 0000000000..08b47f53bb --- /dev/null +++ b/benchmark/so_nsieve_bits.yml @@ -0,0 +1,46 @@ +prelude: | + #!/usr/bin/ruby + #coding: us-ascii +benchmark: + so_nsieve_bits: | + # + # 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 +loop_count: 1 diff --git a/benchmark/so_object.yml b/benchmark/so_object.yml new file mode 100644 index 0000000000..89fbce83cd --- /dev/null +++ b/benchmark/so_object.yml @@ -0,0 +1,59 @@ +prelude: | + #!/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 +benchmark: + so_object: | + + 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 +loop_count: 1 diff --git a/benchmark/so_partial_sums.yml b/benchmark/so_partial_sums.yml new file mode 100644 index 0000000000..d45017bca8 --- /dev/null +++ b/benchmark/so_partial_sums.yml @@ -0,0 +1,34 @@ +benchmark: + so_partial_sums: | + 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 +loop_count: 1 diff --git a/benchmark/so_pidigits.yml b/benchmark/so_pidigits.yml new file mode 100644 index 0000000000..793745f5b5 --- /dev/null +++ b/benchmark/so_pidigits.yml @@ -0,0 +1,95 @@ +prelude: | + # The Great Computer Language Shootout + # http://shootout.alioth.debian.org/ +benchmark: + so_pidigits: | + # + # contributed by Gabriele Renzi + + 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 + n = 2_500 # Integer(ARGV[0]) + j = 0 + + digits = PiDigitSpigot.new + + while n > 0 + if n >= WIDTH + WIDTH.times {print digits.next!} + j += WIDTH + else + n.times {print digits.next!} + (WIDTH-n).times {print " "} + j += n + end + puts "\t:"+j.to_s + n -= WIDTH + end +loop_count: 1 diff --git a/benchmark/so_random.yml b/benchmark/so_random.yml new file mode 100644 index 0000000000..3a39453377 --- /dev/null +++ b/benchmark/so_random.yml @@ -0,0 +1,24 @@ +prelude: | + # from http://www.bagley.org/~doug/shootout/bench/random/random.ruby +benchmark: + so_random: | + + 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<N + i +=1 + gen_random(100.0) + end + # "%.9f" % gen_random(100.0) +loop_count: 1 diff --git a/benchmark/so_sieve.yml b/benchmark/so_sieve.yml new file mode 100644 index 0000000000..ae90f8f6fb --- /dev/null +++ b/benchmark/so_sieve.yml @@ -0,0 +1,28 @@ +prelude: | + # from http://www.bagley.org/~doug/shootout/bench/sieve/sieve.ruby +benchmark: + so_sieve: | + num = 500 + count = i = j = 0 + flags0 = Array.new(8192,1) + k = 0 + while k < num + k += 1 + count = 0 + flags = flags0.dup + i = 2 + while i<8192 + i += 1 + if flags[i] + # remove all multiples of prime: i + j = i*i + while j < 8192 + j += i + flags[j] = nil + end + count += 1 + end + end + end + count +loop_count: 1 diff --git a/benchmark/so_spectralnorm.yml b/benchmark/so_spectralnorm.yml new file mode 100644 index 0000000000..45373b6476 --- /dev/null +++ b/benchmark/so_spectralnorm.yml @@ -0,0 +1,54 @@ +prelude: | + # The Computer Language Shootout + # http://shootout.alioth.debian.org/ + # Contributed by Sokolov Yura +benchmark: + so_spectralnorm: | + + def eval_A(i,j) + return 1.0/((i+j)*(i+j+1)/2+i+1) + end + + def eval_A_times_u(u) + v, i = nil, nil + (0..u.length-1).collect { |i| + v = 0 + for j in 0..u.length-1 + v += eval_A(i,j)*u[j] + end + v + } + end + + def eval_At_times_u(u) + v, i = nil, nil + (0..u.length-1).collect{|i| + v = 0 + for j in 0..u.length-1 + v += eval_A(j,i)*u[j] + end + v + } + end + + def eval_AtA_times_u(u) + return eval_At_times_u(eval_A_times_u(u)) + end + + n = 500 # ARGV[0].to_i + + u=[1]*n + for i in 1..10 + v=eval_AtA_times_u(u) + u=eval_AtA_times_u(v) + end + vBv=0 + vv=0 + for i in 0..n-1 + vBv += u[i]*v[i] + vv += v[i]*v[i] + end + + str = "%0.9f" % (Math.sqrt(vBv/vv)), "\n" + # print str +loop_count: 1 diff --git a/benchmark/string_index.yml b/benchmark/string_index.yml new file mode 100644 index 0000000000..f89193a448 --- /dev/null +++ b/benchmark/string_index.yml @@ -0,0 +1,6 @@ +benchmark: + string_index: | + str1 = "あ" * 1024 + "い" # not single byte optimizable + str2 = "い" + 100_000.times { str1.index(str2) } +loop_count: 1 diff --git a/benchmark/string_scan_re.yml b/benchmark/string_scan_re.yml new file mode 100644 index 0000000000..a1d249ff52 --- /dev/null +++ b/benchmark/string_scan_re.yml @@ -0,0 +1,5 @@ +benchmark: + string_scan_re: | + str = Array.new(1_000, 'abc').join(',') + 1_000.times { str.scan(/abc/) } +loop_count: 1 diff --git a/benchmark/string_scan_str.yml b/benchmark/string_scan_str.yml new file mode 100644 index 0000000000..3c1bae451a --- /dev/null +++ b/benchmark/string_scan_str.yml @@ -0,0 +1,5 @@ +benchmark: + string_scan_str: | + str = Array.new(1_000, 'abc').join(',') + 1_000.times { str.scan('abc') } +loop_count: 1 diff --git a/benchmark/time_subsec.yml b/benchmark/time_subsec.yml new file mode 100644 index 0000000000..ee787639c6 --- /dev/null +++ b/benchmark/time_subsec.yml @@ -0,0 +1,5 @@ +benchmark: + time_subsec: | + t = Time.now + 4000000.times { t.subsec } +loop_count: 1 diff --git a/benchmark/vm3_backtrace.yml b/benchmark/vm3_backtrace.yml new file mode 100644 index 0000000000..58df4bc191 --- /dev/null +++ b/benchmark/vm3_backtrace.yml @@ -0,0 +1,26 @@ +prelude: | + # get last backtrace +benchmark: + vm3_backtrace: | + + begin + caller(0, 0) + rescue ArgumentError + alias caller_orig caller + def caller lev, n + caller_orig(lev)[0..n] + end + end + + def rec n + if n < 0 + 100_000.times{ + caller(0, 1) + } + else + rec(n-1) + end + end + + rec 50 +loop_count: 1 diff --git a/benchmark/vm3_clearmethodcache.yml b/benchmark/vm3_clearmethodcache.yml new file mode 100644 index 0000000000..63dea7aa58 --- /dev/null +++ b/benchmark/vm3_clearmethodcache.yml @@ -0,0 +1,11 @@ +benchmark: + vm3_clearmethodcache: | + i = 0 + while i<200_000 + i += 1 + + Class.new{ + def m; end + } + end +loop_count: 1 diff --git a/benchmark/vm3_gc.yml b/benchmark/vm3_gc.yml new file mode 100644 index 0000000000..5ca177a79a --- /dev/null +++ b/benchmark/vm3_gc.yml @@ -0,0 +1,9 @@ +benchmark: + vm3_gc: | + 5000.times do + 100.times do + {"xxxx"=>"yyyy"} + end + GC.start + end +loop_count: 1 diff --git a/benchmark/vm3_gc_old_full.yml b/benchmark/vm3_gc_old_full.yml new file mode 100644 index 0000000000..11958c2d0a --- /dev/null +++ b/benchmark/vm3_gc_old_full.yml @@ -0,0 +1,7 @@ +benchmark: + vm3_gc_old_full: | + old_object = Array.new(1_000_000){''} + 100.times do + GC.start + end +loop_count: 1 diff --git a/benchmark/vm3_gc_old_immediate.yml b/benchmark/vm3_gc_old_immediate.yml new file mode 100644 index 0000000000..3aea9c05f9 --- /dev/null +++ b/benchmark/vm3_gc_old_immediate.yml @@ -0,0 +1,7 @@ +benchmark: + vm3_gc_old_immediate: | + old_object = Array.new(1_000_000){''} + 30_000.times do + GC.start(full_mark: false, immediate_sweep: true) + end +loop_count: 1 diff --git a/benchmark/vm3_gc_old_lazy.yml b/benchmark/vm3_gc_old_lazy.yml new file mode 100644 index 0000000000..37ad5c38f1 --- /dev/null +++ b/benchmark/vm3_gc_old_lazy.yml @@ -0,0 +1,7 @@ +benchmark: + vm3_gc_old_lazy: | + old_object = Array.new(1_000_000){''} + 30_000.times do + GC.start(full_mark: false, immediate_sweep: false) + end +loop_count: 1 diff --git a/benchmark/vm_symbol_block_pass.yml b/benchmark/vm_symbol_block_pass.yml new file mode 100644 index 0000000000..c690b5944e --- /dev/null +++ b/benchmark/vm_symbol_block_pass.yml @@ -0,0 +1,16 @@ +benchmark: + vm_symbol_block_pass: | + class C + 1000.times {|i| + eval("def i#{i};end") + } + end + + c = C.new + m = C.instance_methods(false) + 5_000.times do + m.each do |n| + c.tap(&n) + end + end +loop_count: 1 diff --git a/benchmark/vm_thread_alive_check1.yml b/benchmark/vm_thread_alive_check1.yml new file mode 100644 index 0000000000..4ab97401da --- /dev/null +++ b/benchmark/vm_thread_alive_check1.yml @@ -0,0 +1,9 @@ +benchmark: + vm_thread_alive_check1: | + 5_000.times{ + t = Thread.new{} + while t.alive? + Thread.pass + end + } +loop_count: 1 diff --git a/benchmark/vm_thread_close.yml b/benchmark/vm_thread_close.yml new file mode 100644 index 0000000000..4867f1ca2a --- /dev/null +++ b/benchmark/vm_thread_close.yml @@ -0,0 +1,9 @@ +benchmark: + vm_thread_close: | + 1000.times { Thread.new { sleep } } + i = 0 + while i<100_000 # benchmark loop 3 + i += 1 + IO.pipe.each(&:close) + end +loop_count: 1 diff --git a/benchmark/vm_thread_condvar1.yml b/benchmark/vm_thread_condvar1.yml new file mode 100644 index 0000000000..1f481ab757 --- /dev/null +++ b/benchmark/vm_thread_condvar1.yml @@ -0,0 +1,32 @@ +prelude: | + # two threads, two mutex, two condvar ping-pong +benchmark: + vm_thread_condvar1: | + require 'thread' + m1 = Mutex.new + m2 = Mutex.new + cv1 = ConditionVariable.new + cv2 = ConditionVariable.new + max = 100000 + i = 0 + wait = nil + m2.synchronize do + wait = Thread.new do + m1.synchronize do + m2.synchronize { cv2.signal } + while (i += 1) < max + cv1.wait(m1) + cv2.signal + end + end + end + cv2.wait(m2) + end + m1.synchronize do + while i < max + cv1.signal + cv2.wait(m1) + end + end + wait.join +loop_count: 1 diff --git a/benchmark/vm_thread_condvar2.yml b/benchmark/vm_thread_condvar2.yml new file mode 100644 index 0000000000..5b5798c7c5 --- /dev/null +++ b/benchmark/vm_thread_condvar2.yml @@ -0,0 +1,39 @@ +prelude: | + # many threads, one mutex, many condvars +benchmark: + vm_thread_condvar2: | + require 'thread' + m = Mutex.new + cv1 = ConditionVariable.new + cv2 = ConditionVariable.new + max = 1000 + n = 100 + waiting = 0 + scvs = [] + waiters = n.times.map do |i| + start_cv = ConditionVariable.new + scvs << start_cv + start_mtx = Mutex.new + start_mtx.synchronize do + th = Thread.new(start_mtx, start_cv) do |sm, scv| + m.synchronize do + sm.synchronize { scv.signal } + max.times do + cv2.signal if (waiting += 1) == n + cv1.wait(m) + end + end + end + start_cv.wait(start_mtx) + th + end + end + m.synchronize do + max.times do + cv2.wait(m) until waiting == n + waiting = 0 + cv1.broadcast + end + end + waiters.each(&:join) +loop_count: 1 diff --git a/benchmark/vm_thread_create_join.yml b/benchmark/vm_thread_create_join.yml new file mode 100644 index 0000000000..7fe82b3fb0 --- /dev/null +++ b/benchmark/vm_thread_create_join.yml @@ -0,0 +1,9 @@ +benchmark: + vm_thread_create_join: | + i = 0 + while i<100_000 # benchmark loop 3 + i += 1 + Thread.new{ + }.join + end +loop_count: 1 diff --git a/benchmark/vm_thread_mutex1.yml b/benchmark/vm_thread_mutex1.yml new file mode 100644 index 0000000000..33ab7a2651 --- /dev/null +++ b/benchmark/vm_thread_mutex1.yml @@ -0,0 +1,25 @@ +prelude: | + # one thread, one mutex (no contention) +benchmark: + vm_thread_mutex1: | + + require 'thread' + m = Thread::Mutex.new + r = 0 + max = 2000 + lmax = max * max + (1..1).map{ + Thread.new{ + i = 0 + while i<lmax + i += 1 + m.synchronize{ + r += 1 + } + end + } + }.each{|e| + e.join + } + raise r.to_s if r != max * max +loop_count: 1 diff --git a/benchmark/vm_thread_mutex2.yml b/benchmark/vm_thread_mutex2.yml new file mode 100644 index 0000000000..e6abe59635 --- /dev/null +++ b/benchmark/vm_thread_mutex2.yml @@ -0,0 +1,25 @@ +prelude: | + # two threads, one mutex +benchmark: + vm_thread_mutex2: | + + require 'thread' + m = Thread::Mutex.new + r = 0 + max = 2000 + lmax = (max * max)/2 + (1..2).map{ + Thread.new{ + i = 0 + while i<lmax + i += 1 + m.synchronize{ + r += 1 + } + end + } + }.each{|e| + e.join + } + raise r.to_s if r != max * max +loop_count: 1 diff --git a/benchmark/vm_thread_mutex3.yml b/benchmark/vm_thread_mutex3.yml new file mode 100644 index 0000000000..99f3b81f99 --- /dev/null +++ b/benchmark/vm_thread_mutex3.yml @@ -0,0 +1,24 @@ +prelude: | + # 1000 threads, one mutex +benchmark: + vm_thread_mutex3: | + + require 'thread' + m = Thread::Mutex.new + r = 0 + max = 2000 + (1..max).map{ + Thread.new{ + i = 0 + while i<max + i += 1 + m.synchronize{ + r += 1 + } + end + } + }.each{|e| + e.join + } + raise r.to_s if r != max * max +loop_count: 1 diff --git a/benchmark/vm_thread_pass.yml b/benchmark/vm_thread_pass.yml new file mode 100644 index 0000000000..c5f264ee36 --- /dev/null +++ b/benchmark/vm_thread_pass.yml @@ -0,0 +1,17 @@ +prelude: | + # Plenty Thtread.pass + # A performance may depend on GVL implementation. +benchmark: + vm_thread_pass: | + + tmax = (ARGV.shift || 2).to_i + lmax = 200_000 / tmax + + (1..tmax).map{ + Thread.new{ + lmax.times{ + Thread.pass + } + } + }.each{|t| t.join} +loop_count: 1 diff --git a/benchmark/vm_thread_pass_flood.yml b/benchmark/vm_thread_pass_flood.yml new file mode 100644 index 0000000000..953288ebe4 --- /dev/null +++ b/benchmark/vm_thread_pass_flood.yml @@ -0,0 +1,14 @@ +prelude: | + # n.b. this is a good test for GVL when pinned to a single CPU +benchmark: + vm_thread_pass_flood: | + + 1000.times{ + Thread.new{loop{Thread.pass}} + } + + i = 0 + while i<10000 + i += 1 + end +loop_count: 1 diff --git a/benchmark/vm_thread_pipe.yml b/benchmark/vm_thread_pipe.yml new file mode 100644 index 0000000000..fe4f3b8166 --- /dev/null +++ b/benchmark/vm_thread_pipe.yml @@ -0,0 +1,20 @@ +prelude: | + # Measure small and plenty pipe read/write. + # A performance may depend on GVL implementation. +benchmark: + vm_thread_pipe: | + + lmax = 100_000 + r, w = IO.pipe + [Thread.new{ + lmax.times{ + w.write('a') + } + p "w:exit" + }, Thread.new{ + lmax.times{ + r.read(1) + } + p "r:exit" + }].each{|t| t.join} +loop_count: 1 diff --git a/benchmark/vm_thread_queue.yml b/benchmark/vm_thread_queue.yml new file mode 100644 index 0000000000..1294ad3a13 --- /dev/null +++ b/benchmark/vm_thread_queue.yml @@ -0,0 +1,21 @@ +benchmark: + vm_thread_queue: | + require 'thread' + + n = 1_000_000 + q = Thread::Queue.new + consumer = Thread.new{ + while q.pop + # consuming + end + } + + producer = Thread.new{ + n.times{ + q.push true + } + q.push nil + } + + consumer.join +loop_count: 1 diff --git a/benchmark/vm_thread_sized_queue.yml b/benchmark/vm_thread_sized_queue.yml new file mode 100644 index 0000000000..0a79bd4fc1 --- /dev/null +++ b/benchmark/vm_thread_sized_queue.yml @@ -0,0 +1,23 @@ +benchmark: + vm_thread_sized_queue: | + require 'thread' + # on producer, one consumer + + n = 1_000_000 + q = Thread::SizedQueue.new(100) + consumer = Thread.new{ + while q.pop + # consuming + end + } + + producer = Thread.new{ + while n > 0 + q.push true + n -= 1 + end + q.push nil + } + + consumer.join +loop_count: 1 diff --git a/benchmark/vm_thread_sized_queue2.yml b/benchmark/vm_thread_sized_queue2.yml new file mode 100644 index 0000000000..7a5c2251c1 --- /dev/null +++ b/benchmark/vm_thread_sized_queue2.yml @@ -0,0 +1,26 @@ +benchmark: + vm_thread_sized_queue2: | + require 'thread' + # one producer, many consumers + n = 1_000_000 + m = 10 + q = Thread::SizedQueue.new(100) + consumers = m.times.map do + Thread.new do + while q.pop + # consuming + end + end + end + + producer = Thread.new do + while n > 0 + q.push true + n -= 1 + end + m.times { q.push nil } + end + + producer.join + consumers.each(&:join) +loop_count: 1 diff --git a/benchmark/vm_thread_sized_queue3.yml b/benchmark/vm_thread_sized_queue3.yml new file mode 100644 index 0000000000..6eefe50984 --- /dev/null +++ b/benchmark/vm_thread_sized_queue3.yml @@ -0,0 +1,25 @@ +benchmark: + vm_thread_sized_queue3: | + require 'thread' + # many producers, one consumer + n = 1_000_000 + m = 10 + q = Thread::SizedQueue.new(100) + consumer = Thread.new do + while q.pop + # consuming + end + end + + producers = m.times.map do + Thread.new do + while n > 0 + q.push true + n -= 1 + end + end + end + producers.each(&:join) + q.push nil + consumer.join +loop_count: 1 diff --git a/benchmark/vm_thread_sized_queue4.yml b/benchmark/vm_thread_sized_queue4.yml new file mode 100644 index 0000000000..2af8ea4ebe --- /dev/null +++ b/benchmark/vm_thread_sized_queue4.yml @@ -0,0 +1,29 @@ +benchmark: + vm_thread_sized_queue4: | + require 'thread' + # many producers, many consumers + nr = 1_000_000 + n = 10 + m = 10 + q = Thread::SizedQueue.new(100) + consumers = n.times.map do + Thread.new do + while q.pop + # consuming + end + end + end + + producers = m.times.map do + Thread.new do + while nr > 0 + q.push true + nr -= 1 + end + end + end + + producers.each(&:join) + n.times { q.push nil } + consumers.each(&:join) +loop_count: 1 |