当前位置: 首页 > news >正文

网站访问流程在线之家

网站访问流程,在线之家,网站视频是什么软件做的,做网站实训心得今天看到一段源码 https://github.com/google-research/google-research/blob/master/instruction_following_eval/instructions_util.py 如下,对其中使用的装饰器函数感到好奇,所以产生了这篇博客。 functools.lru_cache(maxsizeNone) def _get_sentenc…

今天看到一段源码 https://github.com/google-research/google-research/blob/master/instruction_following_eval/instructions_util.py 如下,对其中使用的装饰器函数感到好奇,所以产生了这篇博客。


@functools.lru_cache(maxsize=None)
def _get_sentence_tokenizer():return nltk.data.load("nltk:tokenizers/punkt/english.pickle")

Python中的@functools.lru_cache:用法、来源与应用

在Python中,@装饰器语法的标志,用于将一个函数包装进另一个函数中,扩展或修改其功能。functools.lru_cache是Python标准库functools模块提供的一个装饰器,用于缓存函数的返回值,提升性能。


1. 装饰器的来源与语法

装饰器(Decorators)是Python函数式编程的一个核心特性。其来源可以追溯到Python 2.4版本,主要目的是让代码更简洁、更易读。@符号将装饰器函数与目标函数绑定,简化代码调用。

  • 装饰器定义
    本质上,装饰器是一个接收函数作为参数并返回一个新函数的高阶函数

示例:

def decorator(func):def wrapper():print("装饰器已执行")return func()return wrapper@decorator  # 等价于 func = decorator(func)
def say_hello():print("Hello, World!")

更多关于python装饰器的内容,可以参考笔者的另一篇博客:什么是装饰器?以Python为例:Basic Usage of Decorators (中英双语)


2. functools.lru_cache的作用

functools.lru_cache 是一个用于缓存函数调用结果的装饰器。

  • LRU全称是Least Recently Used,即最近最少使用缓存策略。
  • 当函数被多次调用时,lru_cache会缓存函数的输入和输出,减少重复计算,提升效率。
参数说明
  • maxsize: 设置缓存大小(None表示无限制)。
  • typed: 是否区分输入参数的类型(默认为False)。

示例:

import functools@functools.lru_cache(maxsize=3)
def fibonacci(n):print(f"计算Fibonacci({n})")if n < 2:return nreturn fibonacci(n-1) + fibonacci(n-2)print(fibonacci(5))  # 计算结果缓存,重复调用时不会重新计算

输出示例:

计算Fibonacci(5)
计算Fibonacci(4)
计算Fibonacci(3)
计算Fibonacci(2)
计算Fibonacci(1)
计算Fibonacci(0)
5

3. 代码分析与解释

以下是https://github.com/google-research/google-research/blob/master/instruction_following_eval/instructions_util.py 提供的代码:

import functools
import nltkdef count_words(text):"""Counts the number of words."""tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+")tokens = tokenizer.tokenize(text)num_words = len(tokens)return num_words@functools.lru_cache(maxsize=None)
def _get_sentence_tokenizer():return nltk.data.load("nltk:tokenizers/punkt/english.pickle")
代码解释:
  1. count_words函数

    • 用于统计文本中的单词数量。
    • 通过nltk库中的RegexpTokenizer按正则表达式匹配单词,返回token列表并计算长度。
  2. _get_sentence_tokenizer函数

    • 加载NLTK库的句子分割模型punkt
    • 使用@functools.lru_cache(maxsize=None)装饰,表示缓存该函数的返回值。
    • 好处:加载分词器的操作是耗时的,通过缓存,只需加载一次。

4. functools.lru_cache的应用场景

  • 递归函数:如斐波那契数列,避免重复计算。
  • I/O密集型函数:比如数据库查询、网络请求,缓存结果以减少耗时。
  • 大规模数据处理:缓存部分中间结果,提高性能。
  • 机器学习/自然语言处理:模型加载、分词器初始化等耗时操作。

示例:

@functools.lru_cache(maxsize=128)
def fetch_data_from_db(query):# 模拟数据库查询print("执行查询:", query)return f"结果_{query}"print(fetch_data_from_db("SELECT * FROM table"))
print(fetch_data_from_db("SELECT * FROM table"))  # 直接返回缓存结果

输出:

执行查询: SELECT * FROM table
结果_SELECT * FROM table
结果_SELECT * FROM table

5. 补充内容:查看缓存信息

functools.lru_cache 提供了缓存管理的方法:

  • cache_info():查看缓存命中率和状态。
  • cache_clear():清空缓存。

示例:

@functools.lru_cache(maxsize=2)
def add(a, b):return a + badd(1, 2)
add(3, 4)
add(5, 6)print(add.cache_info())  # 输出缓存状态
add.cache_clear()        # 清除缓存

总结

  1. 装饰器的语法@用来将装饰器与函数绑定。
  2. lru_cache作用:缓存函数结果,提升性能,避免重复计算。
  3. 实际应用:适用于递归计算、I/O密集型操作和数据处理任务。
  4. 代码示例:通过数值分析,展示了lru_cache的高效性。

