66"""
77from . import util
88from .source import util as source_util
9+ import decimal
910import imp
1011import importlib
1112import os
@@ -58,7 +59,7 @@ def builtin_mod(seconds, repeat):
5859
5960
6061def source_wo_bytecode (seconds , repeat ):
61- """Source w/o bytecode"""
62+ """Source w/o bytecode: simple """
6263 sys .dont_write_bytecode = True
6364 try :
6465 name = '__importlib_test_benchmark__'
@@ -72,8 +73,23 @@ def source_wo_bytecode(seconds, repeat):
7273 sys .dont_write_bytecode = False
7374
7475
76+ def decimal_wo_bytecode (seconds , repeat ):
77+ """Source w/o bytecode: decimal"""
78+ name = 'decimal'
79+ decimal_bytecode = imp .cache_from_source (decimal .__file__ )
80+ if os .path .exists (decimal_bytecode ):
81+ os .unlink (decimal_bytecode )
82+ sys .dont_write_bytecode = True
83+ try :
84+ for result in bench (name , lambda : sys .modules .pop (name ), repeat = repeat ,
85+ seconds = seconds ):
86+ yield result
87+ finally :
88+ sys .dont_write_bytecode = False
89+
90+
7591def source_writing_bytecode (seconds , repeat ):
76- """Source writing bytecode"""
92+ """Source writing bytecode: simple """
7793 assert not sys .dont_write_bytecode
7894 name = '__importlib_test_benchmark__'
7995 with source_util .create_modules (name ) as mapping :
@@ -85,8 +101,19 @@ def cleanup():
85101 yield result
86102
87103
104+ def decimal_writing_bytecode (seconds , repeat ):
105+ """Source writing bytecode: decimal"""
106+ assert not sys .dont_write_bytecode
107+ name = 'decimal'
108+ def cleanup ():
109+ sys .modules .pop (name )
110+ os .unlink (imp .cache_from_source (decimal .__file__ ))
111+ for result in bench (name , cleanup , repeat = repeat , seconds = seconds ):
112+ yield result
113+
114+
88115def source_using_bytecode (seconds , repeat ):
89- """Bytecode w/ source"""
116+ """Bytecode w/ source: simple """
90117 name = '__importlib_test_benchmark__'
91118 with source_util .create_modules (name ) as mapping :
92119 py_compile .compile (mapping [name ])
@@ -96,16 +123,32 @@ def source_using_bytecode(seconds, repeat):
96123 yield result
97124
98125
126+ def decimal_using_bytecode (seconds , repeat ):
127+ """Bytecode w/ source: decimal"""
128+ name = 'decimal'
129+ py_compile .compile (decimal .__file__ )
130+ for result in bench (name , lambda : sys .modules .pop (name ), repeat = repeat ,
131+ seconds = seconds ):
132+ yield result
133+
134+
99135def main (import_ ):
100136 __builtins__ .__import__ = import_
101- benchmarks = (from_cache , builtin_mod , source_using_bytecode ,
102- source_wo_bytecode , source_writing_bytecode ,)
103- print ("Measuring imports/second\n " )
137+ benchmarks = (from_cache , builtin_mod ,
138+ source_using_bytecode , source_wo_bytecode ,
139+ source_writing_bytecode ,
140+ decimal_using_bytecode , decimal_writing_bytecode ,
141+ decimal_wo_bytecode ,)
142+ seconds = 1
143+ seconds_plural = 's' if seconds > 1 else ''
144+ repeat = 3
145+ header = "Measuring imports/second over {} second{}, best out of {}\n "
146+ print (header .format (seconds , seconds_plural , repeat ))
104147 for benchmark in benchmarks :
105148 print (benchmark .__doc__ , "[" , end = ' ' )
106149 sys .stdout .flush ()
107150 results = []
108- for result in benchmark (seconds = 1 , repeat = 3 ):
151+ for result in benchmark (seconds = seconds , repeat = repeat ):
109152 results .append (result )
110153 print (result , end = ' ' )
111154 sys .stdout .flush ()
0 commit comments