forked from tqdm/tqdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks.py
89 lines (64 loc) · 2.33 KB
/
benchmarks.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
82
83
84
85
86
87
88
89
# Write the benchmarking functions here.
# See "Writing benchmarks" in the asv docs for more information.
from functools import partial
class Comparison:
"""Running time of wrapped empty loops"""
def __init__(self, length):
try:
from time import process_time
self.time = process_time
except ImportError:
from time import clock
self.time = clock
self.iterable = range(int(length))
def run(self, cls):
pbar = cls(self.iterable)
t0 = self.time()
[0 for _ in pbar] # pylint: disable=pointless-statement
t1 = self.time()
return t1 - t0
def run_by_name(self, method):
return getattr(self, method.replace("-", "_"))()
def no_progress(self):
return self.run(lambda x: x)
def tqdm_optimised(self):
from tqdm import tqdm
return self.run(partial(tqdm, miniters=6e5, smoothing=0))
def tqdm(self):
from tqdm import tqdm
return self.run(tqdm)
def alive_progress(self):
from alive_progress import alive_bar
class wrapper:
def __init__(self, iterable):
self.iterable = iterable
def __iter__(self):
iterable = self.iterable
with alive_bar(len(iterable)) as bar:
for i in iterable:
yield i
bar()
return self.run(wrapper)
# def progressbar(self):
# from progressbar.progressbar import ProgressBar
# return self.run(ProgressBar())
def progressbar2(self):
from progressbar import progressbar
return self.run(progressbar)
def rich(self):
from rich.progress import track
return self.run(track)
# thorough test against no-progress
slow = Comparison(6e6)
def track_tqdm(method):
return slow.run_by_name(method)
track_tqdm.params = ["tqdm", "tqdm-optimised", "no-progress"]
track_tqdm.param_names = ["method"]
track_tqdm.unit = "Seconds (lower is better)"
# quick test against alternatives
fast = Comparison(1e5)
def track_alternatives(library):
return fast.run_by_name(library)
track_alternatives.params = ["rich", "progressbar2", "alive-progress", "tqdm"]
track_alternatives.param_names = ["library"]
track_alternatives.unit = "Seconds (lower is better)"