def timer m
  t0 = Time.now
  yield
  puts "#{m}#{Time.now - t0}"
  GC.start
end

a = []
timer("array / list:\n1m inserts: ") {1000000.times {|i| a << i}}
timer("another 5m inserts: ") {5000000.times {|i| a << i}}

a = []
timer("functional (i**2) 4m-array generation: ") {Array.new(4000000) {|i| i**2}}
timer("6m assigns: ") {6000000.times {|i| a[i] = i}}
timer("6m gets: ") {6000000.times {|i| a[i]}}

a = a[0...2000000]
timer("for 2m-array:\nrandom shuffle: ") {a.shuffle!}
timer("sort by results of (i % 1000): ") {a.sort_by {|i| i % 1000}}
timer("find all elements satisfy (i % 1000 == 0): ")  {a.find_all {|i| i % 1000 == 0}}
timer("simple sort: ") {a.sort!}

h = {}
timer("hash / dict:\n2m assigns: ") {2000000.times {|i| h[i] = i**2}}
timer("2m gets: ") {2000000.times {|i| h[i]}}
timer("1m gets +1m failgets: ") {(-1000000...1000000).each {|i| h[i]}}

s, ss = "yoba", ""
timer("eval expression 100k times: ") {100000.times {eval 's + ss'}}

timer("1m string products: ") {1000000.times {s*100}}
timer("1m concatenations: ") {1000000.times {ss << s}}
timer("1m slices (range): ") {1000000.times {|i| ss[i...i+4]}}
timer("1m slices (length): ") {1000000.times {|i| ss[i, 4]}}

class A
  attr_accessor :x
end
a = A.new
timer("10m times set instance variable: ") {10_000000.times {|i| a.x = i}}
timer("10m times get instance variable: ") {10_000000.times {|i| a.x}}

def yoba(n=0)
  begin;  yoba(n+1)
  rescue SystemStackError; n  end
end
puts "recursive calls:\nstack len: #{$stack_len = yoba - 1}"
timer("recurse until totally 5m recursions done: ") {n = 0; n = yoba n while n < 5000000}

def yoba_optimized(n=0)
  n % $stack_len == 0 ? (n+1) : (yoba_optimized n+1)
end
timer("recurse until totally 5m recursions done (optimized): ") {n = 0; n = yoba_optimized n while n < 5000000}

Add a code snippet to your website: www.paste.org