benchmark.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from __future__ import print_function
  4. from timeit import timeit
  5. import tabulate
  6. import asciitable
  7. import prettytable
  8. import texttable
  9. import sys
  10. import codecs
  11. from platform import python_version_tuple
  12. setup_code = r"""
  13. from csv import writer
  14. try: # Python 2
  15. from StringIO import StringIO
  16. except: # Python 3
  17. from io import StringIO
  18. import tabulate
  19. import asciitable
  20. import prettytable
  21. import texttable
  22. import platform
  23. if platform.platform().startswith("Windows") \
  24. and \
  25. platform.python_version_tuple() < ('3','6','0'):
  26. import win_unicode_console
  27. win_unicode_console.enable()
  28. table=[["some text"]+list(range(i,i+9)) for i in range(10)]
  29. def csv_table(table):
  30. buf = StringIO()
  31. writer(buf).writerows(table)
  32. return buf.getvalue()
  33. def join_table(table):
  34. return "\n".join(("\t".join(map(str,row)) for row in table))
  35. def run_prettytable(table):
  36. pp = prettytable.PrettyTable()
  37. for row in table:
  38. pp.add_row(row)
  39. return str(pp)
  40. def run_asciitable(table):
  41. buf = StringIO()
  42. asciitable.write(table, output=buf, Writer=asciitable.FixedWidth)
  43. return buf.getvalue()
  44. def run_texttable(table):
  45. pp = texttable.Texttable()
  46. pp.set_cols_align(["l"] + ["r"]*9)
  47. pp.add_rows(table)
  48. return pp.draw()
  49. def run_tabletext(table):
  50. return tabletext.to_text(table)
  51. def run_tabulate(table, widechars=False):
  52. tabulate.WIDE_CHARS_MODE = tabulate.wcwidth is not None and widechars
  53. return tabulate.tabulate(table)
  54. """
  55. methods = [
  56. ("join with tabs and newlines", "join_table(table)"),
  57. ("csv to StringIO", "csv_table(table)"),
  58. ("asciitable (%s)" % asciitable.__version__, "run_asciitable(table)"),
  59. ("tabulate (%s)" % tabulate.__version__, "run_tabulate(table)"),
  60. (
  61. "tabulate (%s, WIDE_CHARS_MODE)" % tabulate.__version__,
  62. "run_tabulate(table, widechars=True)",
  63. ),
  64. ("PrettyTable (%s)" % prettytable.__version__, "run_prettytable(table)"),
  65. ("texttable (%s)" % texttable.__version__, "run_texttable(table)"),
  66. ]
  67. if tabulate.wcwidth is None:
  68. del methods[4]
  69. def benchmark(n):
  70. global methods
  71. if "--onlyself" in sys.argv[1:]:
  72. methods = [m for m in methods if m[0].startswith("tabulate")]
  73. results = [
  74. (desc, timeit(code, setup_code, number=n) / n * 1e6) for desc, code in methods
  75. ]
  76. mintime = min(map(lambda x: x[1], results))
  77. results = [
  78. (desc, t, t / mintime) for desc, t in sorted(results, key=lambda x: x[1])
  79. ]
  80. table = tabulate.tabulate(
  81. results, ["Table formatter", "time, μs", "rel. time"], "rst", floatfmt=".1f"
  82. )
  83. from platform import platform
  84. if platform().startswith("Windows"):
  85. print(table)
  86. elif python_version_tuple()[0] < "3":
  87. print(codecs.encode(table, "utf-8"))
  88. else:
  89. print(table)
  90. if __name__ == "__main__":
  91. if sys.argv[1:]:
  92. n = int(sys.argv[1])
  93. else:
  94. n = 10000
  95. benchmark(n)