require 'benchmark/ips' | |
class CallableNoArg | |
def call | |
end | |
end | |
class CallableWithArg | |
def call a | |
end | |
end | |
class CallableWithThreeArgs | |
def call a, b, c | |
end | |
end | |
class CallableWithSplatArgs | |
def call *args | |
end | |
end | |
Benchmark.ips do |x| | |
callable_no_arg = CallableNoArg.new | |
proc_no_arg = proc {} | |
x.report('callable no arg') { callable_no_arg.call } | |
x.report('proc no arg') { proc_no_arg.call } | |
callable_with_arg = CallableWithArg.new | |
proc_with_arg = proc { |a| } | |
x.report('callable with arg') { callable_with_arg.call 1 } | |
x.report('proc with arg') { proc_with_arg.call 1 } | |
callable_three_args = CallableWithThreeArgs.new | |
proc_with_three_args = proc { |a, b, c| } | |
x.report('callable 3 args') { callable_three_args.call 1, 2, 3 } | |
x.report('proc 3 args') { proc_with_three_args.call 1, 2, 3 } | |
callable_splat = CallableWithSplatArgs.new | |
proc_splat_args = proc { |*args| } | |
x.report('callable splat args (0)') { callable_splat.call } | |
x.report('callable splat args (1)') { callable_splat.call 1 } | |
x.report('callable splat args (3)') { callable_splat.call 1, 2, 3 } | |
x.report('proc splat args (0)') { proc_splat_args.call } | |
x.report('proc splat args (1)') { proc_splat_args.call 1 } | |
x.report('proc splat args (3)') { proc_splat_args.call 1, 2, 3 } | |
x.compare! | |
end |
Comparison: | |
callable no arg: 10095848.2 i/s | |
callable with arg: 9777103.9 i/s - same-ish: difference falls within error | |
callable 3 args: 9460308.0 i/s - same-ish: difference falls within error | |
callable splat args (0): 6773190.5 i/s - 1.49x slower | |
proc no arg: 6747397.4 i/s - 1.50x slower | |
proc with arg: 6663572.5 i/s - 1.52x slower | |
proc 3 args: 6454715.5 i/s - 1.56x slower | |
callable splat args (1): 5099903.4 i/s - 1.98x slower | |
proc splat args (0): 5028088.6 i/s - 2.01x slower | |
callable splat args (3): 4880320.0 i/s - 2.07x slower | |
proc splat args (1): 4091623.1 i/s - 2.47x slower | |
proc splat args (3): 4005997.8 i/s - 2.52x slower |
Comparison: | |
callable splat args (0): 18537216.0 i/s | |
callable with arg: 17866238.7 i/s - same-ish: difference falls within error | |
callable 3 args: 17087534.2 i/s - same-ish: difference falls within error | |
callable splat args (3): 16986270.0 i/s - same-ish: difference falls within error | |
callable no arg: 16853450.9 i/s - same-ish: difference falls within error | |
callable splat args (1): 16826254.7 i/s - same-ish: difference falls within error | |
proc no arg: 14977646.8 i/s - 1.24x slower | |
proc splat args (0): 14117857.3 i/s - 1.31x slower | |
proc splat args (3): 13438650.8 i/s - 1.38x slower | |
proc 3 args: 13290881.0 i/s - 1.39x slower | |
proc with arg: 12848996.0 i/s - 1.44x slower | |
proc splat args (1): 11984049.1 i/s - 1.55x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment