From cf7b86ef2f9338b718b02a5b632c7b3a126366d7 Mon Sep 17 00:00:00 2001 From: Chuck Remes Date: Wed, 9 Mar 2011 16:54:13 -0600 Subject: [PATCH] redo Array#select/#reject to use a shared array, no dup --- benchmark/core/array/bench_reject.rb | 23 ++- benchmark/core/array/bench_select.rb | 13 +- benchmark/core/array/shared_array.rb | 226 +++++++++++++++++++++++++++ 3 files changed, 240 insertions(+), 22 deletions(-) create mode 100644 benchmark/core/array/shared_array.rb diff --git a/benchmark/core/array/bench_reject.rb b/benchmark/core/array/bench_reject.rb index 4a1614642a..0d8a5834be 100644 --- a/benchmark/core/array/bench_reject.rb +++ b/benchmark/core/array/bench_reject.rb @@ -1,42 +1,37 @@ require 'benchmark' require 'benchmark/ips' +require File.expand_path('../shared_array.rb', __FILE__) Benchmark.ips do |x| - strings = ('a'..'j').to_a - numbers = [-4, -81, 0, 5, 12, -1_000_000, 1, 10, 100, 1000] - x.report "reject string" do |times| + x.report "reject all" do |times| i = 0 while i < times - a = strings.dup - a.reject { |v| v < 'd' } + $large_array.reject { |v| true } i += 1 end end - x.report "reject numbers" do |times| + x.report "reject none" do |times| i = 0 while i < times - a = numbers.dup - a.reject { |v| v < 10 } + $large_array.reject { |v| false } i += 1 end end - x.report "reject! string" do |times| + x.report "reject! all" do |times| i = 0 while i < times - a = strings.dup - a.reject! { |v| v < 'd' } + $large_array.reject! { |v| true } i += 1 end end - x.report "reject! numbers" do |times| + x.report "reject! none" do |times| i = 0 while i < times - a = numbers.dup - a.reject! { |v| v < 10 } + $large_array.reject! { |v| false } i += 1 end end diff --git a/benchmark/core/array/bench_select.rb b/benchmark/core/array/bench_select.rb index ffe844909e..49cea800e6 100644 --- a/benchmark/core/array/bench_select.rb +++ b/benchmark/core/array/bench_select.rb @@ -1,24 +1,21 @@ require 'benchmark' require 'benchmark/ips' +require File.expand_path('../shared_array.rb', __FILE__) Benchmark.ips do |x| - strings = ('a'..'j').to_a - numbers = [-4, -81, 0, 5, 12, -1_000_000, 1, 10, 100, 1000] - x.report "select string" do |times| + x.report "select all" do |times| i = 0 while i < times - a = strings.dup - a.select { |v| v > 'd' } + $large_array.select { |v| true } i += 1 end end - x.report "select numbers" do |times| + x.report "select none" do |times| i = 0 while i < times - a = numbers.dup - a.select { |v| v > 10 } + $large_array.select { |v| false } i += 1 end end diff --git a/benchmark/core/array/shared_array.rb b/benchmark/core/array/shared_array.rb new file mode 100644 index 0000000000..3b7484d46c --- /dev/null +++ b/benchmark/core/array/shared_array.rb @@ -0,0 +1,226 @@ +$small_array = [ + [[[117953105]]], + [:bRV, :OD2eTYQBQY], + :"4rWCHMbY", + [[327776907, :pXJXJ8]], + 784670386, + [[57548812, [:LVhmVvg0, 113018416]]], + :T, + 299999475, + [[290463539, 604740052]], + :"5mIYTzDPBNV", + 108807115, + 160688221, + 400750804, + 277035297, + :lSnApoUIMS, + 328808562, + [:isof7aNStwzp, 551905300], + 591827388, + 164604988, + 695513112, +] + + +$large_array = [ + :DCne, + :apScTg7EbT, + 640650437, + :roPpncD, + :M, + :Shy8tTJvIK, + :NvL, + [884390611, [[811803718]]], + [:LGW, :"2DToMCMxx"], + 719751067, + [:pIVIU3yJyV, [:mER5fdj]], + 766179378, + :"8I", + 368909659, + 301511183, + 878211544, + 308683354, + 462827137, + 981965402, + [[525760611, [440398323]], [262473977, :"4s4zCTyq"]], + 209805240, + 565508721, + 355311411, + [[487599522], [23816217, :Ad]], + [885664140, 738202711], + :pEs, + 507949515, + 424218410, + :VvFSNf, + 108053500, + [[:uxigf, [[910647325, 396262021], 252069733]], :BGIl], + [[:Kr1fz0el], [94594073, :Kc2]], + [[501572927], :RBuZ], + [[980018256, :g], :iY03R], + 355922486, + [[:"3KqNVj"]], + :EeR2XP, + [[[473462304, 54796102], 801503872]], + :tbFhh1EawuQ, + :VlweBX, + :e5q8hFF85, + [209840388, 381195542], + 77947233, + :"8ZMuAcL", + :ZG, + [:z], + 312142962, + [:i5Bwr2MM, [[[:eH]]]], + :d3o18o4RPDo2, + 178571067, + 98937471, + 570139119, + 198078444, + 954066365, + [:"3eAsdQiXfa", [:H7M4GJcInA]], + [:WPJiRVy8GgWj], + [[:kVpRcp, [925195711, 735262942]]], + 139916477, + [[[449500215, [[[538177591, :zwBo2GeB]]]]]], + 150147479, + [:i], + :nl3p, + :h, + [[690972725]], + 63171115, + 618439953, + [:hUsiP, 941514031], + [:O, [:"0gKNDi7", :"3mHer2Y"]], + 492512323, + 251805990, + :GO2Q, + 805563105, + 21740345, + [[:HVeJ, [[[:EHhUCUbb2H, [[225210099], :VdVgm1]]]]], [933914789, 662774857]], + :OJBV1, + 510765990, + :BNnIwrl, + :kjIl7, + :s70z, + [:TdcdVS], + 966030764, + :MQWJmfCSRj, + [438192977, 502008215], + :"5xi3Y", + 636435632, + 207025153, + :PFzxjdA8o7, + :TqPWSWgsdnhY, + 899256241, + 25966241, + :VIIe4xcfCMp, + [[966745699], 363426800], + 896569084, + 544335267, + 23708017, + 579447032, + [[[73248903], [833143227, 638412150]], [:UZE2]], + 863196727, + :"1", + 876403705, + :S33YA0FD674, + :ei, + [[:g7Ajitpwu1, 699266918], :ZtASraQs0C], + 725589209, + [:CcPFjRW, [[800775167, [[349587951], [[:MrXhz]]]], :Dv]], + :"1T", + [[135940766]], + 162299597, + 804590524, + 995712703, + [:R5f0aP3WUabo, [[401304198], :GpWYVRfT]], + :Bd3GbVC4, + 963974873, + [[:"4ko", [298754976]]], + :Q8g1K, + :hh, + [[[[:KUfwKlTc, [:n1]]]], [[[[264794941]], :dPQ]]], + 376769153, + [415067096, :M6tL21ybm3], + 376526623, + 940099721, + [821478278, :ibgqT11bB7q0], + 366773709, + :IDP, + 29446697, + [444247007, 641181723], + :hEqN, + [:kp, :MkKIi], + [[[:eVkMZmU83ccl], :OPH8fMzDCN], :RvZj3tH4Q], + 471757853, + :fmo, + :GnYkzzL, + :Yby, + :fsb2, + [900813922, :u], + :P, + :DTxZwjWfEoNf, + 218143925, + [562927537], + 536303244, + 438309558, + :XFdfJLXwU, + [[[:zbagiCSYpl], 665516321], [798237776, :LkVqk7JH]], + :RhnG, + 529359312, + 185868915, + 260824547, + 744694017, + 725445205, + [[680032845, :n], :OUj1EILL5GL], + :gm7AdIAQicd, + 665330641, + :YA0x, + :EYK1, + :gfnfPy, + 354183973, + 729295195, + 771920833, + [:fxFj], + 37766541, + 804699185, + :YRrFitD, + :W, + [[:V6u6nzWQZ]], + 104311942, + [500139637], + :bpVnAffQb, + [:MR0rSDTvVl, 343968413], + 267489586, + :x5eLKOzpCws, + 990331322, + :M, + :AU6nFC8V, + 273592958, + :hEj5dbjTG, + [623554159], + 345872794, + 621040909, + 926121238, + [:v, :kEe], + 689561990, + 838812795, + 46835249, + 172284993, + [803588660, 717203845], + [:d], + 126893892, + :"8DBqlrWm", + 139052367, + :"1", + 927142311, + :Rhp, + 192500452, + [16557598, 219816350], + [:Iv5x, [846371158]], + [953388069], + 948311974, + [:rtNolMF, 368694274], + [[[[:"4fDM3cdfHp"]]]], + :vaxk68l6, +]