python测试函数的使用时间
1. 使用装饰器来衡量函数执行时间
有一个简单方法,那就是定义一个装饰器来测量函数的执行时间,并输出结果:(代码通用3.x)
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("Total time running %s: %s seconds" %
(function.__name__, str(t1-t0))
)
return result
return function_timer
要测试函数的使用时间时,只需要@fn_timer装饰器即可。
@fn_timer
def myfunction(...):
...
下面是测试:
In [14]: @fn_timer
...: def norm(a):
...: return sum(a**2)**(1/2)
...: In [15]: @fn_timer
...: def norm2(a):
...: return la.norm(a)
...: In [16]: norm(a)
Total time running norm: 0.0 seconds
Out[16]: 4.7958315233127191 In [17]: norm2(a)
Total time running norm2: 0.0 seconds
Out[17]: 4.7958315233127191 In [18]: a = np.random.randint(-3,3,(10000,)) In [19]: norm(a)
Total time running norm: 0.0010035037994384766 seconds
Out[19]: 177.92695130305583 In [20]: norm2(a)
Total time running norm2: 0.001010894775390625 seconds
Out[20]: 177.92695130305583 In [21]: a = np.random.randint(-3,3,(50000,)) In [22]: norm(a)
Total time running norm: 0.005008220672607422 seconds
Out[22]: 397.39275282772837 In [23]: norm2(a)
Total time running norm2: 0.0 seconds
Out[23]: 397.39275282772837
python测试函数的使用时间的更多相关文章
- Python测试函数的方法之一
Python测试函数的方法之一 首先介绍简单的try......except尝试运行的放例如下面的图和代码来简单介绍下: 注释:提醒以下代码环境为2.7.x 请3.x以上的同学们老规矩print(把打 ...
- python 的经常使用时间操作,取得当前时间等
我们先导入必须用到的一个module>>> import time设置一个时间的格式,以下会用到>>>ISOTIMEFORMAT=’%Y-%m-%d %X’看一下当 ...
- Python测试函数运行时间
import time import datetime # 测试函数运行时间 def cal_time(fn): """计算性能的修饰器""" ...
- Python: 测试函数是否被调用
# helper class defined elsewhere class CallLogger(object): def __init__(self, meth): self.meth = met ...
- python测量函数运行时间长度
python测试函数运行时间长度的方法如下 import time def measure_time(): def wraps(func): def mesure(*args,**kwargs): s ...
- python测试框架-pytest
一.pytest 介绍.运行.参数化和数据驱动.Fixture pytest安装与介绍 官网 : pip install -U pytest 查看版本号:pytest --version 为何选择py ...
- Pytest权威教程21-API参考-05-对象(Objects)
目录 对象(Objects) CallInfo Class Collector Config ExceptionInfo FixtureDef FSCollector Function Item Ma ...
- python模板:自动化执行测试函数
#!/bin/python #example 1.1 #applay def function(a,b): print(a,b) def example1(): apply(function, (&q ...
- python从入门到实践-11章测试模块(测试函数出问题)
#!/user/bin/env python# -*- coding:utf-8 -*- # 用python中unittes中工具来测试代码 # 1.测试函数import unittestfrom n ...
随机推荐
- Web移动端使用localStorage缓存Js和css文件 | 强制不要缓存
1.尽量不把js放在onload事件中,而是放在由用户主动触发的事件 2.加时间戳,时间不同则会加载js而非使用缓存 强制不要缓存: <meta http-equiv=Cache-Control ...
- CodeForeces 665C Simple Strings
C. Simple Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- poco库 RSA加解密
#include "poco/Crypto/Cipher.h"#include "poco/Crypto/CipherFactory.h"#include &q ...
- Linux环境Oracle相关操作命令行
打开Oracle监听:lsnrctl start 关闭监听: lsnrctl stop 监听关闭那么客户端无法连接 进入sqlplus:sqlplus /nolog 使 ...
- JS操作时间 - UNIX时间戳简单介绍
准备知识 GMT(Greenwich Mean Time) - 格林尼治时间.UTC(Universal Time Code) - 国际协调时间.PST(Pacific Standard Time,太 ...
- CSS 之怀疑自己的审美 2 (Day50)
阅读目录 伪类 选择器的优先级 css 属性操作 一.伪类 anchor伪类:专用于控制链接的显示效果 ''' a:link(没有接触过的链接),用于定义了链接的常规状态. a:hover(鼠标放在链 ...
- 启动Django服务让其他电脑可访问
1.修改 Django项目中的settings.py中的 ALLOWED_HOSTS 的值为 [*] # 准许那些地址访问,* 表示任意地址 ALLOWED_HOSTS = ['*'] # ALLOW ...
- ArcGIS COM Exception 0x80040228
问题: string shpDir = Path.GetDirectoryName(shpfile); string shpfilename = Path.GetFileNa ...
- iptables打开22,80,8080,3306等端口
systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services package: yum i ...
- Levenshtein距离
Levenshtein Distance,又称Edit Distance,在自然语言处理中有着广泛的应用.Levenshtein Distance 指的是两个字符串之间,由一个转换成另一个所需的最少 ...