MCPcopy Index your code
hub / github.com/python/cpython / main

Function main

Tools/importbench/importbench.py:166–222  ·  view source on GitHub ↗
(import_, options)

Source from the content-addressed store, hash-verified

164
165
166def main(import_, options):
167 if options.source_file:
168 with open(options.source_file, 'r', encoding='utf-8') as source_file:
169 prev_results = json.load(source_file)
170 else:
171 prev_results = {}
172 __builtins__.__import__ = import_
173 benchmarks = (from_cache, builtin_mod,
174 source_writing_bytecode,
175 source_wo_bytecode, source_using_bytecode,
176 tabnanny_writing_bytecode,
177 tabnanny_wo_bytecode, tabnanny_using_bytecode,
178 decimal_writing_bytecode,
179 decimal_wo_bytecode, decimal_using_bytecode,
180 )
181 if options.benchmark:
182 for b in benchmarks:
183 if b.__doc__ == options.benchmark:
184 benchmarks = [b]
185 break
186 else:
187 print('Unknown benchmark: {!r}'.format(options.benchmark),
188 file=sys.stderr)
189 sys.exit(1)
190 seconds = 1
191 seconds_plural = 's' if seconds > 1 else ''
192 repeat = 3
193 header = ('Measuring imports/second over {} second{}, best out of {}\n'
194 'Entire benchmark run should take about {} seconds\n'
195 'Using {!r} as __import__\n')
196 print(header.format(seconds, seconds_plural, repeat,
197 len(benchmarks) * seconds * repeat, __import__))
198 new_results = {}
199 for benchmark in benchmarks:
200 print(benchmark.__doc__, "[", end=' ')
201 sys.stdout.flush()
202 results = []
203 for result in benchmark(seconds=seconds, repeat=repeat):
204 results.append(result)
205 print(result, end=' ')
206 sys.stdout.flush()
207 assert not sys.dont_write_bytecode
208 print("]", "best is", format(max(results), ',d'))
209 new_results[benchmark.__doc__] = results
210 if prev_results:
211 print('\n\nComparing new vs. old\n')
212 for benchmark in benchmarks:
213 benchmark_name = benchmark.__doc__
214 old_result = max(prev_results[benchmark_name])
215 new_result = max(new_results[benchmark_name])
216 result = '{:,d} vs. {:,d} ({:%})'.format(new_result,
217 old_result,
218 new_result/old_result)
219 print(benchmark_name, ':', result)
220 if options.dest_file:
221 with open(options.dest_file, 'w', encoding='utf-8') as dest_file:
222 json.dump(new_results, dest_file, indent=2)
223

Callers 1

importbench.pyFile · 0.70

Calls 9

formatFunction · 0.85
openFunction · 0.50
benchmarkFunction · 0.50
loadMethod · 0.45
formatMethod · 0.45
exitMethod · 0.45
flushMethod · 0.45
appendMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…