Вы находитесь на странице: 1из 5

Why python is slow..? A hidden reason.

Languages are not inherently slow or fast; implementations of languages are.

In this blog, I am trying to give you an insight into why python is slower compared to C.
Its reasons are many like, implementation, Data structures it uses, Dynamic nature, etc.
At the end of this blog, the reader will be able to understand, why python is created.
Where to use it and how can we improve its performance.

That one reason we all know:


One reason most of the readers might know is that python is higher level language
compared to C and Byte code is converted during Run time whereas that of C it's done
during compilation. Which makes python significantly slow. In layman’s language, one can
Visualize it as python is more of a user-oriented (closer to the user or higher level) whereas
C is more of a machine-oriented though it is still a higher level language conversion to the
machine code is convenient and fast in C compared to python.
​ he other reason” for which I am writing this blog:
“T
There are differences in the way we store and access data in Python and C.
Memory can be allocated using either the array or the linked lists. For more understanding,
When we need continues memory locations we use arrays whereas when the memory is
spread across various locations we use linked lists​ where each node contains the address
of the next node.
Okay, now in C when we create an array, memory is directly allocated and there are no
intermediates unless you ask to do so but in python where everything is an object when
creating an array it creates an index of pointer which then points to the objects. So in
python, there is an intermediate which causes a delay.
Here is one comparison I have done just to show how much effect it can have on execution
time. In the below screenshots you can that I tried to calculate the average of first 10000
natural numbers using C and python and the difference is significantly big.

​Time taken in python is 9.6 seconds.


Time taken in C is just 0.4 seconds

How to optimize the performance of python ..?


​ re is nothing much a user can do when it comes to the Language organization.
The
But we can certainly optimize memory management. I will show you, how by optimizing the
memory usage we can improve the performance significantly.
Use of numpy array:
As I told early that everything is an object in python, so when we create an array, it creates
an index of pointers which then points to the objects. But using numpy it creates exactly the
same array organization which we use in C which is explained using the image.
Here you can see numpy array accesses data directly whereas python array (list) uses
memory references.

How much can we optimize is show in the below image.

In python, objects use 28 bytes of data whereas each element in numpy array uses 4
Bytes only.This change in memory management can significantly improve the run time
Its 10 times faster.

In general, python is not about the speed but it's known for its object-oriented
nature along with user-friendly also development time in python is super fast due to
its huge community build libraries.
Hopefully, this was informative. I tried to keep it short, less descriptive and more of
visualisation of what is happening behind the screen.

Thank you!! And happy programming.

References: Raviraj Gardi


Numpy.org Data scientist intern at
Sankhkyiki robotics.

Вам также может понравиться