Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python decorator to calculate the running time of function

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Python decorator to calculate the running time of functions". In daily operation, I believe many people have doubts about how to use Python decorator to calculate the running time of functions. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer the doubts of "how to use Python decorator to calculate the running time of functions"! Next, please follow the small series to learn together!

personal understanding

Decorators: flexible invocation of existing functionality through closures and taking one function as a parameter to another

For example:

First set a timer function of time_master, which is used to count the time consumption of a function while running a function.

So, if you want to know the performance of a function, every time you write a new function, you put it in the time_master function and run it once.

>> This is more troublesome. This is equivalent to weighing all the newly purchased ingredients one by one to obtain the weight of the ingredients.

--> If you can omit this step once and for all, it will be fine.

--> For example, every ingredient passes through the scale at the same time as it enters the door, and at the same time, it also has weight data.

Example: Timing the function while calling the function Implementation method 1:@ Syntax sugar code: #involves timing, you need to introduce the time module import time#Define a timer to count the running time of the function, use func as a formal parameter to replace the function def time_master(func): def call_func(): print("Timer start: function start call: ") start_time = time.time() func() end_time = time.time() print ('Timer ends, function call complete') return print(f'timer result returns: function call time {end_time-start_time:.2f}') return call_func@time_master #That is, when calling myfunc function, instead of directly calling myfunc#, put myfunc function as a parameter into @'s decorator, and then call decorator def myfunc(): time.sleep(2) print ('myfunc function running') time.sleep(4) print ('myfunc function run end') myfunc() #Call myfunc implementation results:

Timer start: function start call:

myfunc function runs

myfunc function ends

Timer ends, function call completes

Timer result returned: function call time 6.01

Implementation Method 2: Closure

It can be understood as, originally defined a myfunc function, but this function itself has no timing function, and happens to have a time_master function, while running the subfunction, it will also time the subfunction.

Therefore, by redefining myfunc = time_master(myfunc), i.e. passing myfunc as an argument into time_master, as a new definition of the myfunc function

Code: import timedef time_master(func): def call_func(): print("Timer start: function start call: ") start_time = time.time() func() end_time = time.time() print ('Timer ends, function call complete') print(f'timer result returns: function call time {end_time-start_time:.2f}') return call_funcdef myfunc(): time.sleep(2) print ('myfunc function running') time.sleep(4) print ('myfunc function ends running') myfunc = time_master(myfunc) #The difference between implementation method 1 and myfunc is whether to define myfunc function twice before @ decorator or after myfunc function myfunc() implementation result:

Timer start: function start call:

myfunc function runs

myfunc function ends

Timer ends, function call completes

Timer result returned: function call time 6.01

Differences between implementation 1 and 2

Implementation 1 and implementation 2: In fact, there is no difference, except that you use @time_master to specify it at the beginning, or after defining myfunc, do one more step and let myfun be put into time_master to execute.

At this point, the study of "how to use Python decorators to calculate the running time of functions" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report