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. 使用ServiceStack缓存技术

    ServiceStack 是一个高性能的 .NET Web 服务框架,简化了开发 XML.JSON.JSV 和 WCP SOAP Web 服务.它定义了符合 Martin Fowlers 数据传输对象 ...

  2. 总结! http post请求 application/x-www-form-urlencoded body体数据获取不到?

    首先,简单介绍下Http请求中Content-Type类型 类型格式:type/subtype(;parameter)? type 主类型,任意的字符串,如text,如果是*号代表所有: subtyp ...

  3. 【vim使用】

    nano,与vim相似的一个文本编辑工具,在git merge时默认使用 https://www.vpser.net/manage/nano.html 这里介绍一下如何退出nano 按Ctrl+X 如 ...

  4. 【我的Android进阶之旅】Android Studio如何轻松整理字符串到string.xml中

    使用Android Studio一段时间了,还有很多小技巧没有掌握.比如:平常将字符串整理到string.xml中,都是手动的去复制字符串到string.xml中,然后再回来修改引用该字符串的代码,这 ...

  5. Python高级教程-高阶函数

    Higher-order function(高阶函数) 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下代码: >>> abs(-10) 10 但 ...

  6. SSD(Single Shot MultiBox Detector)二读paper

    SSD KeyWords:Real-time Object Detection; Convolutional Neural Network Introduction 目前最尖端(State-of-ar ...

  7. spring-知识小结之注解为属性赋值

    <1>.本类中的属性赋值 public class UserServiceImpl implements UserService { //按照类别赋值 // @Autowired //按照 ...

  8. 003-mysql查询表的数据大小

    在需要备份数据库里面的数据时,我们需要知道数据库占用了多少磁盘大小,可以通过一些sql语句查询到整个数据库的容量,也可以单独查看表所占容量. 1.查看数据库表结构大小,要查询表所占的容量,就是把表的数 ...

  9. Jquery 实现跨域处理

    JS部分代码: $.ajax({ url:url, dataType:'jsonp', data:{title:title}, jsonp:'callback', success:function(l ...

  10. Tornado的基本知识

    Tornado是FriendReed使用的可扩展的非阻塞式的web服务器及其相关工具的开源版本. 这个框架看起来有些像web.py或者Google的webapp,不过为了能有效利用非阻塞服务器环境,这 ...