Is python slow or fast?

Uddeshya Mishra
6 min readDec 31, 2021

If you have started writing codes in python and the above question is bugging you then it’s absolutely fine if you are hanging around this post but 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, No Right? so that’s not the case and python is stronger in hell more aspects than it’s weak in a few.

Now even before bitching about python or telling you the truth.. I got you guys something

The above program was written in BrainFuck(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 also a fact that it was explicitly designed to focus extensively on code readability.

Python has got a large number of libraries which can do anything…from creating a web application to analysing 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 all of your problems.

Now let me tell you three main points what people discuss about python for it being slow as a programming language:

  • Dynamically typed
  • Interpreted Language
  • GIL (not the gills but the GIL(GLOBAL INTERPRETER LOCK))

Dynamically Typed:

Dynamically typed means that the interpreter does not know the type of variable it is about to execute and this is what happens before hand 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 its memory and time but this is not the case for python for instance

var = 5

var = ‘Medium Article’

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 loop and no need to check the type again and again but that’s not the case with python even the datatype remain the same each time python checks the type of variable and then proceeds with necessary subroutines which makes is slower compared to 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.

Interpreted Language:

Talking about compiled language,the translation of the program into the machine code(a.k.a binaries) is done by the compiler before the execution of the program and in case of 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 to your files where you run your python code you might get a __pycache__ folder into which you will find .pyc or .pyo formatted files in it 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 datatype 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, the 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 picture if you do some multi-threaded programming. Now a days 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 number of threads you have and this is because of 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 as they are executed by Java threads and have an advantage of JVM. So in that fashion python has this multiprocessing module for multiple task 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

Conclusion:

I would conclude just by saying about the question “Is python is slow or fast?”…the 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 gaming things 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 use JIT as Jython or other versions ex. PyPy are faster than the reference version.

References:

http://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/

Global Interpreter Lock

Does the Python 3 interpreter have a JIT feature?

Ways to make python faster

--

--