让你Python到很爽的加速递归函数的装饰器


Posted in Python onMay 26, 2019

今天我们会讲到一个[装饰器]

注记:链接“装饰器”指Python3教程中的装饰器教程。可以在这里快速了解什么是装饰器。

@functools.lru_cache——进行函数执行结果备忘,显著提升递归函数执行时间。

示例:寻找宝藏。在一个嵌套元组tuple或列表list中寻找元素'Gold Coin'

import time
from functools import lru_cache
def find_treasure(box):
 for item in box:
  if isinstance(item, (tuple, list)):
   find_treasure(item)
  elif item == 'Gold Coin':
   print('Find the treasure!')
   return True
start = time.perf_counter()
find_treasure(('sth', 'sth', 'sth',
    ('Bad Coin', 'normal coin', 'fish', 'sth', 'any sth'),
    ('Bad Coin', 'normal coin', 'fish', 'sth', 'any sth'),
    'Gold Coin', ))
end = time.perf_counter()
run_time_without_cache = end - start
print('在没有Cache的情况下,运行花费了{} s。'.format(run_time_without_cache))
@lru_cache()
def find_treasure_quickly(box):
 for item in box:
  if isinstance(item, (tuple, list)):
   find_treasure(item)
  elif item == 'Gold Coin':
   print('Find the treasure!')
   return True
start = time.perf_counter()
find_treasure_quickly(('sth', 'sth', 'sth',
      ('Bad Coin', 'normal coin', 'fish', 'sth', 'any sth'),
      ('Bad Coin', 'normal coin', 'fish', 'sth', 'any sth'),
      'Gold Coin', ))
end = time.perf_counter()
run_time_with_cache = end - start
print('在有Cache的情况下,运行花费了{} s。'.format(run_time_with_cache))
print('有Cache比没Cache快{} s。'.format(float(run_time_without_cache-run_time_with_cache)))

最终输出

Find the treasure!
在没有Cache的情况下,运行花费了0.0002182829999810565 s。
Find the treasure!
在有Cache的情况下,运行花费了0.00011638000000857573 s。
有Cache比没Cache快0.00010190299997248076 s。

注记:运行这个示例时我的电脑配置如下

CPU:AMD Ryzen 5 2600
RAM:Kingston HyperX 8Gigabytes 2666

约使用7个月。

这个装饰器可以在函数运行时记录它的输入值与运行结果。当元组('Bad Coin', 'normal coin', 'fish', 'sth', 'any sth')出现第二次时,加了这个装饰器的函数find_the_treasure_quickly不会再次在递归时对这个元组进行查找,而是直接在“备忘录”中找到运行结果并返回!

总结

以上所述是小编给大家介绍的让你Python到很爽的加速递归函数的装饰器,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
用Python将IP地址在整型和字符串之间轻松转换
Mar 22 Python
Numpy中转置transpose、T和swapaxes的实例讲解
Apr 17 Python
儿童编程python入门
May 08 Python
Python restful框架接口开发实现
Apr 13 Python
python使用自定义钉钉机器人的示例代码
Jun 24 Python
在django中实现choices字段获取对应字段值
Jul 12 Python
五种Python转义表示法
Nov 27 Python
详解python第三方库的安装、PyInstaller库、random库
Mar 03 Python
Python数据可视化之用Matplotlib绘制常用图形
Jun 03 Python
anaconda python3.8安装后降级
Jun 11 Python
python实现简易自习室座位预约系统
Jun 30 Python
Python+tkinter实现高清图片保存
Mar 13 Python
Django框架模板的使用方法示例
May 25 #Python
Django框架搭建的简易图书信息网站案例
May 25 #Python
Django框架实现的分页demo示例
May 25 #Python
Flask框架工厂函数用法实例分析
May 25 #Python
Python中Numpy mat的使用详解
May 24 #Python
Python中Numpy ndarray的使用详解
May 24 #Python
numpy数组之存取文件的实现示例
May 24 #Python
You might like
adodb与adodb_lite之比较
2006/12/31 PHP
php5.2.0内存管理改进
2007/01/22 PHP
PHP strtotime函数详解
2009/12/18 PHP
深入extjs与php参数交互的详解
2013/06/25 PHP
PHP去掉json字符串中的反斜杠\及去掉双引号前的反斜杠
2015/09/30 PHP
PHP编写RESTful接口
2016/02/23 PHP
php封装的表单验证类完整实例
2016/10/19 PHP
Jquery之Ajax运用 学习运用篇
2011/09/26 Javascript
jQuery获取字符串中出现最多的数
2016/02/22 Javascript
Javascript获取background属性中url的值
2016/10/17 Javascript
Bootstrap 3 进度条的实现
2017/02/22 Javascript
微信小程序中做用户登录与登录态维护的实现详解
2017/05/17 Javascript
nodejs开发微信小程序实现密码加密
2017/07/11 NodeJs
Angular+Bootstrap+Spring Boot实现分页功能实例代码
2017/07/21 Javascript
vue 自定义组件 v-model双向绑定、 父子组件同步通信的多种写法
2017/11/27 Javascript
JavaScript创建对象方法实例小结
2018/09/03 Javascript
vue-router实现编程式导航的代码实例
2019/01/19 Javascript
新手快速上手webpack4打包工具的使用详解
2019/01/28 Javascript
微信小程序监听用户登录事件的实现方法
2019/11/11 Javascript
JS实现网页端猜数字小游戏
2020/03/06 Javascript
JavaScript中条件语句的优化技巧总结
2020/12/04 Javascript
Python单链表的简单实现方法
2014/09/23 Python
Django中cookie的基本使用方法示例
2018/02/03 Python
python3编写ThinkPHP命令执行Getshell的方法
2019/02/26 Python
深入浅析Python科学计算库Scipy及安装步骤
2019/10/12 Python
Python进阶之迭代器与迭代器切片教程
2020/01/29 Python
Django单元测试中Fixtures的使用方法
2020/02/26 Python
科颜氏加拿大官方网站: Kiehl’s加拿大
2016/08/16 全球购物
为您搜罗全球潮流時尚品牌:HBX
2019/12/04 全球购物
文秘自荐信
2013/10/20 职场文书
给国外客户的邀请函
2014/01/30 职场文书
司法局火灾防控方案
2014/06/05 职场文书
复兴之路观后感
2015/06/02 职场文书
于丹讲座视频观后感
2015/06/15 职场文书
人与自然的观后感
2015/06/18 职场文书
Python Socket编程详解
2021/04/25 Python