Source

pep-456-benchmarks / analyze.py

#!/usr/bin/env python3.3
"""Trivial analyzer
"""
import os
import csv
import statistics

def findcsv():
    for root, dirs, files in os.walk("."):
        if ".hg" in dirs:
            dirs.remove(".hg")
        for fname in files:
            if fname.endswith(".csv"):
                yield os.path.join(root, fname)

def readcsv(fname):
    with open(fname) as f:
        reader = csv.DictReader(f)
        for row in reader:
            yield row["Benchmark"], float(row["Base"]), float(row["Changed"])

def analyze(lines):
    base = []
    changed = []
    diff = []
    for n, b, c in lines:
        base.append(b)
        changed.append(c)
        diff.append(c - b)
    bsum = sum(base)
    bmean = statistics.mean(base)
    csum = sum(changed)
    cmean = statistics.mean(changed)
    var = statistics.variance(diff)
    dev = statistics.stdev(diff)
    dev3 = cmean - (bmean + 3 * dev)
    return bsum, csum, (csum/bsum - 1)*100., bmean, cmean, var, dev, dev3

def main():
    print("filename                               base    changed diff%  bmean  cmean  var    stdev")
    fmt = "{:<36}  {: 1.2f}  {: 1.2f}  {: 1.1f}%  {: 1.3f} {: 1.3f}  {:1.3f}  {:1.3f}  {:1.3f}"
    for fname in sorted(findcsv()):
        lines = list(readcsv(fname))
        name = fname.lstrip("./")[:-4]
        print(fmt.format(name, *analyze(lines)))

if __name__ == "__main__":
    main()