on master help sign in
Add ByteString performance tests.

--HG-- extra : convert_revision : 9cc959cd5b7683691fbb0145b674c58206d28c35

commit258b069ca8ac1b2eac7829f1f8cae1fb06417c3e authorBryan O'Sullivan <bos@serpentine.com> dateFri 9 Oct 2009, 06:35 (17 years ago) parent40452fc
2 files changed+42−5
tests/Makefile+2−1 tests/SearchBench.hs+40−4
tests/Makefile+2−1
@@ -35,7 +35,7 @@ QuickCheckUtils.o: $(lib)
3535qc: Properties.o QuickCheckUtils.o SlowFunctions.o
3636 $(ghc) $(ghc-flags) -threaded -o $@ $^ $(lib)
3737
38−sb: SearchBench.o
38+sb: SearchBench.o SlowFunctions.o
3939 $(ghc) $(ghc-flags) -threaded -o $@ $^ $(lib)
4040
4141qc-hpc: Properties.hs QuickCheckUtils.hs $(lib-srcs:%=../%)
@@ -52,6 +52,7 @@ qc-hpc-html/hpc_index.html: qc-hpc
5252 --exclude=Data.Text.Fusion.CaseMapping \
5353 --exclude=QuickCheckUtils --srcdir=.. --srcdir=. --destdir=$(dir $@)
5454
55+SlowFunctions.o: ghc-opt-flags = -O2
5556SearchBench.o: ghc-opt-flags = -O
5657%.o: %.hs
5758 $(ghc) $(ghc-flags) $(ghc-opt-flags) -c -o $@ $<
tests/SearchBench.hs+40−4
@@ -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\