Navigation Menu

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)
@jc00ke
Copy link

jc00ke commented Oct 9, 2013

∴ ./each_bm.rb 
                                     user     system      total        real
each                             6.890000   0.010000   6.900000 (  6.910201)
for ... in                       6.390000   0.000000   6.390000 (  6.410597)

A lot closer for me.

@ralavay
Copy link

ralavay commented Feb 8, 2014

How could it be? I still think each better than for. Here my bench with ruby 1.9.3

                                     user     system      total        real
each                             7.850000   0.000000   7.850000 (  7.858244)
for ... in                       9.270000   0.000000   9.270000 (  9.287104)

and here for 2.0.0

                                     user     system      total        real
each                             7.630000   0.000000   7.630000 (  7.658610)
for ... in                       9.010000   0.000000   9.010000 (  9.024356)

@os6sense
Copy link

2.1

                                 user     system      total        real
each                             3.390000   0.000000   3.390000 (  3.383373)
for ... in                       3.720000   0.000000   3.720000 (  3.718571)

Yup, each is faster.

@ianheggie
Copy link

With ruby 2.1.2, each is faster, but the good old i = 0; while i < limit; i+= 1; end beats the lot hands down (ugly is still sometimes faster)!

Note, I assign the value from the array to make sure that it isn't optimised away.

#!/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|
      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

I ran it three times to make sure, as the first time I had weird results, possibly from the cpu frequency ramping up. 2nd time

                                     user     system      total        real
each                             4.400000   0.010000   4.410000 (  4.400051)
for ... in                       4.760000   0.010000   4.770000 (  4.766783)
for ... in 0..limit              5.340000   0.000000   5.340000 (  5.341970)
while i <ARRAY.size              3.040000   0.010000   3.050000 (  3.040795)
while i <limit                   2.720000   0.000000   2.720000 (  2.715845)

3rd time

                                     user     system      total        real
each                             4.540000   0.000000   4.540000 (  4.532571)
for ... in                       4.730000   0.010000   4.740000 (  4.738334)
for ... in 0..limit              5.380000   0.000000   5.380000 (  5.370768)
while i <ARRAY.size              3.050000   0.000000   3.050000 (  3.058212)
while i <limit                   2.710000   0.010000   2.720000 (  2.704505)

@adimircolen
Copy link

                                     user     system      total        real
each                             5.970000   0.030000   6.000000 (  6.044845)
for ... in                       6.170000   0.020000   6.190000 (  6.244391)

ruby 2.1.2p95 (2014-05-08 revision 45877)

@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