Back to index

python3.2  3.2.2
Functions | Variables
importlib.test.benchmark Namespace Reference

Functions

def bench
def from_cache
def builtin_mod
def source_wo_bytecode
def decimal_wo_bytecode
def source_writing_bytecode
def decimal_writing_bytecode
def source_using_bytecode
def decimal_using_bytecode
def main

Variables

tuple parser = optparse.OptionParser()
string default = "use the built-in __import__"
 import_ = __import__

Detailed Description

Benchmark some basic import use-cases.

The assumption is made that this benchmark is run in a fresh interpreter and
thus has no external changes made to import-related attributes in sys.

Function Documentation

def importlib.test.benchmark.bench (   name,
  cleanup = lambda: None,
  seconds = 1,
  repeat = 3 
)
Bench the given statement as many times as necessary until total
executions take one second.

Definition at line 18 of file benchmark.py.

00018 
00019 def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3):
00020     """Bench the given statement as many times as necessary until total
00021     executions take one second."""
00022     stmt = "__import__({!r})".format(name)
00023     timer = timeit.Timer(stmt)
00024     for x in range(repeat):
00025         total_time = 0
00026         count = 0
00027         while total_time < seconds:
00028             try:
00029                 total_time += timer.timeit(1)
00030             finally:
00031                 cleanup()
00032             count += 1
00033         else:
00034             # One execution too far
00035             if total_time > seconds:
00036                 count -= 1
00037         yield count // seconds

Here is the caller graph for this function:

def importlib.test.benchmark.builtin_mod (   seconds,
  repeat 
)
Built-in module

Definition at line 50 of file benchmark.py.

00050 
00051 def builtin_mod(seconds, repeat):
00052     """Built-in module"""
00053     name = 'errno'
00054     if name in sys.modules:
00055         del sys.modules[name]
00056     # Relying on built-in importer being implicit.
00057     for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
00058                         seconds=seconds):
00059         yield result
00060 

Here is the call graph for this function:

def importlib.test.benchmark.decimal_using_bytecode (   seconds,
  repeat 
)
Bytecode w/ source: decimal

Definition at line 126 of file benchmark.py.

00126 
00127 def decimal_using_bytecode(seconds, repeat):
00128     """Bytecode w/ source: decimal"""
00129     name = 'decimal'
00130     py_compile.compile(decimal.__file__)
00131     for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
00132                         seconds=seconds):
00133         yield result
00134 

Here is the call graph for this function:

def importlib.test.benchmark.decimal_wo_bytecode (   seconds,
  repeat 
)
Source w/o bytecode: decimal

Definition at line 76 of file benchmark.py.

00076 
00077 def decimal_wo_bytecode(seconds, repeat):
00078     """Source w/o bytecode: decimal"""
00079     name = 'decimal'
00080     decimal_bytecode = imp.cache_from_source(decimal.__file__)
00081     if os.path.exists(decimal_bytecode):
00082         os.unlink(decimal_bytecode)
00083     sys.dont_write_bytecode = True
00084     try:
00085         for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
00086                             seconds=seconds):
00087             yield result
00088     finally:
00089         sys.dont_write_bytecode = False
00090 

Here is the call graph for this function:

def importlib.test.benchmark.decimal_writing_bytecode (   seconds,
  repeat 
)
Source writing bytecode: decimal

Definition at line 104 of file benchmark.py.

00104 
00105 def decimal_writing_bytecode(seconds, repeat):
00106     """Source writing bytecode: decimal"""
00107     assert not sys.dont_write_bytecode
00108     name = 'decimal'
00109     def cleanup():
00110         sys.modules.pop(name)
00111         os.unlink(imp.cache_from_source(decimal.__file__))
00112     for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
00113         yield result
00114 

Here is the call graph for this function:

def importlib.test.benchmark.from_cache (   seconds,
  repeat 
)
sys.modules

Definition at line 38 of file benchmark.py.

