Skip to content

Commit e518bdf

Browse files
committed
Add unicode and dna benchmarks
1 parent 8688296 commit e518bdf

File tree

8 files changed

+18856
-0
lines changed

8 files changed

+18856
-0
lines changed

Sources/RegexBenchmark/BenchmarkRegistration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extension BenchmarkRunner {
1616
benchmark.addHTML()
1717
benchmark.addEmail()
1818
benchmark.addCustomCharacterClasses()
19+
benchmark.addDna()
20+
benchmark.addUnicode()
1921
// -- end of registrations --
2022
return benchmark
2123
}

Sources/RegexBenchmark/Inputs/DnaFASTA.swift

Lines changed: 16676 additions & 0 deletions
Large diffs are not rendered by default.

Sources/RegexBenchmark/Inputs/TaggedUnicode.swift

Lines changed: 2008 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import _StringProcessing
2+
3+
extension BenchmarkRunner {
4+
mutating func addDna() {
5+
// regex-redux from the benchmarks game
6+
// https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/regexredux.html#regexredux
7+
let dna = "agg[act]taaa|ttta[agt]cct"
8+
let ends = "aND|caN|Ha[DS]|WaS"
9+
10+
let dnaMatching = CrossBenchmark(
11+
baseName: "DnaMatch",
12+
regex: dna,
13+
input: Inputs.dnaFASTA,
14+
includeFirst: true)
15+
16+
let sequenceEnds = CrossBenchmark(
17+
baseName: "DnaEndsMatch",
18+
regex: ends,
19+
input: Inputs.dnaFASTA,
20+
includeFirst: true)
21+
22+
dnaMatching.register(&self)
23+
sequenceEnds.register(&self)
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import _StringProcessing
2+
3+
extension BenchmarkRunner {
4+
mutating func addUnicode() {
5+
// tagged unicode: unicode characters surrounded by html tags
6+
// use the same html regex, uses backreference + reluctant quantification
7+
let tags = #"<(\w*)\b[^>]*>(.*?)<\/\1>"#
8+
let taggedEmojis = CrossBenchmark(
9+
baseName: "TaggedEmojis",
10+
regex: tags,
11+
input: Inputs.taggedEmojis)
12+
13+
// Now actually matching emojis
14+
let emoji = #"(😃|😀|😳|😲|😦|😊|🙊|😘|😏|😳|😒){2,5}"#
15+
16+
let emojiRegex = CrossBenchmark(
17+
baseName: "EmojiRegex",
18+
regex: emoji,
19+
input: Inputs.taggedEmojis)
20+
21+
taggedEmojis.register(&self)
22+
emojiRegex.register(&self)
23+
}
24+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# The Computer Language Benchmarks Game
2+
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
3+
#
4+
# modified by Ian Osgood
5+
# modified again by Heinrich Acker
6+
# modified by Justin Peel
7+
# 2to3
8+
9+
"""Copyright © 2004-2008 Brent Fulgham, 2005-2022 Isaac Gouy
10+
All rights reserved.
11+
12+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
13+
14+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
15+
16+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
17+
18+
3. Neither the name "The Computer Language Benchmarks Game" nor the name "The Benchmarks Game" nor the name "The Computer Language Shootout Benchmarks" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
21+
22+
import sys, bisect
23+
24+
alu = (
25+
'GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG'
26+
'GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA'
27+
'CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT'
28+
'ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA'
29+
'GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG'
30+
'AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC'
31+
'AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA')
32+
33+
iub = list(zip('acgtBDHKMNRSVWY', [0.27, 0.12, 0.12, 0.27] + [0.02]*11))
34+
35+
homosapiens = [
36+
('a', 0.3029549426680),
37+
('c', 0.1979883004921),
38+
('g', 0.1975473066391),
39+
('t', 0.3015094502008),
40+
]
41+
42+
43+
def genRandom(ia = 3877, ic = 29573, im = 139968):
44+
seed = 42
45+
imf = float(im)
46+
while 1:
47+
seed = (seed * ia + ic) % im
48+
yield seed / imf
49+
50+
Random = genRandom()
51+
52+
def makeCumulative(table):
53+
P = []
54+
C = []
55+
prob = 0.
56+
for char, p in table:
57+
prob += p
58+
P += [prob]
59+
C += [char]
60+
return (P, C)
61+
62+
def repeatFasta(src, n):
63+
width = 60
64+
r = len(src)
65+
s = src + src + src[:n % r]
66+
for j in range(n // width):
67+
i = j*width % r
68+
print(s[i:i+width])
69+
if n % width:
70+
print(s[-(n % width):])
71+
72+
def randomFasta(table, n):
73+
width = 60
74+
r = range(width)
75+
gR = Random.__next__
76+
bb = bisect.bisect
77+
jn = ''.join
78+
probs, chars = makeCumulative(table)
79+
for j in range(n // width):
80+
x = jn([chars[bb(probs, gR())] for i in r])
81+
print(x)
82+
if n % width:
83+
print(jn([chars[bb(probs, gR())] for i in range(n % width)]))
84+
85+
def main():
86+
n = int(sys.argv[1])
87+
88+
print('>ONE Homo sapiens alu')
89+
repeatFasta(alu, n*2)
90+
91+
print('>TWO IUB ambiguity codes')
92+
randomFasta(iub, n*3)
93+
94+
print('>THREE Homo sapiens frequency')
95+
randomFasta(homosapiens, n*5)
96+
97+
main()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generates lines of junk unicode wrapped in html tags
2+
import random
3+
4+
# devanagari
5+
#low = 0x0900
6+
#high = 0x097F
7+
8+
# emojis only
9+
low = 0x1F600
10+
high = 0x1F64F
11+
12+
numLines = 1000
13+
14+
start = "<data> "
15+
end = " </data>"
16+
minLine = 10
17+
maxLine = 100
18+
19+
def get_random(i):
20+
return "".join([chr(random.randint(low, high)) for _ in range(i)])
21+
22+
lines = [start + get_random(random.randint(minLine, maxLine)) + end for _ in range(numLines)]
23+
24+
print("\n".join(lines))

0 commit comments

Comments
 (0)