让你Python到很爽的加速递归函数的装饰器
作者:咖京学习 发布时间:2022-10-23 13:04:47
标签:python,递归函数,装饰器
今天我们会讲到一个[装饰器]
注记:链接“装饰器”指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到很爽的加速递归函数的装饰器网站的支持!
来源:https://www.jianshu.com/p/66e1fe314154
0
投稿
猜你喜欢
- 当子类继承父类后,需要调用父类的方法和属性时,需要调用父类的初始化函数。class A(object): def __init_
- TO_NUMBER(char[,'format_model']) 字符转换到数字类型TO_DATE(char[,'f
- 数据准备import numpy as npimport pandas as pdimport jsonimport psycopg2dat
- 前言本篇来学习pytest中内置fixture中临时目录的使用tmpdirtmpdir作用范围是函数级别,创建临时文件供单个测试点调用# -
- ctrl+Enter:重建ctrl+0:相当于点击当前行左方的加号或减号ctrl+E:打开新窗口预览ctrl+T:替换\t为两个空格tab:
- python中with可以明显改进代码友好度,比如:with open('a.txt') as f:  
- 1.python中列表list的拷贝,会有什么需要注意的呢? python变量名相当于标签名。list2=list1 ,直接赋值,实质上指向
- Python自身作为一门编程语言,它有多种实现。这里的实现指的是符合Python语言规范的Python解释程序以及标准库等。这些实现虽然实现
- 免费手机号码归属地API查询接口和PHP使用实例分享最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点
- 什么是Inception ResnetV2Inception ResnetV2是Inception ResnetV1的一个加强版,两者的结构
- 编解码器在字符与字节之间的转换过程称为编解码,Python自带了超过100种编解码器,比如:ascii(英文体系)gb2312(中文体系)u
- 这几天忙个合作,开发iPhone版校友录。用了很久的iPhone,为它量身定制一套页面还是头一次,好在限制不是很多,会写html基本呈现就不
- 1.弹启一个全屏窗口 <html> <body onload="win
- def ddns():"""用当前ip更新ddns"""headers = {&
- 代码如下:var obj = document.getElementById("name"
- cli 的全称 command-line interface(命令行界面),也就是前端同学常用的脚手架,比如 yo、vue cli、reac
- 本文实例讲述了Python面向对象程序设计之继承、多态原理与用法。分享给大家供大家参考,具体如下:相关内容:继承:多继承、super、__i
- Python 类Python中的类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以调用
- 我们使用 python setup.py install 来安装python包,但是如何卸载呢?只能手动删除安装的文件可以使用如下命令pyt
- 一、简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装pyt