Getting “global name 'foo' is not defined” with Python's timeit

后端 未结 5 2045
臣服心动
臣服心动 2020-11-28 21:49

I\'m trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called timeit that pur

5条回答
  •  余生分开走
    2020-11-28 22:34

    You can try this hack:

    import timeit
    
    def foo():
        print 'bar'
    
    def dotime():
        t = timeit.Timer("foo()")
        time = t.timeit(1)
        print "took %fs\n" % (time,)
    
    import __builtin__
    __builtin__.__dict__.update(locals())
    
    dotime()
    

提交回复
热议问题