"""G16: reproducible measurement records, not a universal performance conclusion."""
import hashlib
import json
import math
import platform
import random
import statistics
import sys
import time
def digest(path):
h=hashlib.sha256()
with open(path,"rb") as f:
for block in iter(lambda:f.read(65536),b""):
h.update(block)
return h.hexdigest()
def paired_trials(old,new,workload,repeats=7):
if type(repeats) is not int or not 2 <= repeats <= 1000:
raise ValueError("2..1000 pairs required")
expected=old(workload)
if new(workload) != expected:
raise AssertionError("methods disagree before timing")
records=[]
for i in range(repeats):
times={}
for name,fn in (("old",old),("new",new)) if i%2 == 0 else (("new",new),("old",old)):
start=time.perf_counter_ns()
result=fn(workload)
elapsed=time.perf_counter_ns()-start
if result != expected:
raise AssertionError("timed output mismatch")
times[name]=elapsed
records.append({"pair":i,"first":"old" if i%2 == 0 else "new",**times})
differences=[r["new"]-r["old"] for r in records]
return {"python":sys.version,"platform":platform.platform(),"units":"ns",
"timing_boundary":"function call only; output check afterward",
"records":records,"median_paired_difference":statistics.median(differences)}
def manual_sum(data):
total=0
for x in data:
total+=x
return total
if __name__ == "__main__":
rng=random.Random(21)
workload=[rng.randrange(-100,101) for _ in range(10000)]
print(json.dumps(paired_trials(manual_sum,sum,workload),indent=2))