让你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实现二维有序数组查找的方法
Apr 27 Python
ubuntu中配置pyqt4环境教程
Dec 27 Python
python安装pywin32clipboard的操作方法
Jan 24 Python
解决python3中的requests解析中文页面出现乱码问题
Apr 19 Python
Python实用库 PrettyTable 学习笔记
Aug 06 Python
python3实现绘制二维点图
Dec 04 Python
python获取网络图片方法及整理过程详解
Dec 20 Python
python3 使用traceback定位异常实例
Mar 09 Python
Python任务调度利器之APScheduler详解
Apr 02 Python
Python字符串格式化f-string多种功能实现
May 07 Python
Python字符串对齐、删除字符串不需要的内容以及格式化打印字符
Jan 23 Python
python Polars库的使用简介
Apr 21 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
php相当简单的分页类
2008/10/02 PHP
深入php中var_dump方法的使用详解
2013/06/24 PHP
解析获取优酷视频真实下载地址的PHP源代码
2013/06/26 PHP
php实现快速排序的三种方法分享
2014/03/12 PHP
php实现图片上传并进行替换操作
2016/03/15 PHP
基于jQuery制作迷你背词汇工具
2010/07/27 Javascript
动态创建script在IE中缓存js文件时导致编码的解决方法
2014/05/04 Javascript
基于Vue.js实现简单搜索框
2020/03/26 Javascript
javascript显示系统当前时间代码
2016/12/29 Javascript
Node.js 使用request模块下载文件的实例
2018/09/05 Javascript
Vue路由history模式解决404问题的几种方法
2018/09/29 Javascript
vue实现吸顶、锚点和滚动高亮按钮效果
2019/10/21 Javascript
vue.js实现h5机器人聊天(测试版)
2020/07/16 Javascript
[01:03:27]Optic vs VGJ.S 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
浅析Python中的join()方法的使用
2015/05/19 Python
Python中pygame安装方法图文详解
2015/11/11 Python
基于Python闭包及其作用域详解
2017/08/28 Python
PyQt5每天必学之创建窗口居中效果
2018/04/19 Python
Python动态生成多维数组的方法示例
2018/08/09 Python
pyshp创建shp点文件的方法
2018/12/31 Python
python里运用私有属性和方法总结
2019/07/08 Python
简单了解python反射机制的一些知识
2019/07/13 Python
python递归法解决棋盘分割问题
2019/07/17 Python
Python中变量的输入输出实例代码详解
2019/07/28 Python
Python发送邮件实现基础解析
2020/08/14 Python
Python实现爬取网页中动态加载的数据
2020/08/17 Python
Oroton中国官网:澳洲知名奢侈配饰品牌
2017/03/26 全球购物
理肤泉英国官网:La Roche-Posay英国
2019/01/14 全球购物
介绍一下OSI七层模型
2012/07/03 面试题
经销商会议欢迎词
2014/01/11 职场文书
幼儿园大班毕业感言
2014/02/06 职场文书
合作协议书
2014/04/23 职场文书
2016大学自主招生推荐信范文
2015/03/23 职场文书
法人代表资格证明书
2015/06/18 职场文书
导游词之广西漓江
2019/11/02 职场文书
php 获取音视频时长,PHP 利用getid3 获取音频文件时长等数据
2021/04/01 PHP