Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'benchmark'
M = 1000
Benchmark.bm 30 do |r|
[1, 10, 100, 1000].each do |n|
a = (0...(n*100)).to_a
b0 = ((0*n)...(1*n)).to_a
b1 = ((1*n)...(2*n)).to_a
b2 = ((2*n)...(3*n)).to_a
b3 = ((3*n)...(4*n)).to_a
b4 = ((4*n)...(5*n)).to_a
b5 = ((5*n)...(6*n)).to_a
b6 = ((6*n)...(7*n)).to_a
b7 = ((7*n)...(8*n)).to_a
b8 = ((8*n)...(9*n)).to_a
b9 = ((9*n)...(10*n)).to_a
r.report("#{n}, Array#-, 1,") { M.times { a - b0 } }
r.report("#{n}, Array#-, 2,") { M.times { a - b0 - b1 } }
r.report("#{n}, Array#-, 3,") { M.times { a - b0 - b1 - b2 } }
r.report("#{n}, Array#-, 4,") { M.times { a - b0 - b1 - b2 - b3 } }
r.report("#{n}, Array#-, 5,") { M.times { a - b0 - b1 - b2 - b3 - b4 } }
r.report("#{n}, Array#-, 6,") { M.times { a - b0 - b1 - b2 - b3 - b4 - b5 } }
r.report("#{n}, Array#-, 7,") { M.times { a - b0 - b1 - b2 - b3 - b4 - b5 - b6 } }
r.report("#{n}, Array#-, 8,") { M.times { a - b0 - b1 - b2 - b3 - b4 - b5 - b6 - b7 } }
r.report("#{n}, Array#-, 9,") { M.times { a - b0 - b1 - b2 - b3 - b4 - b5 - b6 - b7 - b8 } }
r.report("#{n}, Array#-, 10,") { M.times { a - b0 - b1 - b2 - b3 - b4 - b5 - b6 - b7 - b8 - b9 } }
r.report("#{n}, Array#difference, 1,") { M.times { a.difference(b0) } }
r.report("#{n}, Array#difference, 2,") { M.times { a.difference(b0, b1) } }
r.report("#{n}, Array#difference, 3,") { M.times { a.difference(b0, b1, b2) } }
r.report("#{n}, Array#difference, 4,") { M.times { a.difference(b0, b1, b2, b3) } }
r.report("#{n}, Array#difference, 5,") { M.times { a.difference(b0, b1, b2, b3, b4) } }
r.report("#{n}, Array#difference, 6,") { M.times { a.difference(b0, b1, b2, b3, b4, b5) } }
r.report("#{n}, Array#difference, 7,") { M.times { a.difference(b0, b1, b2, b3, b4, b5, b6) } }
r.report("#{n}, Array#difference, 8,") { M.times { a.difference(b0, b1, b2, b3, b4, b5, b6, b7) } }
r.report("#{n}, Array#difference, 9,") { M.times { a.difference(b0, b1, b2, b3, b4, b5, b6, b7, b8) } }
r.report("#{n}, Array#difference, 10,") { M.times { a.difference(b0, b1, b2, b3, b4, b5, b6, b7, b8, b9) } }
end
end
n method argc time
1 Array#- 1 0.002759
1 Array#- 2 0.006072
1 Array#- 3 0.006631
1 Array#- 4 0.010692
1 Array#- 5 0.011508
1 Array#- 6 0.014615
1 Array#- 7 0.014205
1 Array#- 8 0.019318
1 Array#- 9 0.020224
1 Array#- 10 0.021061
1 Array#difference 1 0.002425
1 Array#difference 2 0.005670
1 Array#difference 3 0.006545
1 Array#difference 4 0.005206
1 Array#difference 5 0.006910
1 Array#difference 6 0.007500
1 Array#difference 7 0.008321
1 Array#difference 8 0.009362
1 Array#difference 9 0.011211
1 Array#difference 10 0.012713
10 Array#- 1 0.075318
10 Array#- 2 0.149664
10 Array#- 3 0.221649
10 Array#- 4 0.291970
10 Array#- 5 0.379423
10 Array#- 6 0.431458
10 Array#- 7 0.502145
10 Array#- 8 0.670647
10 Array#- 9 0.694663
10 Array#- 10 0.783733
10 Array#difference 1 0.074446
10 Array#difference 2 0.130630
10 Array#difference 3 0.197300
10 Array#difference 4 0.297945
10 Array#difference 5 0.342663
10 Array#difference 6 0.456715
10 Array#difference 7 0.447880
10 Array#difference 8 0.581359
10 Array#difference 9 0.573306
10 Array#difference 10 0.655751
100 Array#- 1 0.385401
100 Array#- 2 0.646665
100 Array#- 3 0.952984
100 Array#- 4 1.297722
100 Array#- 5 1.613933
100 Array#- 6 1.845603
100 Array#- 7 2.154625
100 Array#- 8 2.502676
100 Array#- 9 2.889823
100 Array#- 10 3.225137
100 Array#difference 1 0.337869
100 Array#difference 2 0.568485
100 Array#difference 3 0.817729
100 Array#difference 4 0.996100
100 Array#difference 5 1.245841
100 Array#difference 6 1.503868
100 Array#difference 7 1.828281
100 Array#difference 8 1.963303
100 Array#difference 9 2.272640
100 Array#difference 10 2.416088
1000 Array#- 1 3.998049
1000 Array#- 2 7.518972
1000 Array#- 3 10.679167
1000 Array#- 4 14.606882
1000 Array#- 5 18.254795
1000 Array#- 6 22.083142
1000 Array#- 7 26.095931
1000 Array#- 8 28.234937
1000 Array#- 9 32.511315
1000 Array#- 10 35.640246
1000 Array#difference 1 4.061106
1000 Array#difference 2 6.824668
1000 Array#difference 3 8.970046
1000 Array#difference 4 11.629179
1000 Array#difference 5 14.431434
1000 Array#difference 6 17.471096
1000 Array#difference 7 20.723247
1000 Array#difference 8 23.597272
1000 Array#difference 9 26.951781
1000 Array#difference 10 29.637970
user system total real
1, Array#-, 1, 0.002759 0.000997 0.003756 ( 0.004050)
1, Array#-, 2, 0.006072 0.001048 0.007120 ( 0.007137)
1, Array#-, 3, 0.006631 0.000926 0.007557 ( 0.008281)
1, Array#-, 4, 0.010692 0.003327 0.014019 ( 0.014187)
1, Array#-, 5, 0.011508 0.001047 0.012555 ( 0.012707)
1, Array#-, 6, 0.014615 0.000303 0.014918 ( 0.015612)
1, Array#-, 7, 0.014205 0.000110 0.014315 ( 0.014614)
1, Array#-, 8, 0.019318 0.000212 0.019530 ( 0.022787)
1, Array#-, 9, 0.020224 0.000409 0.020633 ( 0.023129)
1, Array#-, 10, 0.021061 0.000239 0.021300 ( 0.022615)
1, Array#difference, 1, 0.002425 0.000029 0.002454 ( 0.002800)
1, Array#difference, 2, 0.005670 0.000204 0.005874 ( 0.009497)
1, Array#difference, 3, 0.006545 0.000033 0.006578 ( 0.006919)
1, Array#difference, 4, 0.005206 0.000047 0.005253 ( 0.005845)
1, Array#difference, 5, 0.006910 0.000133 0.007043 ( 0.009468)
1, Array#difference, 6, 0.007500 0.000083 0.007583 ( 0.012418)
1, Array#difference, 7, 0.008321 0.000079 0.008400 ( 0.008889)
1, Array#difference, 8, 0.009362 0.000073 0.009435 ( 0.009740)
1, Array#difference, 9, 0.011211 0.000167 0.011378 ( 0.011822)
1, Array#difference, 10, 0.012713 0.000181 0.012894 ( 0.014518)
10, Array#-, 1, 0.075318 0.005713 0.081031 ( 0.088888)
10, Array#-, 2, 0.149664 0.006211 0.155875 ( 0.183658)
10, Array#-, 3, 0.221649 0.012065 0.233714 ( 0.241885)
10, Array#-, 4, 0.291970 0.025348 0.317318 ( 0.339085)
10, Array#-, 5, 0.379423 0.007600 0.387023 ( 0.402857)
10, Array#-, 6, 0.431458 0.004851 0.436309 ( 0.441364)
10, Array#-, 7, 0.502145 0.019989 0.522134 ( 0.530016)
10, Array#-, 8, 0.670647 0.011038 0.681685 ( 0.702528)
10, Array#-, 9, 0.694663 0.026919 0.721582 ( 0.787540)
10, Array#-, 10, 0.783733 0.019710 0.803443 ( 1.043021)
10, Array#difference, 1, 0.074446 0.000956 0.075402 ( 0.080318)
10, Array#difference, 2, 0.130630 0.002296 0.132926 ( 0.139268)
10, Array#difference, 3, 0.197300 0.001542 0.198842 ( 0.209748)
10, Array#difference, 4, 0.297945 0.006421 0.304366 ( 0.433877)
10, Array#difference, 5, 0.342663 0.005306 0.347969 ( 0.396090)
10, Array#difference, 6, 0.456715 0.006796 0.463511 ( 0.509525)
10, Array#difference, 7, 0.447880 0.003476 0.451356 ( 0.467933)
10, Array#difference, 8, 0.581359 0.007229 0.588588 ( 0.646385)
10, Array#difference, 9, 0.573306 0.004946 0.578252 ( 0.600846)
10, Array#difference, 10, 0.655751 0.008458 0.664209 ( 0.719927)
100, Array#-, 1, 0.385401 0.008932 0.394333 ( 0.655632)
100, Array#-, 2, 0.646665 0.020362 0.667027 ( 0.736204)
100, Array#-, 3, 0.952984 0.025919 0.978903 ( 1.068324)
100, Array#-, 4, 1.297722 0.044956 1.342678 ( 1.412830)
100, Array#-, 5, 1.613933 0.038228 1.652161 ( 1.717415)
100, Array#-, 6, 1.845603 0.051568 1.897171 ( 1.960612)
100, Array#-, 7, 2.154625 0.042647 2.197272 ( 2.235376)
100, Array#-, 8, 2.502676 0.068105 2.570781 ( 2.640583)
100, Array#-, 9, 2.889823 0.072653 2.962476 ( 3.179080)
100, Array#-, 10, 3.225137 0.089681 3.314818 ( 3.470182)
100, Array#difference, 1, 0.337869 0.007883 0.345752 ( 0.366157)
100, Array#difference, 2, 0.568485 0.009476 0.577961 ( 0.599460)
100, Array#difference, 3, 0.817729 0.011180 0.828909 ( 0.924980)
100, Array#difference, 4, 0.996100 0.007590 1.003690 ( 1.019640)
100, Array#difference, 5, 1.245841 0.014926 1.260767 ( 1.309490)
100, Array#difference, 6, 1.503868 0.016651 1.520519 ( 1.614833)
100, Array#difference, 7, 1.828281 0.029860 1.858141 ( 2.053745)
100, Array#difference, 8, 1.963303 0.014949 1.978252 ( 2.042669)
100, Array#difference, 9, 2.272640 0.026724 2.299364 ( 2.433902)
100, Array#difference, 10, 2.416088 0.031233 2.447321 ( 2.707060)
1000, Array#-, 1, 3.998049 0.514314 4.512363 ( 4.798258)
1000, Array#-, 2, 7.518972 0.982890 8.501862 ( 9.051585)
1000, Array#-, 3, 10.679167 1.270621 11.949788 ( 12.338518)
1000, Array#-, 4, 14.606882 1.751011 16.357893 ( 17.127021)
1000, Array#-, 5, 18.254795 2.068912 20.323707 ( 21.426640)
1000, Array#-, 6, 22.083142 2.769133 24.852275 ( 27.566783)
1000, Array#-, 7, 26.095931 3.202719 29.298650 ( 32.151704)
1000, Array#-, 8, 28.234937 2.976802 31.211739 ( 33.459355)
1000, Array#-, 9, 32.511315 3.493633 36.004948 ( 40.067801)
1000, Array#-, 10, 35.640246 3.872886 39.513132 ( 43.384938)
1000, Array#difference, 1, 4.061106 0.411439 4.472545 ( 4.641684)
1000, Array#difference, 2, 6.824668 0.510581 7.335249 ( 8.340443)
1000, Array#difference, 3, 8.970046 0.464136 9.434182 ( 9.951463)
1000, Array#difference, 4, 11.629179 0.524005 12.153184 ( 13.286905)
1000, Array#difference, 5, 14.431434 0.536189 14.967623 ( 16.337830)
1000, Array#difference, 6, 17.471096 0.481025 17.952121 ( 19.220038)
1000, Array#difference, 7, 20.723247 0.563418 21.286665 ( 23.571910)
1000, Array#difference, 8, 23.597272 0.562419 24.159691 ( 26.248048)
1000, Array#difference, 9, 26.951781 0.639108 27.590889 ( 30.601349)
1000, Array#difference, 10, 29.637970 0.655714 30.293684 ( 33.246102)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.