Created
November 9, 2009 13:49
-
-
Save jodosha/229951 to your computer and use it in GitHub Desktop.
Ruby benchmark: Array#each vs for x in array
This file contains hidden or 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
#!/usr/bin/env ruby -w | |
require "benchmark" | |
TIMES = 100_000 | |
ARRAY = (1..1_000).to_a | |
Benchmark.bm(30) do |b| | |
b.report "each" do | |
TIMES.times do |i| | |
ARRAY.each do |element| | |
end | |
end | |
end | |
b.report "for ... in" do | |
TIMES.times do |i| | |
for x in ARRAY | |
end | |
end | |
end | |
end | |
__END__ | |
user system total real | |
each 9.710000 0.010000 9.720000 ( 9.740620) | |
for ... in 7.460000 0.000000 7.460000 ( 7.475158) |
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
user system total real
each 4.910000 0.000000 4.910000 ( 4.916322)
for ... in 6.780000 0.000000 6.780000 ( 6.783303)
for ... in 0..limit 8.390000 0.000000 8.390000 ( 8.396801)
while i <ARRAY.size 7.920000 0.000000 7.920000 ( 7.936485)
while i <limit 6.100000 0.000000 6.100000 ( 6.101939)
Ruby 2.7.0
each 2.158537 0.000000 2.158537 ( 2.159440)
for ... in 2.278251 0.000000 2.278251 ( 2.279027)
for ... in 0..limit 2.698481 0.000000 2.698481 ( 2.699452)
while i <ARRAY.size 1.650970 0.000000 1.650970 ( 1.651618)
while i <limit 1.560133 0.000000 1.560133 ( 1.560785)
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
user system total real
each 2.767563 0.001415 2.768978 ( 2.769054)
for ... in 2.910278 0.000017 2.910295 ( 2.910301)
for ... in 0..limit 3.669667 0.000005 3.669672 ( 3.669689)
while i < ARRAY.size 2.346777 0.000007 2.346784 ( 2.346790)
while i < limit 1.973913 0.000000 1.973913 ( 1.973939)
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
user system total real
each 6.655506 0.011998 6.667504 ( 6.668422)
for ... in 7.203961 0.008000 7.211961 ( 7.212894)
for ... in 0..limit 8.075872 0.012000 8.087872 ( 8.088514)
while i <ARRAY.size 4.219771 0.008000 4.227771 ( 4.227991)
while i <limit 3.359660 0.003999 3.363659 ( 3.363865)
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
user system total real
each 2.052966 0.000207 2.053173 ( 2.053209)
for ... in 2.225467 0.000006 2.225473 ( 2.225489)
for ... in 0..limit 2.478339 0.000000 2.478339 ( 2.478365)
while i <ARRAY.size 1.727570 0.000000 1.727570 ( 1.727601)
while i <limit 1.469423 0.000001 1.469424 ( 1.469439)
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]
user system total real
each 4.203000 0.000000 4.203000 ( 4.222868)
for ... in 4.562000 0.000000 4.562000 ( 4.583148)
for ... in 0..limit 5.047000 0.000000 5.047000 ( 5.050032)
while i <ARRAY.size 2.219000 0.000000 2.219000 ( 2.235048)
while i <limit 1.984000 0.000000 1.984000 ( 2.017428)
ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux]
user system total real
each 3.574591 0.000000 3.574591 ( 3.586114)
for ... in 3.879859 0.000000 3.879859 ( 3.892989)
for ... in 0..limit 4.631031 0.000000 4.631031 ( 4.637611)
while i <ARRAY.size 2.519849 0.000000 2.519849 ( 2.520258)
while i <limit 1.927129 0.000000 1.927129 ( 1.927600)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is more likely a side effect of how the JIT optimizes one block of code and then the other block of code. These two pieces of code compile to the exact same IR and bytecode.