Skip to content

Commit

Permalink
asv: fully functional, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Jul 11, 2020
1 parent 7418714 commit 286fa9e
Showing 1 changed file with 46 additions and 65 deletions.
111 changes: 46 additions & 65 deletions benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,109 +4,90 @@
from functools import partial


class Base:
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
from tqdm import tqdm
self.tqdm = tqdm
try:
self.iterable = xrange(int(length))
except NameError:
self.iterable = range(int(length))

def fraction(self, cls, invert=False):
def run(self, cls):
pbar = cls(self.iterable)
t0 = self.time()
[0 for _ in pbar]
t1 = self.time()
if invert:
return self.bench_time / (t1 - t0 - self.bench_time)
return (t1 - t0 - self.bench_time) / self.bench_time


class Overhead(Base):
"""Fractional overhead compared to an empty loop"""
def __init__(self):
super(Overhead, self).__init__(6e6)
return t1 - t0

# compare to empty loop
t0 = self.time()
[0 for _ in self.iterable]
t1 = self.time()
self.bench_time = t1 - t0
def run_by_name(self, method):
return getattr(self, method.replace("-", "_"))()

def tqdm_basic(self):
return self.fraction(self.tqdm)
def no_progress(self):
return self.run(lambda x: x)

def tqdm_optimised(self):
return self.fraction(partial(self.tqdm, miniters=6e5, smoothing=0))

from tqdm import tqdm
return self.run(partial(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)"
def tqdm(self):
from tqdm import tqdm
return self.run(tqdm)

def alive_progress(self):
from alive_progress import alive_bar

class Alternatives(Base):
"""Fractional overhead compared to alternatives"""
def __init__(self):
super(Alternatives, self).__init__(1e5)
class wrapper:
def __init__(self, iterable):
self.iterable = iterable

# compare to `tqdm`
with self.tqdm(self.iterable) as pbar:
t0 = self.time()
[0 for _ in pbar]
t1 = self.time()
self.bench_time = t1 - t0
def __iter__(self):
iterable = self.iterable
with alive_bar(len(iterable)) as bar:
for i in iterable:
yield i
bar()

# invert to track `tqdm` regressions (rather than the alternative)
self.fraction = partial(self.fraction, invert=True)
return self.run(wrapper)

# def progressbar(self):
# from progressbar.progressbar import ProgressBar
# return self.fraction(ProgressBar())
# return self.run(ProgressBar())

def progressbar2(self):
from progressbar import progressbar
return self.fraction(progressbar)
return self.run(progressbar)

def rich(self):
from rich.progress import track
return self.fraction(track)
return self.run(track)

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()
# thorough test against no-progress
slow = Comparison(6e6)


def track_tqdm(method):
return slow.run_by_name(method)

return self.fraction(wrapper)

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)


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"]
return fast.run_by_name(library)


track_alternatives.params = ["rich", "progressbar2", "alive-progress", "tqdm"]
track_alternatives.param_names = ["library"]
track_alternatives.unit = "Relative speed (higher is better)"
track_alternatives.unit = "Seconds (lower is better)"

0 comments on commit 286fa9e

Please sign in to comment.