Watson1978
3/4/2012 - 1:23 AM

bench.rb

framework 'Foundation'
require 'benchmark'


Benchmark.bm(25) do |x|

  str_ascii =<<EOS
Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversation?'
EOS

  nsstring = NSString.stringWithString(str_ascii)

  x.report "String[]" do
    10000.times do
      str_ascii[0, 5]
    end
  end

  x.report "NSString[]" do
    10000.times do
      nsstring[0, 5]
    end
  end

  x.report "substringWithRange (1)" do
    range = NSMakeRange(0, 5)
    nsstring.substringWithRange(range)
  end

  x.report "substringWithRange (2)" do
    range = NSMakeRange(0, 5)
    10000.times do
      nsstring.substringWithRange(range)
    end
  end

end