The method of measuring the time code to run python

Python community has a saying: "python with its own battery," Do not write your own timing framework. Python3.2 have called timeit perfect timing tool can measure the running time of python code.

timeit modules:

timeit module defines Timer class accepts two parameters. Both arguments are strings. The first argument is that you should timed statement or function. Timer passed to the second parameter is the import statement for the first environment parameter statement. Internally, timeit build a separate virtual environment, manually executes the setup statement, then manually compiles and executes the timed statement. If you are just learning python / Soon, more confused, it is recommended to small series of Python exchange dress: a long time Wu that while the down stream a thought (digital homonym) conversion can be found, there are new Python Tutorial project may take , do not understand the problem more exchanges with people inside will solve Oh!
Once you have the Timer object, the easiest thing is to call timeit (), which accepts one parameter is the number of the timed statement of each test call, the default is a million times; returns the number of seconds it takes.
Another major method of the Timer object is repeat (), which accepts two optional parameters. The first parameter is the number of repeat the entire test, the second parameter is the number of the timed statement of each test call. Two parameters are optional, their default values are 3 and 1,000,000. repeat () method returns a list of the times per second recorded test cycle. Python has a handy min function can return a list of the minimum value of the input, such as:
min (t.repeat (. 3, 1000000))
You can use timeit line test module in the Python command an existing program, without the need modify the code.
Specific can be found in the document: http://docs.python.org/library/timeit.html
1, program code

print_func_time DEF (function):
'' '
is calculated run time
: param function:
: return:
' ''

@wraps(function)
def func_time(*args, **kwargs):
t0 = time.clock()
result = function(*args, **kwargs)
t1 = time.clock()
print("Total running time: %s s" % (str(t1 - t0)))
return result

return func_time

2, print_func_time

@print_func_time
def test ():
print (123)

Test ()
. 3, the output of the console program runtime

123
Total running Time: 2.233830763170168e S-05
over this technology is to share, if you do not know or just learning python / Soon, you can come to my Python exchange dress: a long time and its military while a stream of thought (digital euphony ) down conversion can be found, and there are new Python tutorial projects can take, do not understand the problem more exchanges with people inside will solve Oh!
Text and images in this article from the network, only to learn, exchange, not for any commercial purposes, belongs to original author, if any questions, please contact us for treatment.

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12084300.html