00038 
00039 def from_cache(seconds, repeat):
00040     """sys.modules"""
00041     name = '<benchmark import>'
00042     module = imp.new_module(name)
00043     module.__file__ = '<test>'
00044     module.__package__ = ''
00045     with util.uncache(name):
00046         sys.modules[name] = module
00047         for result in bench(name, repeat=repeat, seconds=seconds):
00048             yield result
00049 

Here is the call graph for this function:

def importlib.test.benchmark.main (   import_)

Definition at line 135 of file benchmark.py.

00135 
00136 def main(import_):
00137     __builtins__.__import__ = import_
00138     benchmarks = (from_cache, builtin_mod,
00139                   source_using_bytecode, source_wo_bytecode,
00140                   source_writing_bytecode,
00141                   decimal_using_bytecode, decimal_writing_bytecode,
00142                   decimal_wo_bytecode,)
00143     seconds = 1
00144     seconds_plural = 's' if seconds > 1 else ''
00145     repeat = 3
00146     header = "Measuring imports/second over {} second{}, best out of {}\n"
00147     print(header.format(seconds, seconds_plural, repeat))
00148     for benchmark in benchmarks:
00149         print(benchmark.__doc__, "[", end=' ')
00150         sys.stdout.flush()
00151         results = []
00152         for result in benchmark(seconds=seconds, repeat=repeat):
00153             results.append(result)
00154             print(result, end=' ')
00155             sys.stdout.flush()
00156         assert not sys.dont_write_bytecode
00157         print("]", "best is", format(max(results), ',d'))
00158 

def importlib.test.benchmark.source_using_bytecode (   seconds,
  repeat 
)
Bytecode w/ source: simple

Definition at line 115 of file benchmark.py.

00115 
00116 def source_using_bytecode(seconds, repeat):
00117     """Bytecode w/ source: simple"""
00118     name = '__importlib_test_benchmark__'
00119     with source_util.create_modules(name) as mapping:
00120         py_compile.compile(mapping[name])
00121         assert os.path.exists(imp.cache_from_source(mapping[name]))
00122         for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
00123                             seconds=seconds):
00124             yield result
00125 

Here is the call graph for this function:

def importlib.test.benchmark.source_wo_bytecode (   seconds,
  repeat 
)
Source w/o bytecode: simple

Definition at line 61 of file benchmark.py.

00061 
00062 def source_wo_bytecode(seconds, repeat):
00063     """Source w/o bytecode: simple"""
00064     sys.dont_write_bytecode = True
00065     try:
00066         name = '__importlib_test_benchmark__'
00067         # Clears out sys.modules and puts an entry at the front of sys.path.
00068         with source_util.create_modules(name) as mapping:
00069             assert not os.path.exists(imp.cache_from_source(mapping[name]))
00070             for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
00071                                 seconds=seconds):
00072                 yield result
00073     finally:
00074         sys.dont_write_bytecode = False
00075 

Here is the call graph for this function:

def importlib.test.benchmark.source_writing_bytecode (   seconds,
  repeat 
)
Source writing bytecode: simple

Definition at line 91 of file benchmark.py.

00091 
00092 def source_writing_bytecode(seconds, repeat):
00093     """Source writing bytecode: simple"""
00094     assert not sys.dont_write_bytecode
00095     name = '__importlib_test_benchmark__'
00096     with source_util.create_modules(name) as mapping:
00097         def cleanup():
00098             sys.modules.pop(name)
00099             os.unlink(imp.cache_from_source(mapping[name]))
00100         for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
00101             assert not os.path.exists(imp.cache_from_source(mapping[name]))
00102             yield result
00103 

Here is the call graph for this function:


Variable Documentation

string importlib.test.benchmark.default = "use the built-in __import__"

Definition at line 164 of file benchmark.py.

Definition at line 168 of file benchmark.py.

Definition at line 162 of file benchmark.py.