From: Guillaume Marcais Date: 2003-05-31T13:26:53+09:00 Subject: Re: Array.extend versus instance.extend OK, my fault. The following code should pass your test and *is* faster most of the time. Pop_until, because of the reverse, is slower when the search ends early. Guillaume. [gus@gusmac test]$ cat misc2.rb module ArrayMisc2 def shift_until(klass) p = -1 detect { |x| p += 1; klass === x } or p += 1 slice!(0, p) end def pop_until(klass) p = -1 reverse.detect { |x| p += 1; klass === x } or p += 1 slice!(-p, p) end end [gus@gusmac test]$ cat speed_test.rb require 'misc' require 'misc2' def speed_test(src, max, ext1, ext2) 1.upto(10) do |i| b1 = src.dup b2 = src.dup b1.extend(ext1) b2.extend(ext2) t1 = Time.now b1.shift_until(max / i) t1 = Time.now - t1 t2 = Time.now b2.shift_until(max / i) t2 = Time.now - t2 puts("shift %d: 1: %s 2: %s" % [max / i, t1.to_s, t2.to_s]) b1 = src.dup b2 = src.dup b1.extend(ext1) b2.extend(ext2) t1 = Time.now b1.pop_until(max / i) t1 = Time.now - t1 t2 = Time.now b2.pop_until(max / i) t2 = Time.now - t2 puts("pop %d: 1: %s 2: %s" % [max / i, t1.to_s, t2.to_s]) end end max = 10000 a = Array.new 1.upto(max) do |i| a << i end speed_test(a, max, ArrayMisc, ArrayMisc2) [gus@gusmac test]$ ruby speed_test.rb shift 10000: 1: 0.220421 2: 0.080113 pop 10000: 1: 4.5e-05 2: 0.000446 shift 5000: 1: 0.245843 2: 0.038756 pop 5000: 1: 0.05908 2: 0.076305 shift 3333: 1: 0.182882 2: 0.027826 pop 3333: 1: 0.087186 2: 0.086682 shift 2500: 1: 0.144765 2: 0.020657 pop 2500: 1: 0.199623 2: 0.060441 shift 2000: 1: 0.129832 2: 0.017254 pop 2000: 1: 0.177329 2: 0.064388 shift 1666: 1: 0.082475 2: 0.048194 pop 1666: 1: 0.172718 2: 0.076809 shift 1428: 1: 0.153042 2: 0.016205 pop 1428: 1: 0.179198 2: 0.06833 shift 1250: 1: 0.075859 2: 0.009778 pop 1250: 1: 0.192854 2: 0.070832 shift 1111: 1: 0.061179 2: 0.008301 pop 1111: 1: 0.240657 2: 0.073334 shift 1000: 1: 0.397667 2: 0.007408 pop 1000: 1: 0.20591 2: 0.077756 Le vendredi, 30 mai 2003, � 07:27 US/Eastern, Simon Strandgaard a �crit : > On Fri, 30 May 2003 19:48:55 +0900, dblac wrote: >> On Fri, 30 May 2003, Simon Strandgaard wrote: >>> >>> I ran the test-suite on your code and it failed. >> >> Try again :-) It shouldn't fail. >> >> Here's what I ran, cut-and-pasted from your message and my message, >> plus the TestUnit wrapper: > > Yes it works with these 2 test cases.. but there is a few more > test-cases > it has to work with, see: > > http://ruby-talk.org/72408 > >> class TestMe < Test::Unit::TestCase >> def test_shift_until_non_existing_class > [snip] >> def test_pop_until_non_existing_class > [snip] >> end > > > Sorry for causing confusion :-) > > -- > Simon Strandgaard >