-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_cppcount_pybind11.py
61 lines (40 loc) · 1.59 KB
/
benchmark_cppcount_pybind11.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
import os
import time
from cppcount_pybind11 import count_characters, count_values
N = 100
def benchmark_count_characters():
script_dir = os.path.dirname(os.path.realpath(__file__))
input_path = os.path.join(script_dir, 'input.txt')
with open(input_path, 'r') as input_file:
input_string = input_file.read()
start_time = time.time()
for _ in range(N):
_ = count_characters(input_string)
elapsed_time = time.time() - start_time
print('benchmark_count_characters:', elapsed_time / N, 's')
def benchmark_count_values():
script_dir = os.path.dirname(os.path.realpath(__file__))
input_path = os.path.join(script_dir, 'input.txt')
with open(input_path, 'r') as input_file:
input_string = input_file.read()
values = [ord(x) for x in input_string]
start_time = time.time()
for _ in range(N):
_ = count_values(values)
elapsed_time = time.time() - start_time
print('benchmark_count_values:', elapsed_time / N, 's')
def benchmark_count_values_map():
script_dir = os.path.dirname(os.path.realpath(__file__))
input_path = os.path.join(script_dir, 'input.txt')
with open(input_path, 'r') as input_file:
input_string = input_file.read()
values = [ord(x) for x in input_string]
start_time = time.time()
for _ in range(N):
_ = count_values(values, lambda x: x * x)
elapsed_time = time.time() - start_time
print('benchmark_count_values_map:', elapsed_time / N, 's')
if __name__ == '__main__':
benchmark_count_characters()
benchmark_count_values()
benchmark_count_values_map()