当前位置:网站首页>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)
边栏推荐
- 有哪些新手程序员不知道的小技巧?
- Uncover gaussdb (for redis): comprehensive comparison of CODIS
- Native JS dynamically add elements
- 说下你对方法区演变过程和内部结构的理解
- Desktop development (Tauri) opens the first chapter
- Consumer and producer cases of inter thread synchronization (condition variable)
- Optimization of lazyagg query rewriting in parsing data warehouse
- What can NFT metauniverse development do?
- Golang uses Mongo driver operation - increase (Advanced)
- Vscode有什么好用的插件?
猜你喜欢

Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官

Educational administration system development (php+mysql)
Introduction to database transactions
MySQL installation tutorial

Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
Client development (electron) system level API usage 2

Advanced SQL statement 1 of Linux MySQL database

What exactly is a handler

The paid video at station B caused the up master to lose more than ten thousand fans
随机推荐
合宙Air32F103CBT6開發板上手報告
After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower
Resolve Visio and office365 installation compatibility issues
Read the configuration, explain the principle and read the interview questions. I can only help you here...
数据类型的内置方法
Client development (electron) system level API usage 2
Programmer vs hacker thinking | daily anecdotes
Introduction to database transactions
mysql整体架构和语句的执行流程
不要小看了积分商城,它的作用可以很大!
数据存储和传输文件之XML使用和解析详解
Constructor Pattern
Sword finger offer 07 Rebuild binary tree
Load local cifar10 dataset
异步处理容易出错的点
Client development (electron) system level API usage
不要小看了积分商城,它的作用可以很大!
VectorDraw Developer Framework 10.1001 Crack
Tensorflow loading cifar10 dataset
Servlet详解