@@ -1,14 +1,19 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE BangPatterns, OverloadedStrings #-}
22
33import Criterion.Main
4+import Data.Text.Encoding
45import Data.Text.Search
6+import qualified Data.ByteString.Char8 as B
57import qualified Data.Text as T
8+import qualified SlowFunctions as Slow
69
710main = defaultMain [
811 bench "big/fast" $ (length . search indices)
9− , bench "big/slow" $ (length . search slowIndices)
12+ , bench "big/slow" $ (length . search Slow.indices)
13+ , bench "big/bytestring" $ (length . searchBS)
1014 , bench "dna/fast" $ (length . searchDNA indices)
11− , bench "dna/slow" $ (length . searchDNA slowIndices)
15+ , bench "dna/slow" $ (length . searchDNA Slow.indices)
16+ , bench "dna/bytestring" $ (length . searchDNABS)
1217 ]
1318
1419search f n = f needle' haystack
@@ -26,7 +31,38 @@ searchDNA f n = f needle haystack
2631 needle = T.take 4 . T.drop (n `mod` (T.length dna - 4)) $ dna
2732 haystack = T.replicate 100 dna
2833
29−dna = "\
34+replicateBS n = B.concat . replicate n
35+
36+searchBS n = indicesBS needle' haystack
37+ where
38+ needle' = B.drop (n `mod` 20) needle
39+ needle = replicateBS 10 "abcdefghijklmnopqrstuvwxyz"
40+ haystack = replicateBS 100 $
41+ B.concat [ replicateBS 1000 "def"
42+ , needle
43+ , replicateBS 1000 "123"
44+ ]
45+
46+searchDNABS n = indicesBS needle haystack
47+ where
48+ needle = B.take 4 . B.drop (n `mod` (B.length bdna - 4)) $ bdna
49+ haystack = replicateBS 100 bdna
50+
51+indicesBS :: B.ByteString -> B.ByteString -> [Int]
52+indicesBS pat
53+ | B.null pat = error "empty"
54+ | otherwise = go 0
55+ where
56+ l = B.length pat
57+ go !i src = case B.breakSubstring pat src of
58+ (h,t) | B.null t -> []
59+ | otherwise -> i' : go (i'+l) (B.drop l t)
60+ where i' = i + B.length h
61+
62+dna = decodeASCII bdna
63+
64+bdna :: B.ByteString
65+bdna = "\
3066\CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGGGTT\
3167\AATCTGGAGGATCTGTTTACTTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGAATTGCCATCG\
3268\AGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGCAGTTTTGCTCCAAGTCGTT\