Skip to content

Instantly share code, notes, and snippets.

@arbox
Created March 20, 2016 21:58
Show Gist options
  • Save arbox/434d6e09ae38a1c130fe to your computer and use it in GitHub Desktop.
Save arbox/434d6e09ae38a1c130fe to your computer and use it in GitHub Desktop.
require 'benchmark'
Benchmark.bmbm do |r|
NUMBER = 10_000_000
words = %w[apple banana carrot durian pear orange]
r.report('zip') do
NUMBER.times do
words[0..-2].zip(words[1..-1]).all? do |left, right|
left <= right
end
end
end
r.report('cons') do
NUMBER.times do
words.each_cons(2).all? do |left, right|
left <= right
end
end
end
end
require "benchmark/ips"
Benchmark.ips do |x|
words = %w[apple banana carrot durian pear orange]
x.report('`#zip`') do
words[0..-2].zip(words[1..-1]).all? do |left, right|
left <= right
end
end
x.report('`#each_cons`') do
words.each_cons(2).all? do |left, right|
left <= right
end
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment