From e1189a7a3816e9be64dcfee6f8886cc230f14c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20M=2E=20Thi=C3=A9ry?= Date: Fri, 7 Oct 2022 11:37:32 +0200 Subject: [PATCH] Python 3 support --- bleachermark.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/bleachermark.py b/bleachermark.py index e319701..ab366be 100644 --- a/bleachermark.py +++ b/bleachermark.py @@ -55,7 +55,12 @@ ('stupid benchmark', 'zero', 1, 0.0, 0)] """ -from time import clock +# Support both Python 2 (using clock) and Python 3 (using perf_counter) +try: + from time import perf_counter +except (ImportError, AttributeError): + from time import clock as perf_counter + from copy import copy #This part handles the ctrl-c interruption. @@ -147,9 +152,9 @@ def run(self, i): time_vals = [i] intervalue = i for fun in self._pipeline: - tim = clock() + tim = perf_counter() intervalue = fun(intervalue) - time_vals.append( (clock()-tim, intervalue) ) + time_vals.append( (perf_counter()-tim, intervalue) ) return time_vals @@ -308,7 +313,7 @@ def fetch_data(self, format="dict"): data.append( (label, fun_labels[i], run[0], m[0], m[1]) ) return data else: - raise ValueError("Invalid argument to format: %s".format(format)) + raise ValueError(f"Invalid argument to format: {format}") def timings(self, transposed=False): r""" @@ -344,11 +349,8 @@ def averages(self): """ timings = self.timings(transposed=True) - res = {} - for bm in timings.keys(): - totals = map(lambda a: sum(a)/len(a), timings[bm]) - res[bm] = totals - return res + return {bm: [sum(t)/len(t) for t in timings[bm]] + for bm in timings.keys() } def variances(self): r""" @@ -375,21 +377,24 @@ def stdvs(self): """ variances = self.variances() import math - return {bm:map(math.sqrt, variances[bm]) for bm in variances} + return {bm: [math.sqrt(v) for v in variances[bm]] + for bm in variances} def maxes(self): r""" Return the maximum running times of the benchmarks run. """ timings = self.timings(transposed=True) - return {bm:map(max, timings[bm]) for bm in timings} + return {bm: [max(t) for t in timings[bm]] + for bm in timings} def mins(self): r""" Return the minimum running times of the benchmarks run. """ timings = self.timings(transposed=True) - return {bm:map(min, timings[bm]) for bm in timings} + return {bm: [min(t) for t in timings[bm]] + for bm in timings} def pipeline_data(self): r"""