how to pass parameters of a function when using timeit.Timer()

前端 未结 12 1786
礼貌的吻别
礼貌的吻别 2020-12-02 10:19

This is the outline of a simple program

# some pre-defined constants
A = 1
B = 2

# function that does something critical
def foo(num1, num2):
    # do somet         


        
12条回答
  •  抹茶落季
    2020-12-02 10:51

    I prefer creating a static class with all the Data ready to be picked up prior of running the timer.

    Another note, it is better to do test runs in function rather then in the global space, as the global space isn't taking advantage of FAST_LOAD Why does Python code run faster in a function?

    class Data(object):
        """Data Creation"""
        x = [i for i in range(0, 10000)]
        y = tuple([i for i in range(0, 10000)])
        def __init__(self):
            pass
    
    import timeit
    
    def testIterator(x):
        for i in range(10000):
            z = i
    
    
    print timeit.timeit("testIterator(Data.x)", setup="from __main__ import testIterator, Data", number=50)
    print timeit.timeit("testIterator(Data.y)", setup="from __main__ import testIterator, Data", number=50)
    

提交回复
热议问题