2016-10-30 16:15:30 +00:00
|
|
|
# Write the benchmarking functions here.
|
|
|
|
# See "Writing benchmarks" in the asv docs for more information.
|
2016-10-31 00:26:10 +00:00
|
|
|
from __future__ import division
|
2021-01-09 17:00:18 +00:00
|
|
|
|
2020-07-10 17:19:46 +00:00
|
|
|
from functools import partial
|
2016-10-30 16:15:30 +00:00
|
|
|
|
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
class Comparison:
|
|
|
|
"""Running time of wrapped empty loops"""
|
2020-07-10 23:31:45 +00:00
|
|
|
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
|
2016-10-30 16:15:30 +00:00
|
|
|
try:
|
2020-07-10 17:19:46 +00:00
|
|
|
self.iterable = xrange(int(length))
|
2016-10-31 01:53:14 +00:00
|
|
|
except NameError:
|
2020-07-10 17:19:46 +00:00
|
|
|
self.iterable = range(int(length))
|
2016-10-30 16:15:30 +00:00
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
def run(self, cls):
|
2020-07-10 17:19:46 +00:00
|
|
|
pbar = cls(self.iterable)
|
|
|
|
t0 = self.time()
|
2021-02-22 22:42:24 +00:00
|
|
|
[0 for _ in pbar] # pylint: disable=pointless-statement
|
2020-07-10 17:19:46 +00:00
|
|
|
t1 = self.time()
|
2020-07-11 00:08:21 +00:00
|
|
|
return t1 - t0
|
2020-07-10 23:31:45 +00:00
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
def run_by_name(self, method):
|
|
|
|
return getattr(self, method.replace("-", "_"))()
|
2016-10-30 16:15:30 +00:00
|
|
|
|
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
|
|
|
|
2020-07-10 23:31:45 +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-10 17:19:46 +00:00
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
def tqdm(self):
|
|
|
|
from tqdm import tqdm
|
|
|
|
return self.run(tqdm)
|
2020-07-10 23:31:45 +00:00
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
def alive_progress(self):
|
|
|
|
from alive_progress import alive_bar
|
2020-07-10 23:31:45 +00:00
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
class wrapper:
|
|
|
|
def __init__(self, iterable):
|
|
|
|
self.iterable = iterable
|
2020-07-10 17:19:46 +00:00
|
|
|
|
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-10 17:19:46 +00:00
|
|
|
|
2020-07-11 00:08:21 +00:00
|
|
|
return self.run(wrapper)
|
2020-07-10 17:19:46 +00:00
|
|
|
|
2020-07-10 23:31:45 +00:00
|
|
|
# def progressbar(self):
|
2020-07-10 17:19:46 +00:00
|
|
|
# from progressbar.progressbar import ProgressBar
|
2020-07-11 00:08:21 +00:00
|
|
|
# return self.run(ProgressBar())
|
2020-07-10 17:19:46 +00:00
|
|
|
|
2020-07-10 23:31:45 +00:00
|
|
|
def progressbar2(self):
|
2020-07-10 17:19:46 +00:00
|
|
|
from progressbar import progressbar
|
2020-07-11 00:08:21 +00:00
|
|
|
return self.run(progressbar)
|
2020-07-10 17:19:46 +00:00
|
|
|
|
2020-07-10 23:31:45 +00:00
|
|
|
def rich(self):
|
2020-07-10 17:19:46 +00:00
|
|
|
from rich.progress import track
|
2020-07-11 00:08:21 +00:00
|
|
|
return self.run(track)
|
2020-07-10 17:19:46 +00:00
|
|
|
|
|
|
|
|
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-10 17:19:46 +00:00
|
|
|
|
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)
|
2020-07-10 23:31:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
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"]
|
2020-07-10 23:31:45 +00:00
|
|
|
track_alternatives.param_names = ["library"]
|
2020-07-11 00:08:21 +00:00
|
|
|
track_alternatives.unit = "Seconds (lower is better)"
|