Skip to content

Instantly share code, notes, and snippets.

@kengos
Last active May 6, 2019 01:50
Show Gist options
  • Save kengos/6410634 to your computer and use it in GitHub Desktop.
Save kengos/6410634 to your computer and use it in GitHub Desktop.
Ruby benchmark
require 'benchmark'
n = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
Benchmark.bm do |bm|
bm.report("each_with_index:") {
100000.times do
n.each_with_index do |o,i|
x = [o, i]
end
end
}
bm.report("each :") {
100000.times do
i = 0
n.each do |o|
x = [o, i]
i += 1
end
end
}
end
=begin
# rbenv shell 1.8.7-p374
user system total real
each_with_index: 1.690000 0.000000 1.690000 ( 1.687167)
each : 1.190000 0.000000 1.190000 ( 1.196548)
# rbenv shell 1.9.3-p448
user system total real
each_with_index: 0.450000 0.000000 0.450000 ( 0.447725)
each : 0.430000 0.000000 0.430000 ( 0.433347)
# rbenv shell 2.0.0-p247
user system total real
each_with_index: 0.390000 0.000000 0.390000 ( 0.390246)
each : 0.350000 0.000000 0.350000 ( 0.347369)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment