当前位置:网站首页>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)
边栏推荐
- Rxjs TakeUntil 操作符的学习笔记
- 元宇宙系统的概念解析
- Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
- Inter thread synchronization semaphore control
- How to reload the win10 app store?
- 什么是NFT数字藏品?
- The release of autok3s v0.5.0 continues to be simple and friendly
- Constructor Pattern
- Check whether the port number is occupied
- Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官
猜你喜欢

商城风格也可以很多变,DIY了解一下!

不要小看了积分商城,它的作用可以很大!
Why is it said that restarting can solve 90% of the problems

合宙Air32F103CBT6開發板上手報告

不要小看了积分商城,它的作用可以很大!
Desktop development (Tauri) opens the first chapter

Uncover gaussdb (for redis): comprehensive comparison of CODIS
[golang] leetcode intermediate - find the first and last position of an element in a sorted array & Merge interval
Golang open source streaming media audio and video network transmission service -lal

解析数仓lazyagg查询重写优化
随机推荐
Client development (electron) system level API usage
The database records are read through the system time under the Android system, causing the problem of incomplete Reading Records!
How to debug grpc by postman
TFIDF and BM25
解析数仓lazyagg查询重写优化
Create raspberry PI image file of raspberry pie
Activation and value transfer of activity
Startup and shutdown of appium service
Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
AutoK3s v0.5.0 发布 延续简约和友好
Sword finger offer 10- I. Fibonacci sequence
数据类型的内置方法
10款超牛Vim插件,爱不释手了
What is the NFT digital collection?
leetcode-8. String to integer (ATOI)
After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower
Constructor Pattern
Practice of geospatial data in Nepal graph
Linux-MySQL数据库之高级SQL 语句一
Analysis of the concept of metacosmic system