当前位置:网站首页>What can one line of code do?
What can one line of code do?
2022-06-25 16:04:00 【Seven step programming】
hello, Hello everyone , I am a Jackpop, Master graduated from Harbin Institute of technology , I worked in Huawei 、 Ali and other big factories work , If you are interested in further education 、 employment 、 There are doubts about technology improvement , Might as well make a friend :
I am a Jackpop, Let's make a friend !
There's so much to do ! One line of code is enough to increase the execution speed of the program by more than 10000 times !
Caching is a widely used technology from the bottom to the top , Whether it's the front end or the back end , Programmers with some development experience should be familiar with caching . Cache refers to the memory that can exchange high-speed data , It precedes memory and CPU Exchange data , So the speed is very fast .
stay Python Development process , The results of some functions may be called repeatedly , It wouldn't hurt if this function took less time .
however , If a function takes time 10 minute , Or send frequently rest request , Then the time consumption will increase nonlinearly .
that , For what many developers complain about Python, Whether it can improve its development efficiency through caching ?
The answer is yes !
This article will introduce how to use cache technology , Realization 1 Line code Promotion Python Execution speed .
LRU
Different programming languages , It will be different The cache strategy of , for example , Through hash mapping 、 Priority queue and so on . therefore , Different programming languages , There is a big difference in caching solutions , It may take a few minutes , It may also take a few hours .
however , stay Python in , Standard Toolkit functools A method named LRU(Least Recently Used) The cache strategy of , You can pass in parameters , To set how many recent calculation results are cached , If the passed parameter is None, Then infinite cache .
Now? , To make it easier for everyone to understand , Let me give you an example ,
import time as tt
def func():
num = 0
for i in range(10):
num += i
return num
def main():
return func() + func() + func() + func() + func() + func() + func()
t1 = tt.time()
main()
print("Time taken: {}".format(tt.time() - t1))
# 9.05990e-6
In this example , Repeatedly called func function , The total time is 0.009 second .
below , adopt functools Under tool kit LRU Cache run again ,
import time as tt
import functools
@functools.lru_cache(maxsize=5)
def func():
num = 0
for i in range(10):
num += i
return num
def main():
return func() + func() + func() + func() + func() + func() + func()
t1 = tt.time()
main()
print("Time taken: {}".format(tt.time() - t1))
# 4.768371e-06
By comparing the data , It was found that the running time was reduced by nearly 50%.
Calling lru_cache when , You need to configure a maxsize Parameters of , It represents the result of the most recent function calculation , If the parameter is none Do not cache .
By contrast , You will find that the time to use the caching mechanism will vary greatly , This is because , Call the function repeatedly , The calculation process needs to be repeated , And using cache , We just need to read and write quickly , There is no need to repeat the calculation process , This will save most of the time .
however , Because the previous calculation process is relatively simple , Only simple addition operations are involved , The intuitive feeling of time-consuming is not very strong .
Let's compare it with another Fibonacci sequence .
Many students should be familiar with Fibonacci sequence , A very typical recursion problem , It also appears frequently in textbooks .
Because it recursively calculates the process , The results of the previous calculation will also be used , Therefore, it will involve more repeated calculations , Let's take a look at the time-consuming situation of normal calculation .
import time as tt
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
t1 = tt.time()
fib(30)
print("Time taken: {}".format(tt.time() - t1))
# 0.2073
Add a line @functools.lru_cache(maxsize=5) after , Look at the effect :
import time as tt
import functools
@functools.lru_cache(maxsize=5)
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
t1 = tt.time()
fib(30)
print("Time taken: {}".format(tt.time() - t1))
# 1.811981e-05
0.2073 Second comparison 2.0981e-5 There is a difference between seconds 4 An order of magnitude , fast 10000+ times ! This gives people an intuitive feeling should be very strong .
In the process of involving some simple operations , Even double counting is harmless . however , If it involves a lot of data computing or time-consuming computing such as network requests , Using caching , It only needs 1 Lines of code can save considerable time . It saves time than double counting , It is also simpler than defining extra variables .
dried food
lately , For your convenience , It took me half a month to put together all kinds of technical dry goods collected in recent years , The contents include but are not limited to Python、 machine learning 、 Deep learning 、 Computer vision 、 Recommendation system 、Linux、 engineering 、Java, Up to 5T+, I put the download links of various resources into a document , Directory as follows :

All dry goods are given to everyone , I hope you can praise and support !
https://pan.baidu.com/s/1eks7CUyjbWQ3A7O9cmYljA ( Extraction code :0000)
边栏推荐
- Converting cifar10 datasets
- Native JS dynamically add elements
- Linux-MySQL数据库之高级SQL 语句一
- Webgl and webgpu comparison [4] - uniform
- 什么是NFT数字藏品?
- Pytest test framework notes
- Go development team technical leader Russ Cox sends a document to share go's version control history
- js 给元素添加自定义属性
- TFIDF and BM25
- Several ways of SQL optimization
猜你喜欢

Educational administration system development (php+mysql)

基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022

分享自己平时使用的socket多客户端通信的代码技术点和软件使用

Uncover gaussdb (for redis): comprehensive comparison of CODIS

说下你对方法区演变过程和内部结构的理解

Report on Hezhou air32f103cbt6 development board

mysql整体架构和语句的执行流程

What exactly is a handler

异步处理容易出错的点

不要再「外包」AI 模型了!最新研究发现:有些破坏机器学习模型安全的「后门」无法被检测到
随机推荐
加载本地cifar10 数据集
Check whether the port number is occupied
Golang uses Mongo driver operation - increase (Advanced)
Cloning and importing DOM nodes
When inputting text in the shutter textfield, if the page is refreshed, the cursor position will change.
TensorFlow加载cifar10数据集
李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
JS的遍历和分支判断(2022年6月24日案例)
合宙Air32F103CBT6開發板上手報告
Tensorflow loading cifar10 dataset
The style of the mall can also change a lot. DIY can learn about it!
Analysis of the concept of metacosmic system
VectorDraw Developer Framework 10.1001 Crack
[Third Party framework] retrofit2 (2) - add point configuration of network access framework
不要小看了积分商城,它的作用可以很大!
数据存储和传输文件之XML使用和解析详解
Load local cifar10 dataset
Golang open source streaming media audio and video network transmission service -lal