Skip to content

Instantly share code, notes, and snippets.

@manveru
Created December 25, 2009 04:26
Show Gist options
  • Save manveru/263505 to your computer and use it in GitHub Desktop.
Save manveru/263505 to your computer and use it in GitHub Desktop.
require 'benchmark'
def with_zip(n, a1, a2)
n.times do
a1.zip(a2).each{|a,b| [a,b]}
end
end
def with_index(n, a1, a2)
n.times do
a1.each_with_index do |a,i|
[a, a2[i]]
end
end
end
def with_times(n, a1, a2)
n.times do
a1.size.times do |i|
[a1[i], a2[i]]
end
end
end
iterations = 100_0
arr1 = (1..100_00).to_a
arr2 = (1..100_00).to_a
Benchmark.bmbm(20) do |x|
x.report("zip:") { with_zip iterations, arr1, arr2 }
x.report("each_with_index:") { with_index iterations, arr1, arr2 }
x.report("times:") { with_times iterations, arr1, arr2 }
end
jruby-1.4.0: jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_17) [amd64-java]
Rehearsal -------------------------------------------------------
zip: 7.684000 0.000000 7.684000 ( 7.599000)
each_with_index: 7.895000 0.000000 7.895000 ( 7.896000)
times: 4.023000 0.000000 4.023000 ( 4.023000)
--------------------------------------------- total: 19.602000sec
user system total real
zip: 6.123000 0.000000 6.123000 ( 6.122000)
each_with_index: 7.405000 0.000000 7.405000 ( 7.405000)
times: 3.989000 0.000000 3.989000 ( 3.989000)
mput-head: ruby 1.9.2dev (2009-12-24 trunk 26168) [x86_64-linux]
Rehearsal -------------------------------------------------------
zip: 6.260000 0.040000 6.300000 ( 6.766393)
each_with_index: 3.530000 0.040000 3.570000 ( 3.825759)
times: 3.390000 0.020000 3.410000 ( 3.629621)
--------------------------------------------- total: 13.280000sec
user system total real
zip: 5.590000 0.050000 5.640000 ( 6.169143)
each_with_index: 3.460000 0.030000 3.490000 ( 3.678107)
times: 3.340000 0.030000 3.370000 ( 3.643457)
<w> rbx rbx-1.0.0-head is not installed. </w>
<i> To install do: 'rvm install rbx-1.0.0-head' </i>
<w> 'ruby not found for rbx-1.0.0-head' either does not exist or is not executable? :( </w>
rbx-1.0.0-rc1: rubinius 1.0.0-rc1 (1.8.7 release 2009-11-25) [x86_64-unknown-linux-gnu]
Rehearsal -------------------------------------------------------
zip: 51.676848 0.000000 51.676848 ( 51.676859)
each_with_index: 12.555496 0.000000 12.555496 ( 12.555545)
times: 5.334229 0.000000 5.334229 ( 5.334294)
--------------------------------------------- total: 69.566573sec
user system total real
zip: 50.961011 0.000000 50.961011 ( 50.961043)
each_with_index: 12.299367 0.000000 12.299367 ( 12.299399)
times: 5.513145 0.000000 5.513145 ( 5.513186)
ree-1.8.7-2009.10: ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2009.10
Rehearsal -------------------------------------------------------
zip: 14.980000 0.090000 15.070000 ( 15.426589)
each_with_index: 18.180000 0.100000 18.280000 ( 18.639172)
times: 11.440000 0.060000 11.500000 ( 11.716477)
--------------------------------------------- total: 44.850000sec
user system total real
zip: 14.960000 0.070000 15.030000 ( 15.296901)
each_with_index: 18.220000 0.090000 18.310000 ( 18.635532)
times: 11.440000 0.080000 11.520000 ( 11.742455)
ruby-1.8.6-p383: ruby 1.8.6 (2009-08-04 patchlevel 383) [x86_64-linux]
Rehearsal -------------------------------------------------------
zip: 9.880000 0.110000 9.990000 ( 10.202520)
each_with_index: 11.430000 0.050000 11.480000 ( 11.725193)
times: 8.170000 0.050000 8.220000 ( 8.385765)
--------------------------------------------- total: 29.690000sec
user system total real
zip: 9.740000 0.050000 9.790000 ( 10.018042)
each_with_index: 10.920000 0.070000 10.990000 ( 11.202624)
times: 7.940000 0.050000 7.990000 ( 8.139356)
ruby-1.8.7-p174: ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
Rehearsal -------------------------------------------------------
zip: 8.960000 0.090000 9.050000 ( 9.250585)
each_with_index: 10.020000 0.040000 10.060000 ( 10.368467)
times: 7.130000 0.050000 7.180000 ( 7.321719)
--------------------------------------------- total: 26.290000sec
user system total real
zip: 8.950000 0.090000 9.040000 ( 9.273162)
each_with_index: 9.570000 0.050000 9.620000 ( 9.815839)
times: 6.890000 0.030000 6.920000 ( 7.054050)
ruby-1.9.1-p243: ruby 1.9.1p243 (2009-07-16 revision 24175) [x86_64-linux]
Rehearsal -------------------------------------------------------
zip: 8.690000 0.050000 8.740000 ( 8.925555)
each_with_index: 3.900000 0.040000 3.940000 ( 4.011458)
times: 3.850000 0.020000 3.870000 ( 3.942537)
--------------------------------------------- total: 16.550000sec
user system total real
zip: 7.970000 0.040000 8.010000 ( 8.171143)
each_with_index: 3.970000 0.020000 3.990000 ( 4.046158)
times: 3.880000 0.030000 3.910000 ( 3.968713)
ruby-1.9.1-p376: ruby 1.9.1p376 (2009-12-07 revision 26041) [x86_64-linux]
Rehearsal -------------------------------------------------------
zip: 10.180000 0.070000 10.250000 ( 10.555122)
each_with_index: 4.270000 0.040000 4.310000 ( 4.422937)
times: 4.220000 0.020000 4.240000 ( 4.330070)
--------------------------------------------- total: 18.800000sec
user system total real
zip: 9.120000 0.050000 9.170000 ( 9.377272)
each_with_index: 4.290000 0.020000 4.310000 ( 4.394372)
times: 4.260000 0.030000 4.290000 ( 4.379646)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment