From: Robert Klemme Date: 2004-07-14T19:32:20+09:00 Subject: Re: String.split "Lloyd Zusman" schrieb im Newsbeitrag news:m3vfgq6hg6.fsf@asfast.com... > "Robert Klemme" writes: > > > "Cameron McBride" schrieb im Newsbeitrag > > news:dcedf5e204071320562e0ad096@mail.gmail.com... > >> > What possible benefit is there to typing split(" ") vs. split(/\s/)? > >> > One saved character (but two shift key presses!)? > >> > >> they are not the same: > >> > >> irb(main):001:0> s = "this is\tfun \tno?" > >> => ["this", "is", "fun", "no?"] > >> irb(main):003:0> s.split(" ") > >> => "this is\tfun \tno?" > >> irb(main):002:0> s.split(/\s/) > >> => ["this", "is", "fun", "", "no?"] > > > > I'd rather compare split(" ") to split(/\s+/), which is what I use when I > > need this functionality. [ ... ] > > However, the two cases are not equivalent: > > irb(main):001:0> " spaces of doom ".split(/\s+/) > => ["", "spaces", "of", "doom"] > irb(main):002:0> " spaces of doom ".split(" ") > => ["spaces", "of", "doom"] > > You'd have to compare split(" ") with strip.split(/\s+/). I'll do that > later this morning, when I have more time, and I'll then post my > results. You're right, the strip makes def test1(s) s.split ' ' end def test2(s) s.split /\s+/ end def test3(s) s.strip.split /\s+/ end def test4(s) s.sub(/^\s+/, '').split /\s+/ end foo = (1..100).to_a.join " " 1000.times { test1 foo } 1000.times { test2 foo } 1000.times { test3 foo } 1000.times { test4 foo } 12:26:30 [ruby]: ./split-perf.rb % cumulative self self total time seconds seconds calls ms/call ms/call name 56.61 1.36 1.36 4000 0.34 0.34 String#split 10.56 1.62 0.25 4 63.50 597.75 Integer#times 8.89 1.83 0.21 1000 0.21 0.21 String#sub 8.40 2.03 0.20 1000 0.20 0.55 Object#test2 7.15 2.20 0.17 1000 0.17 0.65 Object#test4 5.15 2.33 0.12 1000 0.12 0.39 Object#test1 2.62 2.39 0.06 1000 0.06 0.55 Object#test3 1.29 2.42 0.03 1 31.00 31.00 Profiler__.start_profile 0.00 2.42 0.00 1 0.00 2406.00 #toplevel 0.00 2.42 0.00 100 0.00 0.00 Fixnum#to_s 0.00 2.42 0.00 1000 0.00 0.00 String#strip 0.00 2.42 0.00 4 0.00 0.00 Module#method_added 0.00 2.42 0.00 1 0.00 0.00 Range#each 0.00 2.42 0.00 1 0.00 0.00 Array#join 0.00 2.42 0.00 1 0.00 0.00 Enumerable.to_a def test1(s) s.split ' ' end def test2(s) s.split /\s+/ end def test3(s) s.strip.split /\s+/ end def test4(s) s.sub(/^\s+/, '').split /\s+/ end foo = " " + (1..100).to_a.join( " " ) 1000.times { test1 foo } 1000.times { test2 foo } 1000.times { test3 foo } 1000.times { test4 foo } 12:27:36 [ruby]: ./split-perf.rb % cumulative self self total time seconds seconds calls ms/call ms/call name 51.03 1.26 1.26 4000 0.32 0.32 String#split 12.84 1.58 0.32 1000 0.32 0.65 Object#test3 10.13 1.83 0.25 4 62.50 613.50 Integer#times 8.30 2.03 0.20 1000 0.20 0.39 Object#test1 7.09 2.21 0.17 1000 0.17 0.64 Object#test4 6.97 2.38 0.17 1000 0.17 0.52 Object#test2 1.82 2.42 0.05 1000 0.05 0.05 String#sub 1.26 2.46 0.03 1 31.00 31.00 Profiler__.start_profile 1.22 2.49 0.03 1000 0.03 0.03 String#strip 0.61 2.50 0.01 1 15.00 15.00 Enumerable.to_a 0.00 2.50 0.00 1 0.00 0.00 String#+ 0.00 2.50 0.00 100 0.00 0.00 Fixnum#to_s 0.00 2.50 0.00 1 0.00 2469.00 #toplevel 0.00 2.50 0.00 1 0.00 0.00 Range#each 0.00 2.50 0.00 1 0.00 0.00 Array#join 0.00 2.50 0.00 4 0.00 0.00 Module#method_added Performance ranking depends on whether there are leading spaces or not. robert