通过合理使用functools.lru_cache,Python程序可以在性能瓶颈处得到极大提升。

英文版

Exploring @functools.lru_cache in Python

The @ symbol in Python is used for decorators, which allow you to modify or enhance the behavior of a function or method. One such decorator is functools.lru_cache, which caches a function’s return values for optimized performance, especially in repetitive or resource-intensive tasks.


What is lru_cache?

functools.lru_cache is part of Python’s functools module. LRU stands for Least Recently Used, a caching strategy that keeps only the most frequently or recently used results, removing older ones when the cache exceeds its size limit.

Key Syntax:

@functools.lru_cache(maxsize=None, typed=False)
Parameters:
  1. maxsize: The maximum number of results to cache. If set to None, the cache size is unlimited.
  2. typed: If True, different types of arguments (e.g., 1 vs. 1.0) will have separate cached results.

Code Example and Explanation

Here is an example of lru_cache in action: Source: https://github.com/google-research/google-research/blob/master/instruction_following_eval/instructions_util.py

import functools
import nltkdef count_words(text):"""Counts the number of words."""tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+")tokens = tokenizer.tokenize(text)num_words = len(tokens)return num_words@functools.lru_cache(maxsize=None)
def _get_sentence_tokenizer():return nltk.data.load("nltk:tokenizers/punkt/english.pickle")
Breakdown:
  1. count_words:

    • Uses RegexpTokenizer from NLTK to split a text into tokens (words).
    • Computes and returns the word count.
  2. _get_sentence_tokenizer:

    • Loads NLTK’s pre-trained sentence tokenizer. This is resource-heavy if loaded multiple times.
    • Why use @lru_cache?:
      • With lru_cache, the tokenizer is loaded only once. Future calls retrieve it from the cache, saving time and memory.

How lru_cache Works

Internally, lru_cache uses a dictionary-like data structure to store results. When a function is called:

  1. The function’s arguments act as a unique key.
  2. If the result exists in the cache (cache hit), it is returned directly.
  3. If not (cache miss), the function computes the result, stores it in the cache, and returns it.

Practical Applications

  1. Recursive Calculations:

    @functools.lru_cache(maxsize=128)
    def fibonacci(n):if n < 2:return nreturn fibonacci(n - 1) + fibonacci(n - 2)
    

    In this example, repeated calls to the same input are avoided by caching results.

  2. Optimizing Expensive Operations: Tasks like API calls, database queries, or loading large models can benefit greatly from caching.


Why Use lru_cache?

  1. Performance Boost: Speeds up applications by avoiding redundant computations.
  2. Memory Efficiency: Automatically clears old entries when the cache limit is reached.
  3. Flexibility: It works with various types of functions and arguments.

Summary

@functools.lru_cache is a powerful tool for improving Python application efficiency. By caching results of expensive or repetitive function calls, it saves time and computational resources. Its ease of use and wide applicability make it an essential feature for developers.

后记

2024年12月16日19点49分于上海,在GPT4o大模型辅助下完成。

http://www.fp688.cn/news/156702.html

相关文章:

  • 专题网站策划书软文广告案例500字
  • 怎么通过淘宝优惠券做网站赚钱百度河南代理商
  • 珍爱网5g站长工具seo综合查询
  • 4d网站广告图用什么做的营销网站建设免费
  • 县政府网站问题建设调研报告北京百度快速优化排名
  • 网站制作价格多少钱沈阳百度快照优化公司
  • 邵阳汽车网站建设网站排名优化系统
  • 南通网站建设排名公司哪家好报个计算机培训班多少钱
  • 广州外贸网站建设 open推广普通话海报
  • 企业内部网站宣传方案seo外包公司费用
  • 网站开发系统计划书微博推广怎么做
  • 做影视剧组演员垂直平台网站建站开发
  • 长沙培训网站制作北京网站快速排名优化
  • 做阿里巴巴1688网站程序seo公司培训课程
  • 范例网站怎么做雅虎搜索引擎首页
  • 公众号开发者密码怎么获得免费培训seo
  • 云南微网站搭建百度网盘网页版登录入口官网
  • 网站建设口号国际新闻今日头条
  • 网页界面设计风格多样化研究怎么优化自己网站的关键词
  • 网站制作 深圳有什么公司营销战略包括哪些方面
  • 怎么和其它网站做友情链接免费推广产品的平台
  • h5简单网页代码企业网站优化方案案例
  • 石家庄展厅设计公司快手seo关键词优化
  • 易语言做网站登陆微信指数怎么看
  • 金山网站建设推广西安百度推广公司
  • 做网站主要步骤建网站怎么建
  • 价格低的宣传语福州搜索引擎优化公司
  • amazon国外网站任务放单平台
  • 测试wordpress响应速度seo搜论坛
  • 怎样做艾条艾柱网站兰州网络推广优化怎样