diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2022-05-28 15:30:36 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2022-05-28 15:30:36 -0400 |
commit | 1087f173f877c66872b0d993feb0860560def466 (patch) | |
tree | d56317fde626e58024a82e353a153b710c966865 /perf/bug397.py | |
parent | 0aa1070a2c9a99e10b7790b9a6a40a631ba5514e (diff) | |
download | python-coveragepy-git-1087f173f877c66872b0d993feb0860560def466.tar.gz |
test: new benchmark.py for comparing performance
Also, delete the old perf/ directory which isn't useful.
Diffstat (limited to 'perf/bug397.py')
-rw-r--r-- | perf/bug397.py | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/perf/bug397.py b/perf/bug397.py deleted file mode 100644 index 18c979b8..00000000 --- a/perf/bug397.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python -""" -Run this file two ways under coverage and see that the times are the same: - - $ coverage run lab/bug397.py slow - Runtime per example: 130.96 +/- 3.70 us - $ coverage run lab/bug397.py fast - Runtime per example: 131.34 +/- 4.48 us - -Written by David MacIver as part of https://github.com/nedbat/coveragepy/issues/397 - -""" - -import sys -import random -import time -import math - -if sys.argv[1] == "slow": - sys.settrace(sys.gettrace()) - -random.seed(1) - - -def hash_str(s): - h = 0 - for c in s: - h = (h * 31 + ord(c)) & (2 ** 64 - 1) - return h - -data = [ - hex(random.getrandbits(1024)) for _ in range(500) -] - -N_SAMPLES = 100 - - -def mean(xs): - xs = list(xs) - return sum(xs) / len(xs) - - -def sd(xs): - return math.sqrt(mean(x ** 2 for x in xs) - mean(xs) ** 2) - - -if __name__ == '__main__': - timing = [] - for _ in range(N_SAMPLES): - start = time.time() - for d in data: - hash_str(d) - timing.append(1000000 * (time.time() - start) / len(data)) - print("Runtime per example:", f"{mean(timing):.2f} +/- {sd(timing):.2f} us") |