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

brett.cannon python-checkins at python.org
Tue Dec 19 02:23:28 CET 2006


Author: brett.cannon
Date: Tue Dec 19 02:23:27 2006
New Revision: 53061

Modified:
   sandbox/trunk/import_in_py/importbench.py
Log:
Add benchmark for extension modules.  This also found a bug where bench_frozen
was not cleaning up import state properly.


Modified: sandbox/trunk/import_in_py/importbench.py
==============================================================================
--- sandbox/trunk/import_in_py/importbench.py	(original)
+++ sandbox/trunk/import_in_py/importbench.py	Tue Dec 19 02:23:27 2006
@@ -122,7 +122,8 @@
     sys.path = []
     timer = import_and_clear_timer('xxsubtype')
     return timer.repeat(repeat, times)
-    
+ 
+ at save_import_state 
 def bench_frozen(times, repeat):
     """Benchmark the importing of frozen modules."""
     sys.path = []
@@ -135,8 +136,26 @@
     finally:
         sys.stdout = sys.__stdout__
 
+ at save_import_state
+def bench_extension(times, repeat):
+    """Benchmark the importing of an extension module."""
+    # Choose an extension module that is always included with Python.
+    module = 'datetime'
+    # Import the object to find out which entry in sys.path contains the module.
+    module_object = __import__(module, {}, {}, [], 0)
+    directory = os.path.split(module_object.__file__)[0]
+    sys.meta_path = []
+    sys.path = [directory]
+    timer = import_and_clear_timer(module)
+    return timer.repeat(repeat, times)
+
 
-def main():
+def display_results(fxn, name, spacing, repeat, times):
+    timings = fxn(times, repeat)
+    print "%s %s" % (name.ljust(spacing),
+                             [int(result*1000) for result in timings])
+
+def main(tests=None):
     max_name_len = max(len(name) for name in globals().keys())
     repetitions = 3
     iterations = 10000
@@ -145,12 +164,14 @@
     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(iterations, repetitions)
-            print "%s %s" % (name.ljust(max_name_len),
-                             [int(result*1000) for result in timings])
+    if tests is None:
+        tests = (item for item in globals().iterkeys()
+                    if item.startswith('bench_'))
+    globals_ = globals()
+    for name in tests:
+        item = globals_[name]
+        display_results(item, name, max_name_len, repetitions, iterations)
 
             
 if __name__ == '__main__':
-    main()
+    main(sys.argv[1:] if len(sys.argv) > 1 else None)


More information about the Python-checkins mailing list