Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Brian Ford's refactoring and new drivers. See the README and

utils/README for more information.

Examples of various ways to run the benchmarks:
  rake bench
  rake bench:dir DIR=benchmark/rbs/core-features
  rake bench:file FILE=benchmark/rbs/core-features/bm_app_tak.rb
  rake bench:to_csv

The :to_csv task creates a .csv spreadsheet of the results. Quite handy
for comparing your results over time.

I'd welcome other output formatters. :to_wiki comes to mind so I could put
the results directly in Confluence.
  • Loading branch information...
commit be1955ecd8ecd25e36b68571cb6682bc6233d72f 1 parent 200a519
Monty Williams authored
Showing with 105,794 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +22 −0 README
  3. +153 −0 bench.rake
  4. +11 −0 rbs/core-features/bm_app_factorial.rb
  5. +11 −0 rbs/core-features/bm_app_fib.rb
  6. +12 −0 rbs/core-features/bm_app_raise.rb
  7. +13 −0 rbs/core-features/bm_app_tak.rb
  8. +12 −0 rbs/core-features/bm_app_tarai.rb
  9. +3 −0  rbs/core-features/bm_loop_times.rb
  10. +10 −0 rbs/core-features/bm_loop_whileloop.rb
  11. +13 −0 rbs/core-features/bm_so_ackermann.rb
  12. +25 −0 rbs/core-features/bm_so_nested_loop.rb
  13. +61 −0 rbs/core-features/bm_so_object.rb
  14. +9 −0 rbs/core-features/bm_so_random.rb
  15. +7 −0 rbs/core-features/bm_vm1_block.rb
  16. +10 −0 rbs/core-features/bm_vm1_const.rb
  17. +12 −0 rbs/core-features/bm_vm1_ensure.rb
  18. +14 −0 rbs/core-features/bm_vm1_length.rb
  19. +11 −0 rbs/core-features/bm_vm1_rescue.rb
  20. +7 −0 rbs/core-features/bm_vm1_simplereturn.rb
  21. +7 −0 rbs/core-features/bm_vm1_swap.rb
  22. +20 −0 rbs/core-features/bm_vm2_poly_method.rb
  23. +10 −0 rbs/core-features/bm_vm2_proc.rb
  24. +9 −0 rbs/core-features/bm_vm2_send.rb
  25. +16 −0 rbs/core-features/bm_vm2_super.rb
  26. +6 −0 rbs/core-features/bm_vm2_unif1.rb
  27. +16 −0 rbs/core-features/bm_vm2_zsuper.rb
  28. +3 −0  rbs/core-library/bm_app_strconcat.rb
  29. +28 −0 rbs/core-library/bm_mbari_bogus1.rb
  30. +31 −0 rbs/core-library/bm_mbari_bogus2.rb
  31. +37 −0 rbs/core-library/bm_pathname.rb
  32. +20 −0 rbs/core-library/bm_so_array.rb
  33. +25 −0 rbs/core-library/bm_so_count_words.rb
  34. +53 −0 rbs/core-library/bm_so_exception.rb
  35. +40 −0 rbs/core-library/bm_so_lists.rb
  36. +40 −0 rbs/core-library/bm_so_lists_small.rb
  37. +45 −0 rbs/core-library/bm_so_matrix.rb
  38. +3 −0  rbs/core-library/bm_vm2_array.rb
  39. +8 −0 rbs/core-library/bm_vm2_regexp.rb
  40. +3 −0  rbs/core-library/bm_vm3_thread_create_join.rb
  41. +259 −0 rbs/core-library/wc.input
  42. +25 −0 rbs/core-library/wc.input.base
  43. +5 −0 rbs/garbage_collection/bm_gc_100mb.rb
  44. +5 −0 rbs/garbage_collection/bm_gc_400mb.rb
  45. +5 −0 rbs/garbage_collection/bm_gc_50mb.rb
  46. +17 −0 rbs/garbage_collection/bm_gc_array_800mb.rb
  47. +7 −0 rbs/garbage_collection/bm_gc_cow_100mb.rb
  48. +19 −0 rbs/garbage_collection/bm_gc_string_50mb.rb
  49. +28 −0 rbs/micro-benchmarks/LICENSE
  50. +128 −0 rbs/micro-benchmarks/bm_app_pentomino.rb
  51. +54 −0 rbs/micro-benchmarks/bm_binary_trees.rb
  52. +9 −0 rbs/micro-benchmarks/bm_count_multithreaded.rb
  53. +9 −0 rbs/micro-benchmarks/bm_count_shared_thread.rb
  54. +42 −0 rbs/micro-benchmarks/bm_fannkuch.rb
  55. +79 −0 rbs/micro-benchmarks/bm_fasta.rb
  56. +44 −0 rbs/micro-benchmarks/bm_fractal.rb
  57. +41 −0 rbs/micro-benchmarks/bm_knucleotide.rb
  58. +15 −0 rbs/micro-benchmarks/bm_lucas_lehmer.rb
  59. +61 −0 rbs/micro-benchmarks/bm_mandelbrot.rb
  60. +42 −0 rbs/micro-benchmarks/bm_mergesort.rb
  61. +54 −0 rbs/micro-benchmarks/bm_mergesort_hongli.rb
  62. +557 −0 rbs/micro-benchmarks/bm_meteor_contest.rb
  63. +17 −0 rbs/micro-benchmarks/bm_monte_carlo_pi.rb
  64. +144 −0 rbs/micro-benchmarks/bm_nbody.rb
  65. +37 −0 rbs/micro-benchmarks/bm_nsieve.rb
  66. +42 −0 rbs/micro-benchmarks/bm_nsieve_bits.rb
  67. +39 −0 rbs/micro-benchmarks/bm_partial_sums.rb
  68. +14 −0 rbs/micro-benchmarks/bm_quicksort.rb
  69. +39 −0 rbs/micro-benchmarks/bm_regex_dna.rb
  70. +25 −0 rbs/micro-benchmarks/bm_reverse_compliment.rb
  71. +26 −0 rbs/micro-benchmarks/bm_so_sieve.rb
  72. +48 −0 rbs/micro-benchmarks/bm_spectral_norm.rb
  73. +10 −0 rbs/micro-benchmarks/bm_sum_file.rb
  74. +31 −0 rbs/micro-benchmarks/bm_word_anagrams.rb
  75. +2,500 −0 rbs/micro-benchmarks/fasta.input
  76. +100,000 −0 rbs/micro-benchmarks/random.input
  77. +15 −0 rbs/real-world/bm_hilbert_matrix.rb
  78. +59 −0 rbs/real-world/bm_sudoku.rb
  79. +14 −0 rbs/real-world/hilbert.rb
  80. +24 −0 rbs/standard-library/bm_app_mandelbrot.rb
  81. +100 −0 utils/README
  82. +156 −0 utils/bench.rb
  83. +30 −0 utils/monitor.rb
  84. +61 −0 utils/timeout
