Skip to content

Instantly share code, notes, and snippets.

@britishtea
Created February 20, 2015 20:56
Show Gist options
  • Save britishtea/e0bbbce9129d8c1a6b93 to your computer and use it in GitHub Desktop.
Save britishtea/e0bbbce9129d8c1a6b93 to your computer and use it in GitHub Desktop.
Is _ as an argument name optimized or just a convention?
def does_care(*a)
"hi"
end
def cares_a_little(*_a)
"hi"
end
def does_not_care(*_)
"hi"
end
def really_doesnt_care(*)
"hi"
end
require "benchmark/ips"
Benchmark.ips do |x|
x.report("*a") { does_care(1,2,3) }
x.report("*_a") { cares_a_little(1,2,3) }
x.report("*_") { does_not_care(1,2,3) }
x.report("*") { really_doesnt_care(1,2,3) }
x.compare!
end
Calculating -------------------------------------
*a 52.377k i/100ms
*_a 52.644k i/100ms
*_ 52.383k i/100ms
* 52.646k i/100ms
-------------------------------------------------
*a 1.785M (± 7.2%) i/s - 8.904M
*_a 1.777M (±11.7%) i/s - 8.686M
*_ 1.810M (± 8.0%) i/s - 8.957M
* 1.811M (± 6.9%) i/s - 9.002M
Comparison:
*: 1811206.5 i/s
*_: 1809693.2 i/s - 1.00x slower
*a: 1784987.0 i/s - 1.01x slower
*_a: 1777387.3 i/s - 1.02x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment