Skip to content

Instantly share code, notes, and snippets.

@d4rky-pl
Created July 8, 2015 11:25
Show Gist options
  • Save d4rky-pl/acfc5cd02c5505b884b5 to your computer and use it in GitHub Desktop.
Save d4rky-pl/acfc5cd02c5505b884b5 to your computer and use it in GitHub Desktop.
meth_vs_proc.rb
Calculating -------------------------------------
method 56.995k i/100ms
proc 49.213k i/100ms
proc new 57.562k i/100ms
-------------------------------------------------
method 1.797M (±10.7%) i/s - 8.891M
proc 1.522M (± 8.3%) i/s - 7.579M
proc new 1.867M (± 7.9%) i/s - 9.267M
Comparison:
proc new: 1867458.0 i/s
method: 1796674.5 i/s - 1.04x slower
proc: 1521538.1 i/s - 1.23x slower
require 'benchmark/ips'
module Fns
def self.upcase(input)
input.upcase
end
end
meth_obj = Fns.method(:upcase)
proc_obj = Fns.method(:upcase).to_proc
proc_new = Proc.new { |foo| foo.upcase }
Benchmark.ips do |x|
x.report('method') { meth_obj.call('foo') }
x.report('proc') { proc_obj.call('foo') }
x.report('proc new') { proc_new.call('foo') }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment