Two functions defined functions:
# -*- coding:utf-8 -*- def foo(): # 打印"foo..." print("foo...") def bar(): # 打印"bar..." print("bar...") foo() # foo... bar() # bar...
Calculated run time function, we can rewrite the function:
Import Time # on the (document) the top of the DEF foo (): Start = time.time () # before the function runtime Print ( " foo ... " ) the time.sleep ( 1) # because the program running time too short, so the use of time.sleep () to extend the time 1s End = time.time () # function runs after the moment Print (End - Start) # time print run of DEF bar (): Start = time.time () # prior to the run-time function Print ( " bar ... " ) the time.sleep ( 1) #Because the program running time is too short, so the use of time.sleep () to extend the time 1s End = time.time () # function runs after the moment Print (End - Start) # time print run of foo () # foo .. . # 1.0006506443023682 bar () # bar ... # 1.0007519721984863
By adding some code to calculate the time of each function within the function, we will be able to achieve our demands, but it appeared the following issues:
1, modify the function of the function itself (each call to the function will print run time, if any, I do not want to print it?).
2, large amount of code, and repeatedly (here only two features function, we can be manually modified if there are dozens, hundreds, it would mean that we have to change to day hand cramp).
Therefore, for the above two cases, and resolve code duplication and does not modify the function itself , we can define a new function to calculate the time :
Import Time DEF foo (): # print "foo ..." Print ( " foo ... " ) DEF bar (): # print "bar ..." Print ( " bar ... " ) DEF caltime (FUNC ): # time calculating function to run Start the time.time = () # prior to run-time function FUNC () the time.sleep ( . 1) # because the program running time is too short, so the use of the time.sleep () extended time 1s end time.time = () # function runs after the moment Print (End - Start) caltime (foo) # foo... # 1.0009233951568604 caltime(bar) # bar... # 1.0008749961853027
We found that to achieve a function by creating a new function, it is convenient, simple. But this way, the function is called has changed, such as:
We are beginning to call function function: foo () . To calculate a function of time now, we call way: caltime (foo) .
We need to reach the demand is: on the final execution foo (), can achieve the function itself functions and time of the function to run the calculation . Instead caltime (foo).
If we project in dozens of calls a function foo, and both need to calculate the time, that we not have to put one by one foo () into caltime (foo) , in fact, this is an annoying thing. So we like a way (that is still written foo () function call, but at the same time can be calculated to run):
import time def foo(): # 打印"foo..." print("foo...") def bar(): # 打印"bar..." print("bar...") def caltime(func): def inner(): start = time.time() func() time.sleep(1) end = time.time() print(end - start) return inner foo = caltime(foo) foo() # foo... # 1.0006935596466064
Finally, we are to call a function by foo () instead caltime (foo). So, we can pass the return value of the function as a transfer, to achieve the ultimate invocation (notation when calling the function) the same.
caltime (foo) the execution result is returned inner function (does not perform inner function), then the assignment to the inner function foo (i.e. foo = caltime (foo)). So, foo () is equivalent to the implementation of inner ().
Caltime here () is a decorator , but we will find that before each execution foo () will write foo = caltime (foo), but also very troublesome, it is more professional decorators is more elegant wording:
1, the decorator caltime () function written in front of the function;
2, the foo = caltime (foo) Each call to write sentence (repeat) code written @caltime manner, to the front performance function.
import time def caltime(func): def inner(): start = time.time() func() time.sleep(1) end = time.time() print(end - start) return inner @caltime # 此时caltime就相当于是foo=caltime(foo) def foo(): # 打印"foo..." print("foo...") def bar(): # 打印"bar..." print("bar...") # foo = caltime(foo) foo() # foo... # 1.000216007232666