The examples in this paper describe how time-consuming statistical functions run in Python. Share to everyone for your reference. The implementation method is as follows:
Import Timedef Time_me (FN): def _wrapper (*args, **kwargs): start = Time.clock () fn (*args, **kwargs) Print "%s cost%s second"% (fn.__name__, Time.clock ()-start) return _wrapper# This adorner can be a time-consuming way to easily count function runs. #用来分析脚本的性能是最好不过了. #这样用: @time_medef Test (x, y): time.sleep (0.1) @time_medef test2 (x): time.sleep (0.2) test (1, 2) test2 (2) #输出: # Test cost 0.1001529524 second#test2 cost 0.199968431742 Second
Another higher-level version is:
Import Timeimport functoolsdef Time_me (info= "used"): def _time_me (FN): @functools. Wraps (FN) def _ Wrapper (*args, **kwargs): start = Time.clock () fn (*args, **kwargs) print "%s%s%s"% (fn.__name__, info, Time.clock ()-start), "second" return _wrapper return _time_me@time_me () def test (x, y): time.sleep ( 0.1) @time_me ("cost") def test2 (x): time.sleep (0.2) test (1, 2) test2 (2)
Hopefully this article will help you with Python programming.