22 LICENSE
View
@@ -0,0 +1,22 @@
+Except where otherwise specified, all the files within the Ruby Benchmark Suite
+project are released under the MIT License.
+
+Copyright (c) 2008 Antonio Cangiano <acangiano@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
22 README
View
@@ -0,0 +1,22 @@
+This directory contains a copy of Brian Ford's refactoring of the
+Ruby Benchmark Suite project as used by the Rubinius project. We made
+a one line change to bench.rb to ensure it runs in MagLev as well as
+other Ruby implementations.
+
+Subdirectories:
+ rbs - contains the benchmarks themselves
+ utils - contains the infrastructure necessary to run the benchmarks
+
+see $MAGLEV_HOME/benchmark/utils/README for more detail.
+
+Benchmarks are run using rake. Type "rake -T bench" to see options.
+
+Examples:
+ rake bench
+ rake bench:file FILE=benchmark/rbs/core-features/bm_app_tak.rb
+ rake bench:dir DIR=benchmark/rbs/core-features
+ rake bench:to_csv
+
+Note: You'll probably want to copy bench.rake to your rakelib and modify
+some filenames and locations to suit your needs.
+
153 bench.rake
View
@@ -0,0 +1,153 @@
+# Some tasks to automate running the Ruby Benchmark Suite (RBS).
+# The results are output to YAML and processed by the :to_csv
+# task into a .csv spreadsheet
+#
+# The :run task does not depend on the :update tasks so the
+# updates are done manually to ensure stability.
+#
+# see utils/README for more information
+
+# You'll want to copy this file to your rakelib and modify the
+# paths to reflect the layout you use and change the default VM.
+
+BASEDIR = File.expand_path(File.dirname(__FILE__) + "/..")
+MONITOR = BASEDIR + "/benchmark/utils/monitor.rb"
+RUNNER = BASEDIR + "/benchmark/utils/bench.rb"
+RBS_DIR = BASEDIR + "/benchmark/rbs"
+RESULTS_DIR = BASEDIR + "/benchmark/results"
+RBS_RESULTS_DIR = RESULTS_DIR + "/rbs"
+WEB_DIR = RESULTS_DIR + "/web"
+
+ITERATIONS = (ENV['ITERATIONS'] || 5).to_i
+TIMEOUT = (ENV['TIMEOUT'] || 300).to_i
+VM = ENV['VM'] || "ruby"
+
+def command(name)
+ "ruby #{MONITOR} #{TIMEOUT} '#{VM}' #{RUNNER} #{name} #{ITERATIONS} #{report}"
+end
+
+# Cache the name so it is only generated once during an invocation.
+# Eliminates having to save the name and pass it around.
+def report
+ os = `uname`.chomp
+ host = `uname -n`.chomp
+ vm = File.basename VM.split.first
+ @report ||= "#{RBS_RESULTS_DIR}/RBS-#{vm}-#{os}-#{host}-#{Time.now.strftime "%y%m%d.%H%M%S"}.yaml"
+end
+
+def report_name
+ report[(BASEDIR.size+1)..-1]
+end
+
+desc "Run all the RBS benchmarks"
+task :bench => 'bench:run'
+
+namespace :bench do
+ desc "Plot the RBS benchmark results (not implemented)"
+ task :results => :setup do
+ end
+
+ desc "Generate a CSV file of RBS results"
+ task :to_csv => :setup do
+ require 'yaml'
+
+ field = ENV['FIELD'] || "min"
+ unless ["max", "min", "median", "mean"].include?(field)
+ raise "FIELD must be one of max, min, median, mean"
+ end
+
+ dir = ENV['RESULTS'] || RBS_RESULTS_DIR
+
+ header = ["Benchmark File", "Input Size"]
+ data = Hash.new { |h,k| h[k] = {} }
+ status = Hash.new { |h,k| h[k] = {} }
+
+ puts "Creating Ruby Benchmark spreadsheet"
+ csv_report = "#{dir}/RBS-#{Time.now.strftime "%y%m%d.%H%M"}.csv"
+ puts " Writing spreadsheet to #{csv_report}"
+
+ Dir[dir + "/**/*.yaml"].sort.each do |name|
+ puts " Processing #{File.basename name}"
+ system = File.basename name, ".yaml"
+ header << system
+
+ File.open name, "r" do |file|
+ YAML.load_documents file do |doc|
+ canonical_name = doc["name"].gsub '//', '/'
+ bench_name = File.basename(File.dirname(canonical_name)) + '/' + File.basename(canonical_name)
+ status[bench_name][system] ||= doc["status"]
+
+ next unless doc.key? field
+
+ bench = [bench_name, doc["parameter"]]
+ data[bench]["Input Size"] = doc["parameter"]
+ data[bench][system] = doc[field]
+ end
+ end
+ end
+
+ File.open csv_report, "w" do |file|
+ file.puts(header.map { |h| h.inspect }.join(","))
+ header.shift
+
+ data.keys.sort.each do |key|
+ file.print key.first.inspect, ","
+ line = header.map do |h|
+ (data[key][h] || status[key.first][h]).inspect
+ end
+ file.puts line.join(",")
+ end
+ end
+ puts "Done"
+ end
+
+ # Not public. Creates directories for results, etc.
+ task :setup do
+ mkdir_p RBS_RESULTS_DIR, :verbose => $verbose
+ mkdir_p WEB_DIR, :verbose => $verbose
+ end
+
+ task :run => :setup do
+ puts "Running Ruby Benchmark Suite"
+ puts " Writing report to #{report_name}"
+
+ Dir[RBS_DIR + "/**/bm_*.rb"].sort.each do |name|
+ Dir.chdir File.dirname(name) do
+ puts " Running #{File.basename name}"
+ system "#{command name}"
+ end
+ end
+
+ puts "Done"
+ end
+
+ desc "Run all the RBS benchmarks in DIR"
+ task :dir => :setup do
+ dir = ENV['DIR'] || raise("bench:dir needs DIR to be a directory")
+
+ puts "Running all benchmarks in #{dir}"
+ puts " Writing report to #{report_name}"
+
+ Dir[dir + "/**/bm_*.rb"].sort.each do |name|
+ Dir.chdir File.dirname(name) do
+ puts " Running #{File.basename name}"
+ system "#{command name}"
+ end
+ end
+
+ puts "Done"
+ end
+
+ desc "Run only the RBS benchmark specified by FILE"
+ task :file => :setup do
+ name = ENV['FILE'] || raise("bench:file needs FILE to be a filename")
+
+ puts "Running benchmark #{name}"
+ puts " Writing report to #{report_name}"
+
+ Dir.chdir File.dirname(name) do
+ puts " Running #{File.basename name}"
+ system "#{command name}"
+ end
+ end
+end
11 rbs/core-features/bm_app_factorial.rb
View
@@ -0,0 +1,11 @@
+def fact(n)
+ if(n > 1)
+ n * fact(n-1)
+ else
+ 1
+ end
+end
+
+Bench.run [1000, 2000, 5000, 10000] do |n|
+ fact n
+end
11 rbs/core-features/bm_app_fib.rb
View
@@ -0,0 +1,11 @@
+def fib(n)
+ if n < 2
+ n
+ else
+ fib(n-1) + fib(n-2)
+ end
+end
+
+Bench.run [20, 30, 35] do |n|
+ fib n
+end
12 rbs/core-features/bm_app_raise.rb
View
@@ -0,0 +1,12 @@
+def raise_and_rescue(n)
+ n.times do
+ begin
+ raise
+ rescue
+ end
+ end
+end
+
+Bench.run [300_000] do |n|
+ raise_and_rescue n
+end
13 rbs/core-features/bm_app_tak.rb
View
@@ -0,0 +1,13 @@
+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
+
+Bench.run [5, 6, 7] do |n|
+ tak 18, n, 0
+end
12 rbs/core-features/bm_app_tarai.rb
View
@@ -0,0 +1,12 @@
+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
+
+Bench.run [3, 4, 5] do |n|
+ tarai 12, n, 0
+end
3  rbs/core-features/bm_loop_times.rb
View
@@ -0,0 +1,3 @@
+Bench.run [10_000_000, 20_000_000, 30_000_000] do |n|
+ n.times { |i| 1 + 1 }
+end
10 rbs/core-features/bm_loop_whileloop.rb
View
@@ -0,0 +1,10 @@
+def while_loop(n)
+ i = 0
+ while i < n
+ i += 1
+ end
+end
+
+Bench.run [100_000, 1_000_000, 10_000_000] do |n|
+ while_loop n
+end
13 rbs/core-features/bm_so_ackermann.rb
View
@@ -0,0 +1,13 @@
+def ack(m, n)
+ if m == 0
+ n + 1
+ elsif n == 0
+ ack(m - 1, 1)
+ else
+ ack(m - 1, ack(m, n - 1))
+ end
+end
+
+Bench.run [5, 7, 9] do |n|
+ ack 3, n
+end
25 rbs/core-features/bm_so_nested_loop.rb
View
@@ -0,0 +1,25 @@
+# $Id: nestedloop-ruby.code,v 1.4 2004/11/13 07:42:22 bfulgham Exp $
+# http://www.bagley.org/~doug/shootout/
+# from Avi Bryant
+
+def nested_loop(n)
+ 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
+ return x
+end
+
+Bench.run [5, 10, 15] do |n|
+ nested_loop n
+end
61 rbs/core-features/bm_so_object.rb
View
@@ -0,0 +1,61 @@
+# -*- 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
+
+def run_toggle(n)
+ 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
+end
+
+Bench.run [500_000, 1_000_000, 1_500_000] do |n|
+ puts run_toggle(n)
+end
9 rbs/core-features/bm_so_random.rb
View
@@ -0,0 +1,9 @@
+# from http://www.bagley.org/~doug/shootout/bench/random/random.ruby
+
+def gen_random(max, last, im, ia, ic)
+ (max * (last = (last * ia + ic) % im)) / im
+end
+
+Bench.run [100_000, 500_000, 1_000_000] do |n|
+ n.times{ gen_random(100.0, 42.0, 139968.0, 3877.0, 29573.0) }
+end
7 rbs/core-features/bm_vm1_block.rb
View
@@ -0,0 +1,7 @@
+def m
+ yield
+end
+
+Bench.run [100_000, 1_000_000, 10_000_000] do |n|
+ n.times { m {} }
+end
10 rbs/core-features/bm_vm1_const.rb
View
@@ -0,0 +1,10 @@
+Const = 1
+i = 0
+
+Bench.run [30_000_000] do |n|
+ while i < n # while loop 1
+ i+= 1
+ j = Const
+ k = Const
+ end
+end
12 rbs/core-features/bm_vm1_ensure.rb
View
@@ -0,0 +1,12 @@
+def begin_ensure
+ begin
+ begin
+ ensure
+ end
+ ensure
+ end
+end
+
+Bench.run [100_000, 1_000_000, 10_000_000] do |n|
+ n.times { begin_ensure }
+end
14 rbs/core-features/bm_vm1_length.rb
View
@@ -0,0 +1,14 @@
+inputs = [10, 100, 1000, 10_000]
+strings = inputs.map { |n| "a" * n }
+arrays = inputs.map { |n| Array.new n, 42 }
+idx = -1
+
+Bench.run inputs do |n|
+ string = strings[idx+=1]
+ array = arrays[idx]
+
+ 30_000_000.times do
+ string.length
+ array.length
+ end
+end
11 rbs/core-features/bm_vm1_rescue.rb
View
@@ -0,0 +1,11 @@
+def rescue_loop(n)
+ n.times do
+ begin
+ rescue
+ end
+ end
+end
+
+Bench.run [1_000_000] do |n|
+ rescue_loop(1_000_000)
+end
7 rbs/core-features/bm_vm1_simplereturn.rb
View
@@ -0,0 +1,7 @@
+def method1
+ return 1
+end
+
+Bench.run [10_000_000, 20_000_000, 30_000_000] do |n|
+ n.times { method1 }
+end
7 rbs/core-features/bm_vm1_swap.rb
View
@@ -0,0 +1,7 @@
+Bench.run [10_000_000, 20_000_000, 40_000_000] do |n|
+ n.times do
+ a = 1
+ b = 2
+ a, b = b, a
+ end
+end
20 rbs/core-features/bm_vm2_poly_method.rb
View
@@ -0,0 +1,20 @@
+class C1
+ def m
+ 1
+ end
+end
+
+class C2
+ def m
+ 2
+ end
+end
+
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ o1 = C1.new
+ o2 = C2.new
+ n.times do |i|
+ o = (i % 2 == 0) ? o1 : o2
+ o.m; o.m; o.m; o.m; o.m; o.m; o.m; o.m
+ end
+end
10 rbs/core-features/bm_vm2_proc.rb
View
@@ -0,0 +1,10 @@
+def m(&b)
+ b
+end
+
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ pr = m { a = 1 }
+ n.times do |i|
+ pr.call
+ end
+end
9 rbs/core-features/bm_vm2_send.rb
View
@@ -0,0 +1,9 @@
+class C
+ def m
+ end
+end
+
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ o = C.new
+ n.times { o.__send__ :m }
+end
16 rbs/core-features/bm_vm2_super.rb
View
@@ -0,0 +1,16 @@
+class C
+ def m
+ 1
+ end
+end
+
+class CC < C
+ def m
+ super()
+ end
+end
+
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ obj = CC.new
+ n.times { obj.m }
+end
6 rbs/core-features/bm_vm2_unif1.rb
View
@@ -0,0 +1,6 @@
+def m(a, b)
+end
+
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ n.times { m(100, 200) }
+end
16 rbs/core-features/bm_vm2_zsuper.rb
View
@@ -0,0 +1,16 @@
+class C
+ def m(a)
+ 1
+ end
+end
+
+class CC < C
+ def m(a)
+ super
+ end
+end
+
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ obj = CC.new
+ n.times { obj.m(10) }
+end
3  rbs/core-library/bm_app_strconcat.rb
View
@@ -0,0 +1,3 @@
+Bench.run [1_500_000] do |n|
+ n.times { "#{1+1} #{1+1} #{1+1}" }
+end
28 rbs/core-library/bm_mbari_bogus1.rb
View
@@ -0,0 +1,28 @@
+# test context switching between two threads with very different stack sizes
+# from Brent Roman MBARI patches bogus1.rb file
+# http://sites.google.com/site/brentsrubypatches/
+
+def outer name
+ if ($depth+=1) < 2000
+ outer name
+ else
+ 250.times {
+ Thread.new do
+ inner :inner, name, Thread.current
+ end.join
+ }
+ end
+end
+
+def inner innerName, outerName, parent
+ Thread.new do
+ parent.join
+ k = Proc.new {|n0, n1| q = n0.to_s << n1.to_s }
+ k[innerName, outerName]
+ end
+end
+
+$depth = 0
+Bench.run [1] do |n|
+ outer :outer
+end
31 rbs/core-library/bm_mbari_bogus2.rb
View
@@ -0,0 +1,31 @@
+# test context switching between two threads with very different stack sizes
+# from the bogus2.rb file
+# of the Brent Roman MBARI patches
+# http://sites.google.com/site/brentsrubypatches/
+
+def outer name
+ ball = Thread.current
+ inner = Thread.new Thread.current do | outer |
+ loop {
+ Thread.stop until (Thread.critical=true; ball == inner)
+ (ball=outer).run
+ }
+ end
+ 2000.times {|i|
+ Thread.stop until (Thread.critical=true; ball == Thread.current)
+ (ball=inner).run
+ }
+end
+
+def recurse someArg
+ if ($depth+=1) < 2000
+ recurse 3.14159
+ else
+ outer :outer
+ end
+end
+
+$depth = 0
+Bench.run [1] do |n|
+ recurse "top"
+end
37 rbs/core-library/bm_pathname.rb
View
@@ -0,0 +1,37 @@
+# Lists directories and files using
+# recursion and the Pathname class.
+# Submitted by Lukas Domagala
+
+require 'pathname'
+
+def recursive_dir(path)
+ current_path = Pathname.new(path)
+ current_files = []
+ current_dirs = []
+ current_path.children.each do |file|
+ if file.directory?
+ current_dirs << file
+ else
+ current_files << file
+ end
+ end
+
+ current_dirs.each do |file|
+ recursive_dir(file)
+ @dirs << file.realpath
+ end
+
+ current_files.each do |file|
+ @files << file.realpath
+ end
+end
+
+Bench.run [100] do |n|
+ n.times do
+ @dirs = []
+ @files = []
+ recursive_dir("../")
+ puts "Directories", "---", @dirs
+ puts "Files", "---", @files
+ end
+end
20 rbs/core-library/bm_so_array.rb
View
@@ -0,0 +1,20 @@
+# $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
+
+Bench.run [9000] do |n|
+ x = Array.new(n)
+ y = Array.new(n, 0)
+
+ n.times do |bi|
+ x[bi] = bi + 1
+ end
+
+ (0 .. 999).each do |e|
+ (n-1).step(0,-1) do |bi|
+ y[bi] += x.at(bi)
+ end
+ end
+
+ puts "#{y.first} #{y.last}"
+end
25 rbs/core-library/bm_so_count_words.rb
View
@@ -0,0 +1,25 @@
+# $Id: wc-ruby.code,v 1.4 2004/11/13 07:43:32 bfulgham Exp $
+# http://www.bagley.org/~doug/shootout/
+# with help from Paul Brannan
+
+def concatenate(n)
+ n.times do
+ input = open(File.dirname(__FILE__) + '/wc.input', 'rb')
+
+ nl = nw = nc = 0
+ while true
+ data = (input.read(4096) or break) << (input.gets || "")
+ nc += data.length
+ nl += data.count("\n")
+ ((data.strip! || data).tr!("\n", " ") || data).squeeze!
+ nw += data.count(" ") + 1
+ end
+
+ input.close
+ puts "#{nl} #{nw} #{nc}"
+ end
+end
+
+Bench.run [100] do |n|
+ n.times { concatenate(500) }
+end
53 rbs/core-library/bm_so_exception.rb
View
@@ -0,0 +1,53 @@
+# $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
+
+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
+
+Bench.run [500_000] do |n|
+ n.times {|i| some_function(i+1) }
+end
40 rbs/core-library/bm_so_lists.rb
View
@@ -0,0 +1,40 @@
+#from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby
+
+def test_lists(size)
+ # 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
+
+Bench.run [1000] do |n|
+ n.times { test_lists(10_000) }
+end
40 rbs/core-library/bm_so_lists_small.rb
View
@@ -0,0 +1,40 @@
+#from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby
+
+def test_lists(size)
+ # 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
+
+Bench.run [1000] do |n|
+ n.times { test_lists(2000) }
+end
45 rbs/core-library/bm_so_matrix.rb
View
@@ -0,0 +1,45 @@
+# $Id: matrix-ruby.code,v 1.4 2004/11/13 07:42:14 bfulgham Exp $
+# http://www.bagley.org/~doug/shootout/
+
+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
+
+size = 30
+
+Bench.run [60] do |n|
+ 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]}"
+end
3  rbs/core-library/bm_vm2_array.rb
View
@@ -0,0 +1,3 @@
+Bench.run [1_000_000, 2_000_000, 4_000_000, 8_000_000] do |n|
+ n.times { a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }
+end
8 rbs/core-library/bm_vm2_regexp.rb
View
@@ -0,0 +1,8 @@
+inputs = [10, 100, 1000, 10_000]
+strings = inputs.map { |n| "a" * n + "hoge" + "b" * n }
+idx = -1
+
+Bench.run inputs do |n|
+ string = strings[idx+=1]
+ 1_000_000.times { /hoge/ =~ string }
+end
3  rbs/core-library/bm_vm3_thread_create_join.rb
View
@@ -0,0 +1,3 @@
+Bench.run [1000, 10_000, 100_000] do |n|
+ n.times { Thread.new{}.join }
+end
259 rbs/core-library/wc.input
View
@@ -0,0 +1,259 @@
+#!/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)
25 rbs/core-library/wc.input.base
View
@@ -0,0 +1,25 @@
+Subject: Re: Who was Izchak Miller?
+From: "Jane D. Anonymous" <nobody@yale.edu>
+Date: 1996/04/28
+Message-Id: <4lv7bc$oh@news.ycc.yale.edu>
+References: <317C405E.5DFA@panix.com> <4lk6vl$gde@ns.oar.net>
+To: 75176.2330@compuserve.com
+Content-Type: text/plain; charset=us-ascii
+Organization: Yale University
+X-Url: news:4lk6vl$gde@ns.oar.net
+Mime-Version: 1.0
+Newsgroups: rec.games.roguelike.nethack
+X-Mailer: Mozilla 1.1N (Macintosh; I; 68K)
+
+Hello there, Izchak Miller was my father. When I was younger I spent
+many a night, hunched over the keyboard with a cup of tea, playing
+nethack with him and my brother. my dad was a philosopher with a strong
+weakness for fantasy/sci fi. I remember when he started to get involved
+with the Nethack team- my brother's Dungeons and Dragons monster book
+found a regular place beside my dad's desk. it's nice to see him living
+on in the game he loved so much :-).
+ Tamar Miller
+
+The following is a really long word of 5000 characters:
+
+wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww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wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
5 rbs/garbage_collection/bm_gc_100mb.rb
View
@@ -0,0 +1,5 @@
+Bench.run [1] do |n|
+ a = []
+ 1000000.times { a << []} # use up some RAM
+ 3000000.times {[]}
+end
5 rbs/garbage_collection/bm_gc_400mb.rb
View
@@ -0,0 +1,5 @@
+Bench.run [1] do |n|
+ a = []
+ 3000000.times { a << []} # use up some RAM
+ 100000.times {[]}
+end
5 rbs/garbage_collection/bm_gc_50mb.rb
View
@@ -0,0 +1,5 @@
+Bench.run [1] do |n|
+ a = []
+ 500000.times { a << []} # use up some RAM
+ 3000000.times {[]}
+end
17 rbs/garbage_collection/bm_gc_array_800mb.rb
View
@@ -0,0 +1,17 @@
+# Run a block 10 times, where the block creates
+# 40MB(32bit VM) or 80MB(64bit VM) of 100 element Arrays
+# and 10 times that much garbage.
+# Needs 150MB gemstone GEM_TEMPOBJ_CACHE_SIZE"
+
+Bench.run [1] do |n|
+ a = nil
+ 10.times do |i|
+ a = Array.new(100_000)
+ a.length.times do |x|
+ a[x] = Array.new(100)
+ 10.times do |y|
+ Array.new(100)
+ end
+ end
+ end
+end
7 rbs/garbage_collection/bm_gc_cow_100mb.rb
View
@@ -0,0 +1,7 @@
+GC.copy_on_write_friendly=true if GC.respond_to? :copy_on_write_friendly=
+
+Bench.run [1] do |n|
+ a = []
+ 1000000.times { a << []} # use up some RAM
+ 3000000.times {[]}
+end
19 rbs/garbage_collection/bm_gc_string_50mb.rb
View
@@ -0,0 +1,19 @@
+# Run a block 10 times, where the block creates
+# 5MB of 1K strings and 10 times that much garbage
+# Needs at least 250MB gemstone GEM_TEMPOBJ_CACHE_SIZE
+
+Bench.run [1] do |n|
+ a = nil
+ 10.times do |i|
+ a = Array.new(5000)
+ a.length.times do |x|
+ str = ""
+ 20.times{|j| str << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYy"}
+ a[x] = str
+ 10.times do |y|
+ junk = ""
+ 20.times{|k| str << "GaRbAgEGaRbAgEGaRbAgEGaRbAgEGaRbAgEGaRbAgEGaRbAgE_"}
+ end
+ end
+ end
+end
28 rbs/micro-benchmarks/LICENSE
View
@@ -0,0 +1,28 @@
+The following files have been adapted from The Computer Language Benchmarks Game (http://shootout.alioth.debian.org/)
+and are released under the revised BSD license below:
+
+bm_binary_trees.rb
+bm_fannkuch.rb
+bm_fasta.rb
+bm_knucleotide.rb
+bm_mandelbrot.rb
+bm_meteor_contest.rb
+bm_nbody.rb
+bm_nsieve.rb
+bm_nsieve_bits.rb
+bm_partial_sums.rb
+bm_recursive.rb
+bm_regex_dna.rb
+bm_reverse_compliment.rb
+bm_spectral_norm.rb
+bm_sum_file.rb
+bm_thread_ring.rb
+
+Copyright © 2004-2008 Brent Fulgham. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ * Neither the name of "The Computer Language Benchmarks Game" nor the name of "The Computer Language Shootout Benchmarks" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128 rbs/micro-benchmarks/bm_app_pentomino.rb
View
@@ -0,0 +1,128 @@
+# This program is contributed by Shin Nishiyama
+# modified by K.Sasada
+
+NP = 5
+ROW = 8 + NP
+COL = 8
+
+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
+
+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
+
+Bench.run [1] do |n|
+ $pnum=[]
+ $p = []
+ $b = []
+ $no = 0
+ mkpieces
+ mkboard
+ $p[4] = [$p[4][0]]
+ $pnum = (0...$p.length).to_a
+ setpiece([],0)
+end
54 rbs/micro-benchmarks/bm_binary_trees.rb
View
@@ -0,0 +1,54 @@
+# The Computer Language Shootout Benchmarks
+# http://shootout.alioth.debian.org
+#
+# contributed by Jesse Millikan
+
+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
+
+Bench.run [1] do |n|
+ max_depth = 16
+ 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)}"
+end
9 rbs/micro-benchmarks/bm_count_multithreaded.rb
View
@@ -0,0 +1,9 @@
+def count_high
+ 100_000.times {}
+end
+
+Bench.run [1, 2, 4, 8, 16] do |n|
+ threads = []
+ n.times{ threads << Thread.new { count_high }}
+ threads.each{|t| t.join}
+end
9 rbs/micro-benchmarks/bm_count_shared_thread.rb
View
@@ -0,0 +1,9 @@
+def count_high how_many
+ how_many.times {}
+end
+
+Bench.run [1, 2, 4, 8, 16] do |n|
+ threads = []
+ n.times{ threads << Thread.new { count_high(1_000_000 / n) }}
+ threads.each{|t| t.join}
+end
42 rbs/micro-benchmarks/bm_fannkuch.rb
View
@@ -0,0 +1,42 @@
+# 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
+ return maxFlips if r == n
+ perm.insert r,perm.shift
+ break if (count[r] -= 1) > 0
+ r += 1
+ end
+ end
+end
+
+Bench.run [6, 8, 10] do |n|
+ puts "Pfannkuchen(#{n}) = #{fannkuch(n)}"
+end
79 rbs/micro-benchmarks/bm_fasta.rb
View
@@ -0,0 +1,79 @@
+# 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
+
+Bench.run [1_000_000] do |n|
+ 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)
+end
44 rbs/micro-benchmarks/bm_fractal.rb
View
@@ -0,0 +1,44 @@
+BAILOUT = 16
+MAX_ITERATIONS = 1000
+
+class Mandelbrot
+
+ def initialize
+ puts "Rendering"
+ for y in -39...39 do
+ puts
+ for x in -39...39 do
+ i = iterate(x/40.0,y/40.0)
+ if (i == 0)
+ print "*"
+ else
+ print " "
+ end
+ end
+ end
+ end
+
+ def iterate(x,y)
+ cr = y-0.5
+ ci = x
+ zi = 0.0
+ zr = 0.0
+ i = 0
+
+ while(1)
+ i += 1
+ temp = zr * zi
+ zr2 = zr * zr
+ zi2 = zi * zi
+ zr = zr2 - zi2 + cr
+ zi = temp + temp + ci
+ return i if (zi2 + zr2 > BAILOUT)
+ return 0 if (i > MAX_ITERATIONS)
+ end
+ end
+
+end
+
+Bench.run [5] do |n|
+ Mandelbrot.new
+end
41 rbs/micro-benchmarks/bm_knucleotide.rb
View
@@ -0,0 +1,41 @@
+# The Computer Language Shootout
+# http://shootout.alioth.debian.org
+#
+# contributed by jose fco. gonzalez
+# modified by Sokolov Yura
+# Adapted for the Ruby Benchmark Suite.
+
+def frecuency(seq, length)
+ n, table = seq.length - length + 1, Hash.new(0)
+ f, i = nil, nil
+ (0 ... length).each do |f|
+ (f ... n).step(length) do |i|
+ table[seq[i,length]] += 1
+ end
+ end
+ [n,table]
+end
+
+def sort_by_freq(seq, length)
+ n,table = frecuency(seq, length)
+ a, b, v = nil, nil, nil
+ table.sort{|a,b| b[1] <=> a[1]}.each do |v|
+ puts "%s %.3f" % [v[0].upcase,((v[1]*100).to_f/n)]
+ end
+ puts
+end
+
+def find_seq(seq, s)
+ n,table = frecuency(seq, s.length)
+ puts "#{table[s].to_s}\t#{s.upcase}"
+end
+
+Bench.run [1] do |n|
+ seq = String.new
+ fname = File.dirname(__FILE__) + "/fasta.input"
+ File.open(fname, "r").each_line do |line|
+ seq << line.chomp
+ end
+ [1,2].each {|i| sort_by_freq(seq, i) }
+ %w(ggt ggta ggtatt ggtattttaatt ggtattttaatttatagt).each{|s| find_seq(seq, s) }
+end
15 rbs/micro-benchmarks/bm_lucas_lehmer.rb
View
@@ -0,0 +1,15 @@
+# Lucas–Lehmer primality test for Mersenne numbers (2**p - 1)
+
+def is_prime?(p)
+ s = 4
+ m = 2**p - 1
+ (p-2).times do
+ s = (s**2 - 2) % m
+ end
+ s == 0 ? true : false
+end
+
+# 4 Mersenne's exponents
+Bench.run [9689, 9941, 11213, 19937] do |n|
+ puts "2**#{n} - 1 is prime" if is_prime?(n)
+end
61 rbs/micro-benchmarks/bm_mandelbrot.rb
View
@@ -0,0 +1,61 @@
+# The Computer Language Benchmarks Game
+# http://shootout.alioth.debian.org/
+#
+# contributed by Karl von Laudermann
+# modified by Jeremy Echols
+# modified by Detlef Reichl
+
+size = 1000
+puts "P4\n#{size} #{size}"
+
+ITER = 50 # Mandelbrot iterations
+LIMIT_SQUARED = 4.0 # Presquared limit
+
+Bench.run [1] do |n|
+ 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.
+ # That's not true, but left it here
+ 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
+
+ zrzr = zr*zr
+ zizi = zi*zi
+ ITER.times do
+ tr = zrzr - zizi + cr
+ ti = 2.0*zr*zi + ci
+ zr = tr
+ zi = ti
+ # preserve recalculation
+ zrzr = zr*zr
+ zizi = zi*zi
+ if zrzr+zizi > 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
+end
42 rbs/micro-benchmarks/bm_mergesort.rb
View
@@ -0,0 +1,42 @@
+# Submitted by Emil Ivanov
+
+class Array
+ def merge_sort
+ len = self.length
+ return self if len <= 1
+ middle = len / 2
+ left = self.slice(0, middle).merge_sort
+ right = self.slice(middle, len - middle).merge_sort
+ merge(left, right)
+ end
+
+ protected
+
+ def merge(left, right)
+ result = []
+
+ while (left.length > 0 && right.length > 0)
+ if (left.first < right.first)
+ result.push(left.shift)
+ else
+ result.push(right.shift)
+ end
+ end
+
+ if left.length > 0
+ result += left
+ end
+
+ if right.length > 0
+ result += right
+ end
+
+ result
+ end
+end
+
+Bench.run [1] do |n|
+ fname = File.dirname(__FILE__) + "/random.input"
+ array = File.read(fname).split(/\n/).map!{|m| m.to_i }
+ puts "Mergesort verified." if array.merge_sort == array.sort
+end
54 rbs/micro-benchmarks/bm_mergesort_hongli.rb
View
@@ -0,0 +1,54 @@
+# from http://izumi.plan99.net/blog/index.php/2008/01/17/ruby-vs-php-performance/
+
+def merge(a, b)
+ c = []
+ while !a.empty? && !b.empty?
+ if a[0] > b[0]
+ c << b[0]
+ b.shift
+ else
+ c << a[0]
+ a.shift
+ end
+ end
+ while !a.empty?
+ c << a[0]
+ a.shift
+ end
+ while !b.empty?
+ c << b[0]
+ b.shift
+ end
+ return c
+end
+
+def mergesort(a)
+ n = a.size
+ if n <= 1
+ return a
+ end
+
+ l1 = []
+ l2 = []
+ i = 0
+ while i < n / 2
+ l1 << a[i]
+ i += 1
+ end
+ while i < n
+ l2 << a[i]
+ i += 1
+ end
+
+ l1 = mergesort(l1)
+ l2 = mergesort(l2)
+ return merge(l1, l2)
+end
+
+numbers = [47448054, 1106251565, 1208921855, 170086026, 840395770, 444281018, 1297307905, 1613614128, 357068250, 1829657695, 654555439, 1261773796, 1821640729, 449683981, 1062536538, 96076061, 1387478498, 1835855315, 364455615, 4830124, 864633601, 289493189, 471351435, 435996916, 1366312031, 888420407, 1923379522, 735726044, 1094401518, 245520239, 109946712, 1107893495, 592868510, 700148765, 273016388, 343881444, 420725947, 1259049694, 1692920986, 71271532, 1154617350, 593508009, 1106700528, 430204045, 1045928775, 1330476642, 49983990, 1451164767, 1175404600, 644832496, 365016297, 1048732794, 503615317, 217186301, 1176160338, 1183622513, 81711049, 1720671278, 1393072097, 1315236388, 1451774341, 92848458, 271000544, 1667871288, 380233084, 1053079658, 1249341507, 1276652307, 1722015039, 1243698025, 178813868, 1449271074, 1994327579, 270972819, 1043379189, 1592595484, 462468972, 1464773315, 1994172406, 997300623, 46405283, 1614271949, 447907123, 317292284, 378291676, 1253835093, 523476912, 1606023999, 59263848, 1234358080, 140981643, 1828471854, 1197394207, 1317927546, 878287915, 334576359, 982149842, 642878238, 1024064999, 1834342299];
+
+Bench.run [3000] do |n|
+ n.times do
+ mergesort(numbers)
+ end
+end
557 rbs/micro-benchmarks/bm_meteor_contest.rb
View
@@ -0,0 +1,557 @@
+# 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
+
+# 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 illegal 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 boundries 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 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 accross the rows
+ (location / 6).to_i.upto(9) do | row_on |
+ # obtain a set of regions representing the bits of the curent 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 muliple 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
+ # follwing 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 acheived 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]} "