Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Temporarily added a script for benchmarking ABCMeta. This script will…
… be removed in a future commit.
  • Loading branch information
alegonz committed Jun 16, 2019
commit e3f8e026f77bdbe2f6d91bbb05df1a0268271078
56 changes: 56 additions & 0 deletions abc_bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import csv

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was temporarily added for sharing purposes. It will removed in a future commit.

import timeit


def median(x):
n = len(x)
middle = n // 2
sx = sorted(x)
if n % 2:
return sx[middle]
else:
return (sx[middle] + sx[middle - 1]) / 2


implementations = {"C": "import abc",
"Py": "import _py_abc"}
statements = {
"C": {
"master": "abc.ABCMeta('ABC_C', (), {'__module__': __name__})",
"fix": "abc.ABCMeta('ABC_C', (), {})"
},
"Py": {
"master": "_py_abc.ABCMeta('ABC_Py', (), {'__module__': __name__})",
"fix": "_py_abc.ABCMeta('ABC_Py', (), {})"
}
}

repeat = 50000
number = 1

data = {}
for imp, setup in implementations.items():
for branch, stmt in statements[imp].items():
print("timing {} - {} implementation of ABCMeta...".format(branch, imp))
times = timeit.repeat(stmt, setup=setup, repeat=repeat, number=number)
header = "{}_{}".format(branch, imp)
data[header] = times


for imp in implementations:
t_master = median(data["master_{}".format(imp)])
t_fix = median(data["fix_{}".format(imp)])

absdiff = t_fix - t_master
slowdown = (t_fix - t_master) / t_master

print("{} implementation".format(imp))
print(" Absolute difference:", 1000 * absdiff, "ms")
print(" Slowdown:", 100 * slowdown, "%")

print("Dumping to CSV file...")
with open("abc_bench.csv", "w") as csvfile:
writer = csv.writer(csvfile)
data = [[header] + times for header, times in data.items()]
for row in zip(*data):
writer.writerow(row)