[Python-checkins] r53060 - sandbox/trunk/import_in_py/importbench.py
brett.cannon
python-checkins at python.org
Mon Dec 18 23:29:28 CET 2006
Author: brett.cannon
Date: Mon Dec 18 23:29:28 2006
New Revision: 53060
Modified:
sandbox/trunk/import_in_py/importbench.py
Log:
Benchmark importing built-ins and frozen modules.
Modified: sandbox/trunk/import_in_py/importbench.py
==============================================================================
--- sandbox/trunk/import_in_py/importbench.py (original)
+++ sandbox/trunk/import_in_py/importbench.py Mon Dec 18 23:29:28 2006
@@ -1,9 +1,19 @@
from py_compile import compile as compile_to_pyc
import os
+import StringIO
import sys
import tempfile
from timeit import Timer
+def import_and_clear_timer(module_name, globals_={}, locals_={}, fromlist=[],
+ level=0):
+ import_stmt = ('__import__(%r, %r, %r, %r, %s)' %
+ (module_name, globals_, locals_, fromlist, level))
+ del_stmt = 'del sys.modules[%r]' % module_name
+ stmt = import_stmt + '; ' + del_stmt
+ # Warm up path_importer_cache and make sure module is not in sys.modules.
+ exec stmt in globals(), {}
+ return Timer(import_stmt + '; ' + del_stmt, 'import sys')
def save_import_state(fxn):
"""Backup and restore the import state."""
@@ -97,13 +107,7 @@
sys.path = []
sys.meta_path = []
with PyPycFiles(py, pyc) as file_state:
- import_stmt = ("__import__(%(module)r, {}, {}, [], 0);"
- "del sys.modules[%(module)r]")
- import_stmt = import_stmt % {'module':file_state.module_name}
- # Warm up path_importer_cache.
- exec import_stmt in globals(), {}
- # Benchmark.
- timer = Timer(import_stmt, "import sys")
+ timer = import_and_clear_timer(file_state.module_name)
return timer.repeat(repeat, times)
return inner
@@ -111,10 +115,30 @@
bench_pyc_without_py = py_pyc_module_benchmark(py=False, pyc=True)
bench_py_without_pyc = py_pyc_module_benchmark(py=True, pyc=False)
+ at save_import_state
+def bench_builtins(times, repeat):
+ """Benchmark the importation of a built-in module."""
+ sys.meta_path = []
+ sys.path = []
+ timer = import_and_clear_timer('xxsubtype')
+ return timer.repeat(repeat, times)
+
+def bench_frozen(times, repeat):
+ """Benchmark the importing of frozen modules."""
+ sys.path = []
+ sys.meta_path = []
+ # Must suppress output from importing __hello__.
+ sys.stdout = StringIO.StringIO()
+ try:
+ timer = import_and_clear_timer('__hello__')
+ return timer.repeat(repeat, times)
+ finally:
+ sys.stdout = sys.__stdout__
+
def main():
max_name_len = max(len(name) for name in globals().keys())
- repetitions = 2
+ repetitions = 3
iterations = 10000
print ("Repeating tests %s times with %s iterations ..." %
(repetitions, iterations))
More information about the Python-checkins
mailing list