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测试函数的使用时间的更多相关文章

  1. Python测试函数的方法之一

    Python测试函数的方法之一 首先介绍简单的try......except尝试运行的放例如下面的图和代码来简单介绍下: 注释:提醒以下代码环境为2.7.x 请3.x以上的同学们老规矩print(把打 ...

  2. python 的经常使用时间操作,取得当前时间等

    我们先导入必须用到的一个module>>> import time设置一个时间的格式,以下会用到>>>ISOTIMEFORMAT=’%Y-%m-%d %X’看一下当 ...

  3. Python测试函数运行时间

    import time import datetime # 测试函数运行时间 def cal_time(fn): """计算性能的修饰器""" ...

  4. Python: 测试函数是否被调用

    # helper class defined elsewhere class CallLogger(object): def __init__(self, meth): self.meth = met ...

  5. python测量函数运行时间长度

    python测试函数运行时间长度的方法如下 import time def measure_time(): def wraps(func): def mesure(*args,**kwargs): s ...

  6. python测试框架-pytest

    一.pytest 介绍.运行.参数化和数据驱动.Fixture pytest安装与介绍 官网 : pip install -U pytest 查看版本号:pytest --version 为何选择py ...

  7. Pytest权威教程21-API参考-05-对象(Objects)

    目录 对象(Objects) CallInfo Class Collector Config ExceptionInfo FixtureDef FSCollector Function Item Ma ...

  8. python模板:自动化执行测试函数

    #!/bin/python #example 1.1 #applay def function(a,b): print(a,b) def example1(): apply(function, (&q ...

  9. python从入门到实践-11章测试模块(测试函数出问题)

    #!/user/bin/env python# -*- coding:utf-8 -*- # 用python中unittes中工具来测试代码 # 1.测试函数import unittestfrom n ...

随机推荐

  1. HDU 1863 畅通工程(Kruskal)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. CodeForces 667A Pouring Rain

    A. Pouring Rain time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. https://help.aliyun.com/knowledge_detail/49787.html?spm=a2c4g.11186631.2.3.6f856f39tiE98P

    https://help.aliyun.com/knowledge_detail/49787.html?spm=a2c4g.11186631.2.3.6f856f39tiE98P

  4. 解决URL地址中的中文乱码问题的办法

    解决URL地址中的中文乱码问题的办法 引言: 在Restful类的服务设计中,经常会碰到需要在URL地址中使用中文作为的参数的情况,这种情况下,一般都需要正确的设置和编码中文字符信息.乱码问题就此产生 ...

  5. travelsal all files in a dir using recursion shell

    #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] then g ...

  6. cookies设置时间

    默认cookies失效时间是直到关闭浏览器,cookies失效,也可以指定cookies时间. Response.Cookies("user_name").Expires=Date ...

  7. python 2.7.11安装pywin32过程中 停止工作问题

    问题描述 由于需要安装pywin32,官网下载了pywin32-220.win32-py2.7.exe.注意:你的pywin32是32位还是64位取决于你的python,而不是你的电脑.CMD上可以查 ...

  8. java多线程总结(一)

    在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: 1 2 3 4 5 6 7 8 9 10 11 ...

  9. Seaborn相关

    Seaborn:基于Matplotlib,seaborn提供许多功能,比如:内置主题.颜色调色板.函数和提供可视化单变量.双变量.线性回归的工具.其能帮助我们构建复杂的可视化. ————————缩写定 ...

  10. 2016年国内开源maven镜像站点汇总

    本文系转载,原文链接:https://www.cnblogs.com/xunianchong/p/5684042.html 一.站点版 (一).企业站 1.网易:http://mirrors.163. ...