Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created August 30, 2016 15:51
Show Gist options
  • Select an option

  • Save cheeyeo/05d86458f71b56eebb29c3ad11b436a3 to your computer and use it in GitHub Desktop.

Select an option

Save cheeyeo/05d86458f71b56eebb29c3ad11b436a3 to your computer and use it in GitHub Desktop.
Benchmark to_proc vs block in Enumerable#map
#/usr/bin/env ruby
require "benchmark/ips"
arr = [1.0, 2.0, 3.0]
h = { foo: 1, bar: 2, baz: 3 }
class Double
def to_proc
proc{ |n| n * 2 }
end
end
Benchmark.ips do |x|
x.report("arr.map{ |x| x*2 }"){
arr.map{ |x| x*2 }
}
x.report("arr.map(&Double.new)"){
arr.map(&Double.new)
}
x.report("[:foo, :bar].map"){
[:foo, :bar].map{ |x| h[x] }
}
x.report("[:foo, :bar].map(&h)"){
[:foo, :bar].map(&h)
}
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment