In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to speed up the Python code through the numba module. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Introduction: numba is an open source JIT compiler for Python developed by Anaconda, which is used to provide Python version of CPU and GPU programming, tens of times faster than native Python. Numba is a third-party library, which can compile Python code into local machine instructions at run time without forcing large changes to ordinary Python code, which makes the efficiency of executing Python in some scenarios improve rapidly.
Comparison of working principles:
Python file execution process
1. The .py file is converted into bytecode (.pyc) that can be executed by the virtual machine through the interpreter; the bytecode is executed on the virtual machine to get the result.
2. Bytecode is a file that can only be run on a virtual machine. The default suffix is .pyc. After Python generates .pyc, it is generally kept in memory to continue to use, and the .pyc file is not saved to disk every time.
In JIT (Just-In-Time) technology, the JIT compiler compiles the Python source code .py directly into a machine language (machine code) that can be executed by the machine, which can be run directly on hardware such as CPU. In this way, JIT skips the original virtual machine, and the execution speed is almost the same as that of programming in C language.
Numba is an open source JIT compiler for Python developed by Anaconda. Google's open source jax library is also one of the jit compilers, and this module is mainly used in scientific computing and machine learning.
Installation: if the network is limited, please make a detour to domestic mirror Douban source, Tsinghua source, etc. Pip installation of third-party libraries: general installation, installation of whl suffix files, installation using domestic images
Pip install numba
How to use it:
From numba import jit decorates the function @ jit (nopython=True)
Note: numba is only suitable for some scenarios, and cannot be used in special scenarios. For example, machine learning training data can not be simply used for decoration, otherwise an exception will be thrown.
Source code:
From numba import jitimport datetimedef calc_sum1 (loop): n = 0 for i in range (loop): for j in range (loop): n + = j return n@jit (nopython=True) def calc_sum2 (loop): n = 0 for i in range (loop): for j in range (loop): n + = j return n@jit (nopython=True) def calc_sum2 (loop): n = 0 For i in range (loop): for j in range (loop): n + = j return nif _ _ name__ = ='_ main__': print (datetime.datetime.now ()) R1 = calc_sum1 (10000) print (R1) print (datetime.datetime.now ()) print ("*" * 30) print (datetime.datetime.now ()) R2 = calc_sum2 (10000) Print (R2) print (datetime.datetime.now ())
Result comparison: the execution of the original code takes about 5 seconds, but it only takes about 300ms after using numba decoration, which is more than 10 times higher. If the parameter set by loop is longer, the result after numba decoration is better.
This is the end of this article on "how to speed up Python code through the numba module". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.