annotate Lib/importlib/test/benchmark.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 305902b3c1a0
children fc007eef3c0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
1 """Benchmark some basic import use-cases.
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
2
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
3 The assumption is made that this benchmark is run in a fresh interpreter and
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
4 thus has no external changes made to import-related attributes in sys.
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
5
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
6 """
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7 from . import util
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8 from .source import util as source_util
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
9 import decimal
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
10 import imp
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
11 import importlib
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
12 import os
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
13 import py_compile
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14 import sys
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 import timeit
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
18 def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3):
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
19 """Bench the given statement as many times as necessary until total
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
20 executions take one second."""
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
21 stmt = "__import__({!r})".format(name)
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
22 timer = timeit.Timer(stmt)
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
23 for x in range(repeat):
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
24 total_time = 0
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
25 count = 0
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
26 while total_time < seconds:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
27 try:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
28 total_time += timer.timeit(1)
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
29 finally:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
30 cleanup()
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
31 count += 1
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
32 else:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
33 # One execution too far
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
34 if total_time > seconds:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
35 count -= 1
63038
2026ea442889 Touch up comments and code along with outputting what the unit of measure is.
Brett Cannon <bcannon@gmail.com>
parents: 55811
diff changeset
36 yield count // seconds
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
37
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
38 def from_cache(seconds, repeat):
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
39 """sys.modules"""
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 name = '<benchmark import>'
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
41 module = imp.new_module(name)
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
42 module.__file__ = '<test>'
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
43 module.__package__ = ''
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
44 with util.uncache(name):
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45 sys.modules[name] = module
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
46 for result in bench(name, repeat=repeat, seconds=seconds):
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
47 yield result
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
48
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
49
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
50 def builtin_mod(seconds, repeat):
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
51 """Built-in module"""
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
52 name = 'errno'
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
53 if name in sys.modules:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
54 del sys.modules[name]
63038
2026ea442889 Touch up comments and code along with outputting what the unit of measure is.
Brett Cannon <bcannon@gmail.com>
parents: 55811
diff changeset
55 # Relying on built-in importer being implicit.
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
56 for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
57 seconds=seconds):
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
58 yield result
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
61 def source_wo_bytecode(seconds, repeat):
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
62 """Source w/o bytecode: simple"""
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
63 sys.dont_write_bytecode = True
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
64 try:
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
65 name = '__importlib_test_benchmark__'
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
66 # Clears out sys.modules and puts an entry at the front of sys.path.
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
67 with source_util.create_modules(name) as mapping:
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
68 assert not os.path.exists(imp.cache_from_source(mapping[name]))
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
69 for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
70 seconds=seconds):
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
71 yield result
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
72 finally:
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
73 sys.dont_write_bytecode = False
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
74
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
75
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
76 def decimal_wo_bytecode(seconds, repeat):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
77 """Source w/o bytecode: decimal"""
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
78 name = 'decimal'
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
79 decimal_bytecode = imp.cache_from_source(decimal.__file__)
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
80 if os.path.exists(decimal_bytecode):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
81 os.unlink(decimal_bytecode)
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
82 sys.dont_write_bytecode = True
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
83 try:
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
84 for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
85 seconds=seconds):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
86 yield result
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
87 finally:
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
88 sys.dont_write_bytecode = False
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
89
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
90
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
91 def source_writing_bytecode(seconds, repeat):
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
92 """Source writing bytecode: simple"""
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
93 assert not sys.dont_write_bytecode
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
94 name = '__importlib_test_benchmark__'
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
95 with source_util.create_modules(name) as mapping:
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
96 def cleanup():
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
97 sys.modules.pop(name)
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
98 os.unlink(imp.cache_from_source(mapping[name]))
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
99 for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
100 assert not os.path.exists(imp.cache_from_source(mapping[name]))
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
101 yield result
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
102
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
103
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
104 def decimal_writing_bytecode(seconds, repeat):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
105 """Source writing bytecode: decimal"""
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
106 assert not sys.dont_write_bytecode
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
107 name = 'decimal'
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
108 def cleanup():
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
109 sys.modules.pop(name)
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
110 os.unlink(imp.cache_from_source(decimal.__file__))
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
111 for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
112 yield result
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
113
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
114
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
115 def source_using_bytecode(seconds, repeat):
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
116 """Bytecode w/ source: simple"""
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
117 name = '__importlib_test_benchmark__'
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
118 with source_util.create_modules(name) as mapping:
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
119 py_compile.compile(mapping[name])
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
120 assert os.path.exists(imp.cache_from_source(mapping[name]))
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
121 for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
122 seconds=seconds):
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
123 yield result
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
124
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
125
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
126 def decimal_using_bytecode(seconds, repeat):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
127 """Bytecode w/ source: decimal"""
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
128 name = 'decimal'
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
129 py_compile.compile(decimal.__file__)
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
130 for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
131 seconds=seconds):
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
132 yield result
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
133
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
134
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
135 def main(import_):
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
136 __builtins__.__import__ = import_
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
137 benchmarks = (from_cache, builtin_mod,
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
138 source_using_bytecode, source_wo_bytecode,
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
139 source_writing_bytecode,
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
140 decimal_using_bytecode, decimal_writing_bytecode,
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
141 decimal_wo_bytecode,)
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
142 seconds = 1
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
143 seconds_plural = 's' if seconds > 1 else ''
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
144 repeat = 3
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
145 header = "Measuring imports/second over {} second{}, best out of {}\n"
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
146 print(header.format(seconds, seconds_plural, repeat))
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
147 for benchmark in benchmarks:
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
148 print(benchmark.__doc__, "[", end=' ')
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
149 sys.stdout.flush()
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
150 results = []
63135
305902b3c1a0 Add importlib benchmarks which try to be "realistic" by importing the decimal
Brett Cannon <bcannon@gmail.com>
parents: 63046
diff changeset
151 for result in benchmark(seconds=seconds, repeat=repeat):
55811
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
152 results.append(result)
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
153 print(result, end=' ')
3b9e75311e43 Rework importlib benchmarks so that they measure number of executions within a
Brett Cannon <bcannon@gmail.com>
parents: 52442
diff changeset
154 sys.stdout.flush()
63043
e2ec5fbc6cf1 Add benchmarks for importing just source w/o writing bytecode, importing source
Brett Cannon <bcannon@gmail.com>
parents: 63038
diff changeset
155 assert not sys.dont_write_bytecode
63046
84ff41794d3f Add comma grouping to max result so it's easier to read.
Brett Cannon <bcannon@gmail.com>
parents: 63043
diff changeset
156 print("]", "best is", format(max(results), ',d'))
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
157
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
158
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
159 if __name__ == '__main__':
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
160 import optparse
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
161
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
162 parser = optparse.OptionParser()
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
163 parser.add_option('-b', '--builtin', dest='builtin', action='store_true',
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
164 default=False, help="use the built-in __import__")
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
165 options, args = parser.parse_args()
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
166 if args:
63038
2026ea442889 Touch up comments and code along with outputting what the unit of measure is.
Brett Cannon <bcannon@gmail.com>
parents: 55811
diff changeset
167 raise RuntimeError("unrecognized args: {}".format(args))
52442
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
168 import_ = __import__
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
169 if not options.builtin:
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
170 import_ = importlib.__import__
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
171
b9c499e9f74d Add simple tests for __import__ for future optimizations to importlib.
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
172 main(import_)