Skip to content

Instantly share code, notes, and snippets.

@davidpdrsn
Last active July 4, 2018 11:15
Show Gist options
  • Select an option

  • Save davidpdrsn/197c40ec546d38d6520f557efa53f81c to your computer and use it in GitHub Desktop.

Select an option

Save davidpdrsn/197c40ec546d38d6520f557efa53f81c to your computer and use it in GitHub Desktop.
require "benchmark/ips"
def f(x)
x + 1
end
a = (1..1000).to_a
raise unless a.map(&method(:f)) == a.map { |x| f(x) }
Benchmark.ips do |x|
x.report("method") do
a.map(&method(:f))
end
x.report("proc") do
a.map { |x| f(x) }
end
x.compare!
end
# Warming up --------------------------------------
# method 1.384k i/100ms
# proc 1.213k i/100ms
# Calculating -------------------------------------
# method 15.387k (±11.7%) i/s - 76.120k in 5.065370s
# proc 12.162k (± 9.7%) i/s - 60.650k in 5.044157s
# Comparison:
# method: 15387.0 i/s
# proc: 12162.3 i/s - 1.27x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment