tqdm/benchmarks/benchmarks.py

90 lines
2.3 KiB
Python
Raw Normal View History

# Write the benchmarking functions here.
# See "Writing benchmarks" in the asv docs for more information.
from functools import partial
2020-07-11 00:08:21 +00:00
class Comparison:
"""Running time of wrapped empty loops"""
def __init__(self, length):
2016-10-31 00:26:10 +00:00
try:
from time import process_time
self.time = process_time
except ImportError:
from time import clock
self.time = clock
2023-03-03 15:43:42 +00:00
self.iterable = range(int(length))
2020-07-11 00:08:21 +00:00
def run(self, cls):
pbar = cls(self.iterable)
t0 = self.time()
2021-02-22 22:42:24 +00:00
[0 for _ in pbar] # pylint: disable=pointless-statement
t1 = self.time()
2020-07-11 00:08:21 +00:00
return t1 - t0
2020-07-11 00:08:21 +00:00
def run_by_name(self, method):
return getattr(self, method.replace("-", "_"))()
2020-07-11 00:08:21 +00:00
def no_progress(self):
return self.run(lambda x: x)
2016-10-31 01:53:14 +00:00
def tqdm_optimised(self):
2020-07-11 00:08:21 +00:00
from tqdm import tqdm
return self.run(partial(tqdm, miniters=6e5, smoothing=0))
2020-07-11 00:08:21 +00:00
def tqdm(self):
from tqdm import tqdm
return self.run(tqdm)
2020-07-11 00:08:21 +00:00
def alive_progress(self):
from alive_progress import alive_bar
2020-07-11 00:08:21 +00:00
class wrapper:
def __init__(self, iterable):
self.iterable = iterable
2020-07-11 00:08:21 +00:00
def __iter__(self):
iterable = self.iterable
with alive_bar(len(iterable)) as bar:
for i in iterable:
yield i
bar()
2020-07-11 00:08:21 +00:00
return self.run(wrapper)
# def progressbar(self):
# from progressbar.progressbar import ProgressBar
2020-07-11 00:08:21 +00:00
# return self.run(ProgressBar())
def progressbar2(self):
from progressbar import progressbar
2020-07-11 00:08:21 +00:00
return self.run(progressbar)
def rich(self):
from rich.progress import track
2020-07-11 00:08:21 +00:00
return self.run(track)
2020-07-11 00:08:21 +00:00
# thorough test against no-progress
slow = Comparison(6e6)
def track_tqdm(method):
return slow.run_by_name(method)
2020-07-11 00:08:21 +00:00
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):
2020-07-11 00:08:21 +00:00
return fast.run_by_name(library)
track_alternatives.params = ["rich", "progressbar2", "alive-progress", "tqdm"]
track_alternatives.param_names = ["library"]
2020-07-11 00:08:21 +00:00
track_alternatives.unit = "Seconds (lower is better)"