Created
July 8, 2015 11:25
-
-
Save d4rky-pl/acfc5cd02c5505b884b5 to your computer and use it in GitHub Desktop.
meth_vs_proc.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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