Concurrent Clean : 部分文字列比較の速度チェック
(注)この記事は、嘘をつきまくりです。上の訂正記事をお読みください。
次の2つの速度を比較してみた。
f = lst % (0,2) == "aaa"
f = lst startswith "aaa" (startswith) infix 9 :: !String !String -> Bool (startswith) ss tt = lss >= ltt && check 0 where lss = size ss ltt = size tt check i | i >= ltt = True = ss.[i] == tt.[i] && check (inc i)
1000000000回の試行で結果は変わらず。(ByRuntimeが上で、ByCleanが下)
$ time ./ByRuntime.exe True real 0m9.860s user 0m0.010s sys 0m0.060s $ time ./ByClean.exe True real 0m9.851s user 0m0.020s sys 0m0.040s $ time ./ByRuntime.exe True real 0m9.775s user 0m0.010s sys 0m0.040s $ time ./ByClean.exe True real 0m9.757s user 0m0.010s sys 0m0.030s
まあ、スライスで文字列コピーしないなら当然か。
ためしに、比較文字列長を長くしてみたが、結果は同じ。
$ time ./ByRuntime.exe True real 0m9.739s user 0m0.040s sys 0m0.020s $ time ./ByClean.exe True real 0m9.758s user 0m0.020s sys 0m0.040s
ところで、組み込みのタイムプロファイラって、本当に正しいのかな? startswith関数の中身の消費時間が0秒なんですけど・・・