1#----------------------------------------------------------------- 2# Benchmarking utility for internal use. 3# 4# Use with Python 3.6+ 5# 6# Eli Bendersky [https://eli.thegreenplace.net/] 7# License: BSD 8#----------------------------------------------------------------- 9import os 10import statistics 11import sys 12import time 13 14sys.path.extend(['.', '..']) 15 16from pycparser import c_parser, c_ast 17 18 19def measure_parse(text, n, progress_cb): 20 """Measure the parsing of text with pycparser. 21 22 text should represent a full file. n is the number of iterations to measure. 23 progress_cb will be called with the iteration number each time one is done. 24 25 Returns a list of elapsed times, one per iteration. 26 """ 27 times = [] 28 for i in range(n): 29 parser = c_parser.CParser() 30 t1 = time.time() 31 ast = parser.parse(text, '') 32 elapsed = time.time() - t1 33 assert isinstance(ast, c_ast.FileAST) 34 times.append(elapsed) 35 progress_cb(i) 36 return times 37 38 39def measure_file(filename, n): 40 progress_cb = lambda i: print('.', sep='', end='', flush=True) 41 with open(filename) as f: 42 print('%-25s' % os.path.basename(filename), end='', flush=True) 43 text = f.read() 44 times = measure_parse(text, n, progress_cb) 45 print(' Mean: %.3f Stddev: %.3f' % (statistics.mean(times), 46 statistics.stdev(times))) 47 48 49NUM_RUNS = 5 50 51 52if __name__ == '__main__': 53 if len(sys.argv) < 2: 54 print("Usage: %s <dir with input files>") 55 sys.exit(1) 56 for filename in os.listdir(sys.argv[1]): 57 filename = os.path.join(sys.argv[1], filename) 58 measure_file(filename, NUM_RUNS) 59