Skip to content

Instantly share code, notes, and snippets.

@tomclose
Created April 8, 2016 10:14
Show Gist options
  • Save tomclose/e3336916b470f65e66997018f3296b36 to your computer and use it in GitHub Desktop.
Save tomclose/e3336916b470f65e66997018f3296b36 to your computer and use it in GitHub Desktop.
Benchmarking functions vs procs
require 'benchmark'
def f_func(x); 2*x; end
def f_proc_func; proc {|x| 2*x}; end
f_proc_var = proc {|x| 2*x }
n = 1000000
Benchmark.bm(7) do |x|
x.report("using function: ") { n.times { |i| f_func(i) } }
x.report("using function via method: ") { n.times(&method(:f_func)) }
x.report("using proc func: ") { n.times(&f_proc_func) }
x.report("using proc var: ") { n.times(&f_proc_var) }
end
# user system total real
# using function: 0.140000 0.010000 0.150000 ( 0.141750)
# using function via method: 0.170000 0.000000 0.170000 ( 0.181539)
# using proc func: 0.080000 0.000000 0.080000 ( 0.078792)
# using proc var: 0.070000 0.000000 0.070000 ( 0.083151)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment