Skip to content

Commit 6a1e774

Browse files
committed
benchmark.py: number of runs on command line and --onlyself
1 parent b1c84c3 commit 6a1e774

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

benchmark.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import asciitable
66
import prettytable
77
import texttable
8+
import sys
9+
import codecs
810

911
setup_code = r"""
1012
from csv import writer
@@ -51,23 +53,36 @@ def run_tabulate(table):
5153
5254
"""
5355

54-
methods = [("join with tabs and newlines", "join_table(table)"),
55-
("csv to StringIO", "csv_table(table)"),
56-
("asciitable (%s)" % asciitable.__version__, "run_asciitable(table)"),
57-
("tabulate (%s)" % tabulate.__version__, "run_tabulate(table)"),
58-
("PrettyTable (%s)" % prettytable.__version__, "run_prettytable(table)"),
59-
("texttable (%s)" % texttable.__version__, "run_texttable(table)"),
56+
methods = [(u"join with tabs and newlines", "join_table(table)"),
57+
(u"csv to StringIO", "csv_table(table)"),
58+
(u"asciitable (%s)" % asciitable.__version__, "run_asciitable(table)"),
59+
(u"tabulate (%s)" % tabulate.__version__, "run_tabulate(table)"),
60+
(u"PrettyTable (%s)" % prettytable.__version__, "run_prettytable(table)"),
61+
(u"texttable (%s)" % texttable.__version__, "run_texttable(table)"),
6062
]
6163

6264

6365
def benchmark(n):
66+
global methods
67+
if '--onlyself' in sys.argv[1:]:
68+
methods = [ m for m in methods if m[0].startswith("tabulate") ]
69+
else:
70+
methods = methods
71+
6472
results = [(desc, timeit(code, setup_code, number=n)/n * 1e6)
6573
for desc, code in methods]
6674
mintime = min(map(lambda x: x[1], results))
67-
results = [(desc, t, t/mintime) for desc, t in results]
68-
print tabulate.tabulate(results,
69-
["Table formatter", "time, μs", "rel. time"],
70-
"rst", floatfmt=".1f")
71-
72-
73-
benchmark(10000)
75+
results = [(desc, t, t/mintime) for desc, t in
76+
sorted(results, key=lambda x: x[1])]
77+
table = tabulate.tabulate(results,
78+
[u"Table formatter", u"time, μs", u"rel. time"],
79+
u"rst", floatfmt=".1f")
80+
print codecs.encode(table, "utf-8")
81+
82+
83+
if __name__ == "__main__":
84+
if sys.argv[1:]:
85+
n = int(sys.argv[1])
86+
else:
87+
n = 10000
88+
benchmark(n)

0 commit comments

Comments
 (0)