-
Notifications
You must be signed in to change notification settings - Fork 63
/
doubles_with_c_swig.py
81 lines (51 loc) · 1.56 KB
/
doubles_with_c_swig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
import string
import random
import itertools
import myrustlib # <-- Importing Rust Implemented Library
import sys
sys.path.append('./pyext-myclib')
import myclib # <-- Importing C Implemented Library
def count_doubles(val):
total = 0
for c1, c2 in zip(val, val[1:]):
if c1 == c2:
total += 1
return total
def count_doubles_once(val):
total = 0
chars = iter(val)
c1 = next(chars)
for c2 in chars:
if c1 == c2:
total += 1
c1 = c2
return total
def count_doubles_itertools(val):
c1s, c2s = itertools.tee(val)
next(c2s, None)
total = 0
for c1, c2 in zip(c1s, c2s):
if c1 == c2:
total += 1
return total
double_re = re.compile(r'(?=(.)\1)')
def count_double_regex(val):
return len(double_re.findall(val))
val = ''.join(random.choice(string.ascii_letters) for i in range(1000000))
def test_pure_python(benchmark):
print(benchmark(count_doubles, val))
def test_pure_python_once(benchmark):
print(benchmark(count_doubles_once, val))
def test_itertools(benchmark):
print(benchmark(count_doubles_itertools, val))
def test_regex(benchmark):
print(benchmark(count_double_regex, val))
def test_rust(benchmark):
print(benchmark(myrustlib.count_doubles, val))
def test_rust_once(benchmark):
print(benchmark(myrustlib.count_doubles_once, val))
def test_c_bytes_once(benchmark):
print(benchmark(myclib.count_byte_doubles, val))
# def test_rust_regex(benchmark):
# print(benchmark(myrustlib.count_doubles_regex, val))