Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created November 9, 2009 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jodosha/229951 to your computer and use it in GitHub Desktop.
Save jodosha/229951 to your computer and use it in GitHub Desktop.
Ruby benchmark: Array#each vs for x in array
#!/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)
@elifoster
Copy link

Ruby 2.2.0

                                     user     system      total        real
each                             6.010000   0.020000   6.030000 (  6.041084)
for ... in                       6.450000   0.020000   6.470000 (  6.498104)
for in limit                     6.140000   0.020000   6.160000 (  6.182828)

@guizmaii
Copy link

guizmaii commented Dec 10, 2018

With JRuby 9.2.5.0:

                                     user     system      total        real
each                             3.580000   0.000000   3.580000 (  3.552550)
for ... in                       3.410000   0.010000   3.420000 (  3.368357)
for ... in 0..limit              4.390000   0.020000   4.410000 (  4.285572)
while i <ARRAY.size              2.990000   0.010000   3.000000 (  2.913514)
while i <limit                   1.910000   0.010000   1.920000 (  1.888160)

I tuned a bit the @ianheggie code because the JVM needs to be hot to give the best perfs possible:

#!/usr/bin/env ruby -w
require "benchmark"

TIMES = 100_000
ARRAY = (1..1_000).to_a

10.times do

  Benchmark.bm(30) do |b|

    b.report "each" do
      TIMES.times do |i|
        t = 0
        ARRAY.each do |element|
          t = element
        end
      end
    end

    b.report "for ... in" do
      TIMES.times do |i|
        t = 0
        for x in ARRAY
          t = x
        end
      end
    end

    b.report "for ... in 0..limit" do
      TIMES.times do |i|
        limit = ARRAY.size - 1
        t = 0
        for i in 0..limit
          t = ARRAY[i]
        end
      end
    end

    b.report "while i <ARRAY.size" do
      TIMES.times do |i|
        t = 0
        i = 0
        while i < ARRAY.size
          t = ARRAY[i]
          i += 1
        end
      end
    end

    b.report "while i <limit" do
      TIMES.times do |i|
        limit = ARRAY.size
        t = 0
        i = 0
        while i < limit
          t = ARRAY[i]
          i += 1
        end
      end
    end
    
  end
  
end

@guizmaii
Copy link

guizmaii commented Dec 10, 2018

Message deleted because my comment wasn't relevent. See https://gist.github.com/jodosha/229951#gistcomment-2782034

@headius
Copy link

headius commented Dec 10, 2018

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.

@el-rotny
Copy link

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)

@SamSaffron
Copy link

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)

@mtunjic
Copy link

mtunjic commented Jul 25, 2021

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)

@ozovalihasan
Copy link

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)

@os6sense
Copy link

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)

@realhaidinh
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment