David A. Black wrote:

> I always figured it's easiest just to learn the regex stuff and get it
> over with. 

I did--8 years ago.

> 
> My benchmarks suggest that it's a little faster (about 10%).
> 
> 

Ok, I see where you're coming from.  The following test shows that 
split() operates 45% faster without a regex:

require 'benchmark'
include Benchmark

L_COUNT = 1_000_000

bm(25) do |test|
  test.report("split:") do
    L_COUNT.times do |i|
      str = "hello     world goodbye"
      arr = str.split()
    end
  end

  test.report("regex:") do
    L_COUNT.times do |i|
      str = "hello     world goodbye"
      arr = str.split(/\s+/)
    end
  end
end


                              user     system      total        real
split:                     2.470000   0.010000   2.480000 (  2.494421)
regex:                     4.550000   0.030000   4.580000 (  4.576609)



And this test shows that split() is 13% faster with a regex:

require 'benchmark'
include Benchmark

L_COUNT = 1_000_000

bm(25) do |test|
  test.report("split:") do
    L_COUNT.times do |i|
      str = "hello|world|goodbye"
      arr = str.split("|")
    end
  end

  test.report("regex:") do
    L_COUNT.times do |i|
      str = "hello|world|goodbye"
      arr = str.split(/\|/)
    end
  end
end


That indicates to me that unless you use the default behavior of 
split(), which splits on spaces, then split() has to spend time 
converting its argument to a regex.



-- 
Posted via http://www.ruby-forum.com/.