Created
March 20, 2016 21:58
-
-
Save arbox/434d6e09ae38a1c130fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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