The disadvantage of your test is the timeit way timeit compile your stmt code. It is really compiled in the following template:
template = """ def inner(_it, _timer): %(setup)s _t0 = _timer() for _i in _it: %(stmt)s _t1 = _timer() return _t1 - _t0 """
Thus, stmt actually works in a function using the fastlocals array (i.e. STORE_FAST ).
Here's a test with your function in question like f_opt compared to the unoptimized compiled stmt executed in f_no_opt function:
>>> code = compile(stmt, '<string>', 'exec') >>> f_no_opt = types.FunctionType(code, globals()) >>> t_no_opt = min(timeit.repeat(f_no_opt, repeat=10, number=10)) >>> t_opt = min(timeit.repeat(f_opt, repeat=10, number=10)) >>> t_opt / t_no_opt 0.4931101445632647
eryksun
source share