python test program fragment runtime timeit module

python test program snippet run time

import timeit

print timeit.timeit("x = 1")

or:

You can also test by creating a timing instance

def test():
    x = 1
    x += 1


t = timeit.Timer(test)
print t.timeit() # default 1,000,000 times

t = timeit.Timer("x = 1")
print t.timeit()
The above code shows that the timer class can be instantiated with characters, or it can be called with an object with the __call__ attribute, and the function is also an object with the __call__ attribute!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325190858&siteId=291194637