aboutsummaryrefslogtreecommitdiff
path: root/utils/benchmark/benchmark-parse.py
diff options
context:
space:
mode:
authorEli Bendersky <eliben@google.com>2019-08-23 13:29:21 -0700
committerEli Bendersky <eliben@google.com>2019-08-23 13:29:21 -0700
commitb95fb13996f23de6e64e5a0c46c8511375c70280 (patch)
treebe7bbc9424a86cac4178a7ec60d900839180d171 /utils/benchmark/benchmark-parse.py
parentc2e34dc6b6ca06705b2c7862486dff151f9f03ea (diff)
downloadpycparser-b95fb13996f23de6e64e5a0c46c8511375c70280.tar.gz
Move benchmarking stuff to its own dir
Diffstat (limited to 'utils/benchmark/benchmark-parse.py')
-rw-r--r--utils/benchmark/benchmark-parse.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/utils/benchmark/benchmark-parse.py b/utils/benchmark/benchmark-parse.py
new file mode 100644
index 0000000..b2552fe
--- /dev/null
+++ b/utils/benchmark/benchmark-parse.py
@@ -0,0 +1,51 @@
+#-----------------------------------------------------------------
+# Benchmarking utility for internal use.
+#
+# Use with Python 3.6+
+#
+# Eli Bendersky [https://eli.thegreenplace.net/]
+# License: BSD
+#-----------------------------------------------------------------
+import os
+import statistics
+import sys
+import time
+
+sys.path.extend(['.', '..'])
+
+from pycparser import c_parser, c_ast
+
+
+def measure_parse(text, n, progress_cb):
+ times = []
+ for i in range(n):
+ parser = c_parser.CParser()
+ t1 = time.time()
+ ast = parser.parse(text, '')
+ elapsed = time.time() - t1
+ assert isinstance(ast, c_ast.FileAST)
+ times.append(elapsed)
+ progress_cb(i)
+ return times
+
+
+def measure_file(filename, n):
+ progress_cb = lambda i: print('.', sep='', end='', flush=True)
+ with open(filename) as f:
+ print('%-20s' % os.path.basename(filename), end='', flush=True)
+ text = f.read()
+ times = measure_parse(text, n, progress_cb)
+ print(' Mean: %.3f Stddev: %.3f' % (statistics.mean(times),
+ statistics.stdev(times)))
+
+
+NUM_RUNS = 5
+
+
+if __name__ == '__main__':
+ if len(sys.argv) < 2:
+ print("Usage: %s <dir with input files>")
+ sys.exit(1)
+ for filename in os.listdir(sys.argv[1]):
+ filename = os.path.join(sys.argv[1], filename)
+ measure_file(filename, NUM_RUNS)