asv: functional refactor to reduce setup overhead

This commit is contained in:
Casper da Costa-Luis 2020-07-11 00:31:45 +01:00
parent 5e64e3854d
commit 7418714377
No known key found for this signature in database
GPG Key ID: 986B408043AE090D
1 changed files with 40 additions and 17 deletions

View File

@ -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)"