$python用装饰器实现一个计时器
直接上代码:
import time
from functools import wraps
# 定义装饰器
def fn_timer(function):
@wraps(function)
def function_timer(*args,**kwargs):
t0 = time.time()
result = function(*args,**kwargs)
t1 = time.time()
print '[finished {func_name} in {time:.2f}s]'.format(func_name = function.__name__,time = t1 - t0)
return result
return function_timer
# 使用装饰器来计时
@fn_timer
def download(url):
# 模拟下载3秒
print 'start to download from {0}...'.format(url)
time.sleep(3)
print 'download finished!'
download('www.baidu.com')
start to download from www.baidu.com...
download finished!
[finished download in 3.00s]
随机推荐
- Linux下的高级拾色器—Pick
导读 虽然大多数设计师都在使用 Mac,但也有一少部分在使用 Windows 甚至是 Linux 系统.在 Mac 和 Windows 中都有非常丰富的拾色器工具或插件可用,反而在开源界中这类颜色选择 ...
- python 自己定义异常
通过创建一个新的异常类,就可以命名自己的异常,异常应该是典型的继承自Exception类 例如: # 定义了一个自己的异常类,可在适当时候通过raise来触发它class ExError(Except ...
- Spring MVC静态资源访问
最近在学习servlet的时候发现自己不能访问到css和js, 于是google一番学到不少方法加载,总结如下: 1.对于Spring MVC, 由于我们截获了所有请求<url-pattern& ...
- SAS9.4安装
安装教程请查看博客https://blog.csdn.net/qq_38960682/article/details/80567686 启动SAS时就报下面的错了:WARNING: 连接逻辑库“SAS ...
- c#系统中类的方法 Console、Object,ToolStripDropDownItem,string
一.Console 1.System 命名空间中的 Console 类提供了一个函数 ReadLine(),用于接收来自用户的输入,并把它存储到一个变量中. int num; num = Conver ...
- Servlet------>jsp自定义标签5(标签体内容改为大写)
5.把标签体内容改为大写(tld中的配置我就省略了,详细请看jsp自定义标签1) import java.io.IOException; import javax.servlet.jsp.JspExc ...
- Exception in thread "main" java.lang.IllegalArgumentException: System memory 202768384 must be at least 4.718592E8. Please use a larger heap size.
Spark-submit 提交任务时候报错 Exception in thread "main" java.lang.IllegalArgumentException: Syste ...
- window.location下的属性说明
属性 说明 window.location.href 完整的url window.location.protocol 协议 window.location.hostname 主机名 window.lo ...
- golang: multiple http.writeHeader calls
背景: golang的http服务,读取文件,提供给client下载时候. 出现 multiple http.writeHeader calls 错误. func DownloadFile(w htt ...
- LInux中ThreadInfo中的preempt_count字段
最近看各种上下文,发现和ThreadInfo中的preemption字段密切,于是便调查了下. 看下Linux源码中的注释: /* * We put the hardirq and softirq c ...