2008-08-07から1日間の記事一覧

固定の文字列が先頭にあるかを調べる場合、String#indexより正規表現の方が早いらしい。

require 'benchmark' n = 1_000_000 str = ('a'..'z').to_a.to_s str += ('A'..'Z').to_a.to_s str = str * 100 x = "abc" Benchmark.bm do |x| x.report("regexp") { n.times { str =~ /^abc/ ? "t" : "f" } } x.report("index") { n.times { str.index("ab…