Skip to content

Instantly share code, notes, and snippets.

@mhat
Created February 24, 2010 00:00
Show Gist options
  • Save mhat/312885 to your computer and use it in GitHub Desktop.
Save mhat/312885 to your computer and use it in GitHub Desktop.
require 'benchmark'
puts `uname -a`
puts `ruby --version`
puts '-' * 78
@small = (0 .. 10).to_a
@exps = []
GC.enable_stats
def with_gc
alloc = ObjectSpace.allocated_objects
GC.clear_stats
yield
alloc = ObjectSpace.allocated_objects - alloc
puts " times gc'ed: #{GC.collections}"
puts " allocated..: #{alloc}"
[ GC.collections, alloc ]
end
puts "--"
puts "map with block vs &: -- iterations by powers of 10"
puts '#' * 78
Benchmark.bm do |x|
(0...6).each do |exp|
puts "## 10^#{exp}"
stat1 = with_gc { x.report("simply-S") { (10**exp).times { @small.map {|s| s.to_s } } } }
stat2 = with_gc { x.report("cutesy-S") { (10**exp).times { @small.map &:to_s } } }
puts " Δ garbage (simple-cutesy): #{stat1[0] - stat2[0]}"
puts " Δ alloced (simple-cutesy): #{stat1[1] - stat2[1]}"
puts '-----'
end
end
puts
puts "map with block vs &: -- data size by powers of 10"
puts '*' * 78
@exps = (0..6).map{ |exp| ( 0 ... (10**exp) ).to_a }
Benchmark.bm do |x|
@exps.each_with_index do |ary, exp|
puts "## 10^#{exp} #{ary.class} #{ary.size}"
stat1 = with_gc { x.report('simply') { ary.map {|s| s.to_s } } }
stat2 = with_gc { x.report('cutesy') { ary.map &:to_s } }
puts " Δ garbage (simple-cutesy): #{stat1[0] - stat2[0]}"
puts " Δ alloced (simple-cutesy): #{stat1[1] - stat2[1]}"
puts '-----'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment