File size: 1,365 Bytes
f69d4c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Speed of the main model through the installed wheel: characters per second (whitespace not counted), one thread.
    python bench.py LINES.txt [--batch 64]
One line per call (the mode every tool supports) unless --batch N, through the batch functions. Best of 2 passes."""
import sys
import time

from openthai_nlp_core_lite import NLPCore

lines = [x.rstrip("\n") for x in open(sys.argv[1], encoding="utf-8") if x.strip()]
batch = int(sys.argv[sys.argv.index("--batch") + 1]) if "--batch" in sys.argv else 1
chars = sum(len("".join(x.split())) for x in lines)
nlp = NLPCore(threads=1)
jobs = {
    "words (tokenize_batch)": lambda xs: nlp.tokenize_batch(xs, keep_whitespace=False),
    "sentences (sent_tokenize_batch)": nlp.sent_tokenize_batch,
    "POS (pos_tag_batch)": nlp.pos_tag_batch,
    "named entities (ner_batch)": nlp.ner_batch,
    "dependency trees (dependency_parse_batch)": nlp.dependency_parse_batch,
    "every task (analyze_batch)": nlp.analyze_batch,
}
for name, f in jobs.items():
    f(lines[:4])  # load the heads first
    best = 1e9
    for _ in range(2):
        t0 = time.perf_counter()
        for i in range(0, len(lines), batch):
            f(lines[i:i + batch])
        best = min(best, time.perf_counter() - t0)
    print(f"{name:42s} {chars / best / 1000:7.1f}K chars/s  ({len(lines)} lines, {chars} chars, {batch} per call)")