Benchmark : MacRuby's Regexp vs Cocoa's
require 'benchmark'
Benchmark.bm(5) do |x|
str = "MacRuby is an implementation of Ruby 1.9"
x.report "Ruby" {
str.match(/([^ ]+)/)
#p $1
}
framework 'Foundation'
x.report "Cocoa" {
error = Pointer.new(:object)
regexp =
NSRegularExpression.regularExpressionWithPattern("([^ ]+)",
options:NSRegularExpressionCaseInsensitive,
error:error)
match =
regexp.firstMatchInString(str,
options:NSMatchingReportProgress,
range:NSMakeRange(0, str.length))
#p str.substringWithRange(match.rangeAtIndex(0))
}
end
$ macruby bm_regexp.rb
user system total real
Ruby 0.000000 0.000000 0.000000 ( 0.000072)
Cocoa 0.010000 0.000000 0.010000 ( 0.012324)