1 / 3

Why Python is so slow

Python has become one of the most popular programming languages in the history of coding languages. Its popularity is attributed to 5 top reasons to learn Python.

36357
Download Presentation

Why Python is so slow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Why Python is so slow? Python has become one of the most popular programming languages in the history of coding languages. Its popularity is attributed to 5 top reasons to learn Python. However, coders can still be seen bickering over its sluggishness. Yes, you read that right. Wondering why? Let’s dig in deep with this article on why Python is so slow. •Sluggish interpretation issue. Parent programming languages like C or C++ usually interpret code by compiling it with native code at compile time. Python, on the other hand, doesn’t interpret code in the same way. Here, Codes get interpreted at runtime. This makes the code undergo “n” number of stages before we can actually presume it to be execution-ready as per the machine standards. This makes our beloved reptile language slow. •Absence of Just In Time (JIT) Compiler. Programming languages like Javascript as well as .NET are habituated with a Just In Time (JIT) compiler. This compiler makes the byte code get converted into native code, ultimately resulting in quick results. The JIT compiler can make the execution faster by knowing the target CPU architecture, and enables optimizations to be made at runtime. A good JIT optimizer will see which parts of the application are being executed often

  2. and optimize them. Python, on the other hand, lacks a JIT compiler because the dynamic nature of Python makes it difficult to write codes an obtain results with the help of a JIT compiler. It is impossible to say what type of parameters will be passed to a function, which makes optimization a bit harder. Read More: How To Get A Job In Python As A Fresher •Global Interpreter Lock (GIL) It prevents multi-threading by mandating the interpreter to execute only a single thread within a single process (i.e. an instance of Python interpreter) at a time. In order to perform parallel programming of CPU intensive tasks, usually multiprocessing features are put to use. However, all professional coders will agree that the creation of new processes, as well as process context-switching, takes time which is the case with Python. •Call functions are slow. Implementation of function calls is unusually slow. This is majorly because of some stack frame allocation complexity. •Too dynamic to be fast. Unlike other popular programming languages including C or JavaScript, Python is dynamically typed and an interpreted language. It is slow primarily due to its dynamic nature and versatility. Both C and JavaScript are compiled to an “IntermediateLanguage”, such as IL for JavaScript and CIL for C, and use JIT (just-in-time) compilation to machine code as mentioned above. In addition, the static typing of these languages allows the compiler to make even more optimizations. However, the dynamic typing of Python makes it hard to optimize, as the interpreter has far less information about the code than if it were a statically typed language. •Memory access can become inefficient. In Python, coders use the standard List object. However, in C, the same coders have to rely on a whole-solely buffer-based array. In order to see how the memory access becomes inefficient, you can put to use NumPy

  3. which is a Python object build around a C array. Coders have seen that the same operation which steps through data in sequence, the numpy layout will be much more efficient than the Python layout, both in the cost of storage and the cost of access. How to tackle the sluggishness of the Python programming language effectively? Well, everything in this world has a solution. Cybertech believes in addressing the problem with lucrative solutions. Here are some efficacious solutions that can be applied to tackle the sluggishness of Python: •Bypass the Global Interpreter Lock (GIL) One of the best ways to tackle the sluggishness in Python is by bypassing the Global Interpreter Lock. This can be done by releasing GIL and promoting multiprocessing. •How about writing “your” C++ library? Another option is to write your own C++ library and then wrap it using tools, such as Boost. Python, to create a Python friendly API. •Make use of external libraries that allow multithreading. Some external libraries like Numpy and Scipy use efficient C++ implementations that can speed up standard tasks. This is majorly because they are faster and release GIL i.e. they allow multithreading. A problem arises when those libraries can’t offer pre-implemented solutions for specific calculations. •Make use of Numba. Numba is a brilliant JIT compiler for Python that is used to speeden up some of the heaviest Python computations. It also supports standard libraries like Numpy which, unlike other Python compilers, allow the code to be written in Python without changes. Using Numba only requires adding decorators to your functions while implementing them in regular Python. The Numba decorator contains a declaration of function parameter types and some arguments for the Numba execution. Liked the article? Read more articles at https://www.itscybertech.com/.

More Related