-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.py
51 lines (42 loc) · 1.28 KB
/
bench.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
from io import TextIOBase
import sys
import time
from typing import Any, Callable, Optional
from pathlib import Path
def benchmark_impl(
msg: bytes,
detector: Callable[[Any], Any],
n_calls: int,
module: Any,
output_buf: Optional[TextIOBase] = None,
):
result = 0
for _ in range(n_calls):
start = time.time()
detector(msg)
result += time.time() - start
print(
"%s v%s:" % (module.__name__, module.__version__),
1 / (result / n_calls),
"call(s)/s",
file=(output_buf or sys.stdout),
)
def main():
import chardet
import cchardet
import rs_chardet
do_times = 5
path = (
Path(__file__).parent
) / "samples/wikipediaJa_One_Thousand_and_One_Nights_SJIS.txt"
with path.open("rb") as f:
msg = f.read()
detector_chardet = lambda msg: chardet.detect(msg)
detector_rschardet = lambda msg: rs_chardet.detect_rs_enc_name(msg)
detector_cchardet = lambda msg: cchardet.detect(msg)
# Test chardet
benchmark_impl(msg, detector_chardet, do_times, chardet, None)
benchmark_impl(msg, detector_rschardet, do_times, rs_chardet, None)
benchmark_impl(msg, detector_cchardet, do_times, cchardet, None)
if __name__ == "__main__":
main()