Skip to content

Instantly share code, notes, and snippets.

@dbenhur
Created June 5, 2013 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbenhur/5715478 to your computer and use it in GitHub Desktop.
Save dbenhur/5715478 to your computer and use it in GitHub Desktop.
Simple Micro-Benchmark to show Symbol#to_proc performance hit
#!/usr/bin/env ruby
require 'benchmark'
N = 1_000_000
A = (0..10).to_a
def map_plain
A.map {|e| e.to_s}
end
def map_to_proc
A.map(&:to_s)
end
puts RUBY_DESCRIPTION
Benchmark.bm(8) do |x|
x.report('plain') { N.times { map_plain } }
x.report('to_proc') { N.times { map_to_proc } }
end
__END__
# Example results:
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-linux]
user system total real
plain 2.580000 0.000000 2.580000 ( 2.591577)
to_proc 2.460000 0.000000 2.460000 ( 2.473722)
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-linux]
user system total real
plain 2.590000 0.000000 2.590000 ( 2.601503)
to_proc 2.470000 0.030000 2.500000 ( 2.503922)
ruby 1.8.7 (2012-02-08 MBARI 8/0x6770 on patchlevel 358) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2012.02
user system total real
plain 6.300000 0.000000 6.300000 ( 6.322921)
to_proc 8.240000 0.000000 8.240000 ( 8.263345)
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]
user system total real
plain 4.770000 0.000000 4.770000 ( 4.791040)
to_proc 6.960000 0.030000 6.990000 ( 7.015733)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment