Skip to content

Instantly share code, notes, and snippets.

@nepalez
Forked from d4rky-pl/meth_vs_proc.rb
Last active August 29, 2015 14:24
Show Gist options
  • Save nepalez/bdfe11d86f07f6aee133 to your computer and use it in GitHub Desktop.
Save nepalez/bdfe11d86f07f6aee133 to your computer and use it in GitHub Desktop.
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
jruby-9.0.0.0-pre1
Calculating -------------------------------------
method 76.559k i/100ms
proc 85.904k i/100ms
proc new 81.708k i/100ms
-------------------------------------------------
method 3.673M (±15.0%) i/s - 17.762M
proc 2.894M (±13.6%) i/s - 14.088M
proc new 2.764M (± 8.6%) i/s - 13.727M
Comparison:
method: 3673100.4 i/s
proc: 2893793.1 i/s - 1.27x slower
proc new: 2763847.8 i/s - 1.33x 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
rbx-head
Calculating -------------------------------------
method 17.781k i/100ms
proc 64.211k i/100ms
proc new 91.358k i/100ms
-------------------------------------------------
method 1.336M (± 3.6%) i/s - 6.650M
proc 1.055M (± 4.3%) i/s - 5.265M
proc new 1.199M (± 2.7%) i/s - 6.030M
Comparison:
method: 1336218.3 i/s
proc new: 1198769.5 i/s - 1.11x slower
proc: 1055264.2 i/s - 1.27x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment