on master help sign in
Add ByteString performance tests.
258b069Bryan O'Sullivan
tests/Makefile+2−1
@@ -35,7 +35,7 @@ QuickCheckUtils.o: $(lib)
35qc: Properties.o QuickCheckUtils.o SlowFunctions.o
35qc: Properties.o QuickCheckUtils.o SlowFunctions.o
36 $(ghc) $(ghc-flags) -threaded -o $@ $^ $(lib)
36 $(ghc) $(ghc-flags) -threaded -o $@ $^ $(lib)
37
37
38−sb: SearchBench.o
38+sb: SearchBench.o SlowFunctions.o
39 $(ghc) $(ghc-flags) -threaded -o $@ $^ $(lib)
39 $(ghc) $(ghc-flags) -threaded -o $@ $^ $(lib)
40
40
41qc-hpc: Properties.hs QuickCheckUtils.hs $(lib-srcs:%=../%)
41qc-hpc: Properties.hs QuickCheckUtils.hs $(lib-srcs:%=../%)
@@ -52,6 +52,7 @@ qc-hpc-html/hpc_index.html: qc-hpc
52 --exclude=Data.Text.Fusion.CaseMapping \
52 --exclude=Data.Text.Fusion.CaseMapping \
53 --exclude=QuickCheckUtils --srcdir=.. --srcdir=. --destdir=$(dir $@)
53 --exclude=QuickCheckUtils --srcdir=.. --srcdir=. --destdir=$(dir $@)
54
54
55+SlowFunctions.o: ghc-opt-flags = -O2
55SearchBench.o: ghc-opt-flags = -O
56SearchBench.o: ghc-opt-flags = -O
56%.o: %.hs
57%.o: %.hs
57 $(ghc) $(ghc-flags) $(ghc-opt-flags) -c -o $@ $<
58 $(ghc) $(ghc-flags) $(ghc-opt-flags) -c -o $@ $<
tests/SearchBench.hs+40−4
@@ -1,14 +1,19 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE BangPatterns, OverloadedStrings #-}
2
2
3import Criterion.Main
3import Criterion.Main
4+import Data.Text.Encoding
4import Data.Text.Search
5import Data.Text.Search
6+import qualified Data.ByteString.Char8 as B
5import qualified Data.Text as T
7import qualified Data.Text as T
8+import qualified SlowFunctions as Slow
6
9
7main = defaultMain [
10main = defaultMain [
8 bench "big/fast" $ (length . search indices)
11 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)
10 , bench "dna/fast" $ (length . searchDNA indices)
14 , 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)
12 ]
17 ]
13
18
14search f n = f needle' haystack
19search f n = f needle' haystack
@@ -26,7 +31,38 @@ searchDNA f n = f needle haystack
26 needle = T.take 4 . T.drop (n `mod` (T.length dna - 4)) $ dna
31 needle = T.take 4 . T.drop (n `mod` (T.length dna - 4)) $ dna
27 haystack = T.replicate 100 dna
32 haystack = T.replicate 100 dna
28
33
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 = "\
30\CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGGGTT\
66\CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGGGTT\
31\AATCTGGAGGATCTGTTTACTTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGAATTGCCATCG\
67\AATCTGGAGGATCTGTTTACTTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGAATTGCCATCG\
32\AGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGCAGTTTTGCTCCAAGTCGTT\
68\AGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGCAGTTTTGCTCCAAGTCGTT\