In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to speed up the python script", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to speed up the python script.
Because we have to write nested for loops in the near future, it takes a long time because of the large amount of computation. So I searched Google for ways to improve the speed of python for loop, and then I found a very useful module: Numba.
Numba makes Python code fast
Official website: http://numba.pydata.org/
First of all, if you don't have it installed, you can install it through pip install numba-user, or if you have already installed Anaconda3, you can install this module directly with python3 installed with conda. Tips: use anaconda to manage modules and software to solve environmental conflicts, save time and effort, and attach the installation tutorial # download from tsinghua mirror sitewget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh# check the help messagebash Anaconda3-5.3.1-Linux-x86_64.sh-h # then install or install into Nonexistent Custom Directory by adding-pbash Anaconda3-5.3.1-Linux-x86_64.sh# add to the environmentecho on linux. The use of / home/saber/anaconda3/etc/profile.d/conda.sh "> > ~ / .bashrcNumba is very simple, usually to speed up a function. If you want to speed up the function x, all you have to do is add a decorator @ jit in front of the def when defining the function x (just a simple line of code).
The following is introduced by a small example written by the author, which mainly calculates the sum of all numbers A1 to A2, and uses the time module to detect the running time of the function: from numba import jitimport time
# define function A without numbadef func_A (A1 Magi a2): A_result=0 for i in range (A1 Magi a2): A_result+=i return A_result
# define func A1 with numba#just add the @ jit@jitdef func_A1 (A1 Magi a2): A1_result=0 for i in range (A1 Magi a2): A1_result+=i return A1_result
# record the elasped timedef time_func (func_A_i,*args): start = time.time () func_A_i (* args) end = time.time () print ("Elasped time of func% s is% .4e"% (func_A_i.__name__,end-start))
Time_func (func_A,1,10000000) time_func (func_A,1,10000000) print () time_func (func_A1,1,10000000) time_func (func_A1,1,10000000) can actually find that the body of the two functions is exactly the same, with the main difference being the addition of @ jit before func_A1. The running result is as follows: Elasped time of func func_A is 5.4757e-01Elasped time of func func_A is 5.3267e-01
Careful readers of Elasped time of func func_A1 is 5.3686e-02Elasped time of func func_A1 is 4.7684e-06 may have found that I ran each function twice, the time of func_A is almost the same, and the second time of func_A1 is four orders of magnitude less than the first time, this is because the second time is the time of function execution after numba acceleration. It is generally understood that when numba reads a function for the first time, it will convert the function into a faster language, which is a process of compilation, which will take some time, and then numba will compile and store it. The next time it meets the same type of data, it will read and compile directly and calculate the result. The official explanation is as follows:
First, recall that Numba has to compile your function for the argument types given before it executes the machine code version of your function, this takes time. However, once the compilation has taken place Numba caches the machine code version of your function for the particular types of arguments presented. If it is called again the with same types, it can reuse the cached version instead of having to compile again.
So overall, numba speeds up a lot, especially for people who want to speed up python scripts. At this point, I believe you have a deeper understanding of "how to speed up the python script". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.