[Python-checkins] r53059 - sandbox/trunk/import_in_py/importbench.py

brett.cannon python-checkins at python.org
Mon Dec 18 23:07:09 CET 2006


Author: brett.cannon
Date: Mon Dec 18 23:07:08 2006
New Revision: 53059
Modified:
 sandbox/trunk/import_in_py/importbench.py
Log:
Add tests for importing Python modules.
Also clean up output.
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:07:08 2006
@@ -41,11 +41,11 @@
 """Create requested files."""
 self.directory = tempfile.gettempdir()
 self.py_path = os.path.join(self.directory, self.module_name + '.py')
+ self.pyc_path = self.py_path + ('c' if __debug__ else 'o')
 with open(self.py_path, 'w') as py_file:
 py_file.write("# Temporary file for benchmarking import.")
 if self.pyc:
 compile_to_pyc(self.py_path, doraise=True)
- self.pyc_path = self.py_path + ('c' if __debug__ else 'o')
 if not self.py:
 os.remove(self.py_path)
 sys.path.append(self.directory)
@@ -53,9 +53,9 @@
 
 def __exit__(self, *args):
 """Clean up created state."""
- if self.py:
+ if os.path.exists(self.py_path):
 os.remove(self.py_path)
- if self.pyc:
+ if os.path.exists(self.pyc_path):
 os.remove(self.pyc_path)
 
 
@@ -65,10 +65,10 @@
 sys.path = []
 sys.meta_path = []
 sys.path_importer_cache.clear()
- with PyPycFiles() as py_pyc_state:
+ with PyPycFiles() as file_state:
 # Force the module into sys.modules .
- __import__(py_pyc_state.module_name, {})
- timer = Timer("__import__(%r, {}, {}, [], 0)" % py_pyc_state.module_name)
+ __import__(file_state.module_name, {})
+ timer = Timer("__import__(%r, {}, {}, [], 0)" % file_state.module_name)
 return timer.repeat(repeat, times)
 
 @save_import_state
@@ -90,14 +90,43 @@
 timer = Timer("try:__import__(%r, {}, {}, [], 0)\nexcept ImportError:pass" %
 bad_name)
 return timer.repeat(repeat, times)
- 
- 
+
+def py_pyc_module_benchmark(py, pyc):
+ @save_import_state
+ def inner(times, repeat):
+ 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")
+ return timer.repeat(repeat, times)
+ return inner
+ 
+bench_pyc_with_py = py_pyc_module_benchmark(py=True, pyc=True)
+bench_pyc_without_py = py_pyc_module_benchmark(py=False, pyc=True)
+bench_py_without_pyc = py_pyc_module_benchmark(py=True, pyc=False)
+
+
 def main():
+ max_name_len = max(len(name) for name in globals().keys())
+ repetitions = 2
+ iterations = 10000
+ print ("Repeating tests %s times with %s iterations ..." %
+ (repetitions, iterations))
+ print
+ print "%s time per repetition (in ms)" % "benchmark".ljust(max_name_len)
+ print '-' * 50
 for name, item in globals().iteritems():
 if name.startswith('bench_'):
- timings = item(10000, 1)
- print "%s: %s" % (item.__name__, timings)
- 
+ timings = item(iterations, repetitions)
+ print "%s %s" % (name.ljust(max_name_len),
+ [int(result*1000) for result in timings])
+
 
 if __name__ == '__main__':
 main()


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /