Python timeit 模块,template() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用timeit.template()

项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def __init__(self, stmt, setup='pass', timer=timeit.default_timer, globals=globals()):
        # copy of timeit.Timer.__init__
        # similarity index 95%
        self.timer = timer
        stmt = timeit.reindent(stmt, 8)
        setup = timeit.reindent(setup, 4)
        src = timeit.template % {'stmt': stmt, 'setup': setup}
        self.src = src  # Save for traceback display
        code = compile(src, timeit.dummy_src_name, "exec")
        ns = {}
        #exec code in globals(), ns      -- original timeit code
        exec_(code, globals, ns)  # -- we use caller-provided globals instead
        self.inner = ns["inner"]
项目:Python-iBeacon-Scan    作者:NikNitro    | 项目源码 | 文件源码
def __init__(self, stmt, setup='pass', timer=timeit.default_timer, globals=globals()):
        # copy of timeit.Timer.__init__
        # similarity index 95%
        self.timer = timer
        stmt = timeit.reindent(stmt, 8)
        setup = timeit.reindent(setup, 4)
        src = timeit.template % {'stmt': stmt, 'setup': setup}
        self.src = src  # Save for traceback display
        code = compile(src, timeit.dummy_src_name, "exec")
        ns = {}
        #exec code in globals(), ns      -- original timeit code
        exec_(code, globals, ns)  # -- we use caller-provided globals instead
        self.inner = ns["inner"]