1717 python benchmark.py # undirected graphs
1818 python benchmark.py --directed # directed graphs
1919 python benchmark.py --quick # skip the largest graph
20+ python benchmark.py --md # table as MarkDown, to paste into a README
2021 python benchmark.py --rst # table as reST, to paste into the docs
22+
23+ The table is made with ``ansitable``, which also writes it as MarkDown or reST.
24+ With ``--md`` or ``--rst`` only the table is written to standard output, so it
25+ can be redirected to a file, and the description of the run goes to standard
26+ error.
2127"""
2228
2329from __future__ import annotations
3541from typing import Any
3642
3743import numpy as np
44+ from ansitable import ANSITable , Column
3845
3946from pgraph import DGraph , Graph , UGraph
4047
@@ -261,39 +268,23 @@ def fmt(value: float | None) -> str:
261268
262269def make_table (
263270 results : dict [int , dict [str , float | None ]], sizes : list [int ]
264- ) -> list [list [str ]]:
265- rows = [["Operation" , "per" ] + [f"N={ n :,} " for n in sizes ] + ["growth" ]]
271+ ) -> ANSITable :
272+ table = ANSITable (
273+ Column ("Operation" , colalign = "<" , headalign = "<" ),
274+ Column ("per" , colalign = "<" , headalign = "<" ),
275+ * [Column (f"N={ n :,} " ) for n in sizes ],
276+ Column ("growth exponent" ),
277+ border = "thin" ,
278+ )
266279 for key , text , scale , unit in ROWS :
267280 if key not in results [sizes [0 ]]:
268281 continue
269282 vals = [
270283 None if results [n ][key ] is None else results [n ][key ] * scale # type: ignore[operator]
271284 for n in sizes
272285 ]
273- rows .append ([text , unit ] + [fmt (v ) for v in vals ] + [growth (sizes , vals )])
274- return rows
275-
276-
277- def print_text (rows : list [list [str ]]) -> None :
278- widths = [max (len (r [i ]) for r in rows ) for i in range (len (rows [0 ]))]
279- for k , row in enumerate (rows ):
280- cells = [
281- c .ljust (w ) if i < 2 else c .rjust (w )
282- for i , (c , w ) in enumerate (zip (row , widths ))
283- ]
284- print (" " .join (cells ))
285- if k == 0 :
286- print (" " .join ("-" * w for w in widths ))
287-
288-
289- def print_rst (rows : list [list [str ]], title : str ) -> None :
290- print (f".. list-table:: { title } " )
291- print (" :header-rows: 1" )
292- print (" :align: left" )
293- print ()
294- for row in rows :
295- for i , cell in enumerate (row ):
296- print (f" { '*' if i == 0 else ' ' } - { cell } " )
286+ table .row (text , unit , * [fmt (v ) for v in vals ], growth (sizes , vals ))
287+ return table
297288
298289
299290def main () -> None :
@@ -311,7 +302,9 @@ def main() -> None:
311302 help = "largest N for the dense matrix rows (default %(default)s)" ,
312303 )
313304 parser .add_argument ("--plot" , action = "store_true" , help = "also time plot() (needs matplotlib)" )
314- parser .add_argument ("--rst" , action = "store_true" , help = "print the table as reST" )
305+ output = parser .add_mutually_exclusive_group ()
306+ output .add_argument ("--md" , action = "store_true" , help = "print the table as MarkDown" )
307+ output .add_argument ("--rst" , action = "store_true" , help = "print the table as reST" )
315308 args = parser .parse_args ()
316309
317310 sizes = sorted (args .sizes )[:- 1 ] if args .quick and len (args .sizes ) > 1 else sorted (args .sizes )
@@ -329,7 +322,7 @@ def main() -> None:
329322 f" * { NQUERIES } random connected start/goal pairs per size, median reported,\n "
330323 f" * running on { cpu_name ()} , Python { platform .python_version ()} , "
331324 f"numpy { np .__version__ } .\n " ,
332- file = sys .stderr if args .rst else sys .stdout ,
325+ file = sys .stderr if ( args .md or args . rst ) else sys .stdout ,
333326 )
334327
335328 results : dict [int , dict [str , float | None ]] = {}
@@ -338,11 +331,13 @@ def main() -> None:
338331 results [n ] = run_size (cls , n , args .seed , args .matrix_max , args .plot )
339332 print ("done" , file = sys .stderr )
340333
341- rows = make_table (results , sizes )
342- if args .rst :
343- print_rst (rows , f"Performance of { cls .__name__ } (pgraph { version } )" )
334+ table = make_table (results , sizes )
335+ if args .md :
336+ print (table .markdown (), end = "" )
337+ elif args .rst :
338+ print (table .rest (), end = "" )
344339 else :
345- print_text ( rows )
340+ table . print ( )
346341
347342
348343if __name__ == "__main__" :
0 commit comments