mirror of https://github.com/tqdm/tqdm.git
asv: functional refactor to reduce setup overhead
This commit is contained in:
parent
5e64e3854d
commit
7418714377
|
@ -5,7 +5,7 @@ from functools import partial
|
|||
|
||||
|
||||
class Base:
|
||||
def setup(self, length=6e6):
|
||||
def __init__(self, length):
|
||||
try:
|
||||
from time import process_time
|
||||
self.time = process_time
|
||||
|
@ -19,11 +19,6 @@ class Base:
|
|||
except NameError:
|
||||
self.iterable = range(int(length))
|
||||
|
||||
t0 = self.time()
|
||||
[0 for _ in self.iterable]
|
||||
t1 = self.time()
|
||||
self.bench_time = t1 - t0
|
||||
|
||||
def fraction(self, cls, invert=False):
|
||||
pbar = cls(self.iterable)
|
||||
t0 = self.time()
|
||||
|
@ -36,22 +31,39 @@ class Base:
|
|||
|
||||
class Overhead(Base):
|
||||
"""Fractional overhead compared to an empty loop"""
|
||||
def setup(self):
|
||||
super(Overhead, self).setup(6e6)
|
||||
def __init__(self):
|
||||
super(Overhead, self).__init__(6e6)
|
||||
|
||||
def track_tqdm(self):
|
||||
# compare to empty loop
|
||||
t0 = self.time()
|
||||
[0 for _ in self.iterable]
|
||||
t1 = self.time()
|
||||
self.bench_time = t1 - t0
|
||||
|
||||
def tqdm_basic(self):
|
||||
return self.fraction(self.tqdm)
|
||||
|
||||
def track_optimsed(self):
|
||||
def tqdm_optimised(self):
|
||||
return self.fraction(partial(self.tqdm, miniters=6e5, smoothing=0))
|
||||
|
||||
|
||||
overhead = Overhead()
|
||||
def track_tqdm(method):
|
||||
if method == "no-progress":
|
||||
return 1
|
||||
global overhead
|
||||
return getattr(overhead, method.replace("-", "_"))()
|
||||
track_tqdm.params = ["no-progress", "tqdm-basic", "tqdm-optimised"]
|
||||
track_tqdm.param_names = ["method"]
|
||||
track_tqdm.unit = "Relative time (lower is better)"
|
||||
|
||||
|
||||
class Alternatives(Base):
|
||||
"""Fractional overhead compared to alternatives"""
|
||||
def setup(self):
|
||||
super(Alternatives, self).setup(1e5)
|
||||
def __init__(self):
|
||||
super(Alternatives, self).__init__(1e5)
|
||||
|
||||
# compare to `tqdm` rather than empty loop
|
||||
# compare to `tqdm`
|
||||
with self.tqdm(self.iterable) as pbar:
|
||||
t0 = self.time()
|
||||
[0 for _ in pbar]
|
||||
|
@ -61,19 +73,19 @@ class Alternatives(Base):
|
|||
# invert to track `tqdm` regressions (rather than the alternative)
|
||||
self.fraction = partial(self.fraction, invert=True)
|
||||
|
||||
# def track_progressbar(self):
|
||||
# def progressbar(self):
|
||||
# from progressbar.progressbar import ProgressBar
|
||||
# return self.fraction(ProgressBar())
|
||||
|
||||
def track_progressbar2(self):
|
||||
def progressbar2(self):
|
||||
from progressbar import progressbar
|
||||
return self.fraction(progressbar)
|
||||
|
||||
def track_rich(self):
|
||||
def rich(self):
|
||||
from rich.progress import track
|
||||
return self.fraction(track)
|
||||
|
||||
def track_alive_progress(self):
|
||||
def alive_progress(self):
|
||||
from alive_progress import alive_bar
|
||||
|
||||
class wrapper:
|
||||
|
@ -87,3 +99,14 @@ class Alternatives(Base):
|
|||
bar()
|
||||
|
||||
return self.fraction(wrapper)
|
||||
|
||||
|
||||
alternatives = Alternatives()
|
||||
def track_alternatives(library):
|
||||
if library == "tqdm":
|
||||
return 1
|
||||
global alternatives
|
||||
return getattr(alternatives, library.replace("-", "_"))()
|
||||
track_alternatives.params = ["tqdm", "progressbar2", "rich", "alive-progress"]
|
||||
track_alternatives.param_names = ["library"]
|
||||
track_alternatives.unit = "Relative speed (higher is better)"
|
||||
|
|
Loading…
Reference in New Issue