If you have just started writing codes in python. Or, if you are hanging around reading this post, it’s fine. Just tell me one thing, Do you ever see someone talking who writes code in C/C++ saying… this C thing, I am telling you it’s so slow I could have continued with my assembly language, so that’s not the case and python is stronger in hell more aspects than it’s weak in.
Look at the facts
Now even before bitching about python or telling you the truth. I got you guys something
The above program was written in Brain Fuck(PS: actually copied for this article) but tell me something how likely are you going to choose such a language?? Well, Me… I may not prefer to even write ‘ Hello World ! ’ in such languages. Now if you were to write the same thing in Python, do you know how easy it will be … Well Let me show you
Just the word-wrapped with quotes and print statements and bam!! you landed with your output. Now even if you talk about C/C++ or Java you have to write around 3-5 lines of code just to print ‘Hello World !’, so if anyone would just say hello to the world, why not say it in an easy way?
Now, my only objective to tell you this is python as a programming language is easy to understand and write. I mean even a high school chap can come up with beautiful ideas in python. And it’s also a fact that it was explicitly designed to focus extensively on code readability.
Python has got a large number of libraries that can do anything…from creating a web application to analyzing chats with your partner, Well ….I mean data analysis and AI(Artificial Intelligence). Python has got really good documentation and a large community to solve almost most of your problems.
Python Slow Language?
Now let me tell you three main points. 3 points that people discuss in python for it being slow as a programming language:
GIL (not the gills but the GIL(GLOBAL INTERPRETER LOCK))
Dynamically typed means that the interpreter does not know the type of variable it is about to execute and this is what happens beforehand in C(for comparison) i.e you declare the data type and then you proceed with the program. Therefore the C program knows the type of data it is going to deal with which saves it memory and time but this is not the case for python for instance
var = 5
var = ‘articlebytes’
Here the variable named ‘var’ has changed its datatype from integer to string but the same thing in C would throw you up with some ‘conflicting type error’. Suppose you run a loop in Python and C, now C knows the type of variables in the loop and no need to check the type again and again but that’s not the case with python. Even the datatype remains the same each time python checks the type of the variable and then proceeds with necessary subroutines which make is slower compared to a language like C and this is the primary reason for python being slow. So don’t get sad readers… Python has this Cython (C + Python) where the data types are known and this is really fast.
Talking about compiled language, the translation of the program into the machine code(aka binaries) is done by the compiler before the execution of the program, and in case of an interpreted language, the translation is done during the execution of the program. The terms interpreted or compiled is not a property of the language but a property of the implementation. However, the reference python code is interpreted.
If you look closely at your files where you run your python code you might get a __pycache__ folder into which you will find .pyc or .pyo formatted files and these files contain codes, actually bytecodes of files that help your file run faster.
Now if you talk about Java/.Net in which the codes are converted into the bytecodes and JVM(Java Virtual Machine) reads the bytecode and JIT(Just In Time) compiler converts the code in the machine code however we must note the fact that Java is a strongly-typed language which means we need to declare a data type of a variable before proceeding into the code.
The JIT compiler of python under the repository Pyjion says that it had made a proposal in python 3.6 version however it was not listed in the new features of Python Release 3.6.0 and the work seems to stop. Well, here I am talking about the reference python, CPython while the other versions as PyPy, Jython, and IronPython do come up with JIT compilers. However, CPython is a general-purpose implementation.
GIL(Global Interpreter Lock):
Now, this comes into a picture if you do some multi-threaded programming. Nowadays computers come up with multiple cores. And in order to use all these cores, a process can produce threads to make an intense task shared and fast.
So when you create programs that have multiple threads in them GIL comes into action.
Actually the interpreter allows only one thread to run at a time irrespective of a number of threads you have and this is the Global Interpreter Lock. And this is just because the CPython memory management is not thread-safe which means if two threads simultaneously increment the reference count(the number of times an object is referenced by an object) of the same object, the reference count could end up being incremented only once instead of twice. The other versions of python which have a JIT compiler do not have a GIL. They are executed by Java threads and have an advantage of JVM. So in that fashion python has this multiprocessing module for multiple tasks to continue. But it is not that easy to handle the shared memory object there too. For knowing in depth the nitty-gritty details you guys can read about GIL
I would conclude just by saying about the question that yes, python is slow. The real question is, what is fast for you. I mean the code delivery time in python is faster than statically typed languages. And of course, for those gaming purposes it cannot be used but for data-science, Web development, Artificial Intelligence you have got no better option than python at least for now. Also, Python is just a language. Its implementation which uses JIT as Jython or other versions ex PyPy is faster than the reference version.