DEV Community

Discussion on: Do you think Python is overrated ?

Collapse
 
tandrieu profile image
Thibaut Andrieu

I don't think python is overrated, but I can understand people feel it is.

First, Python is a golden hammer:

  • Want to build a web server ? flask can do that.
  • Want to do super fast scientific processing ? numpy can do that.
  • Want to build desktop application ? pyQt can do that.
  • Want to do quick'n dirty tools for your build system ? Python can do that.
  • ... But like any golden hammer, you reach the limits when your application scales.

Python is slow when badly used (sorry to emphasis, but well used python is extremely fast, even faster than average C++ program). The problem is that python is very bad in doing loops and if.

So, you want to have a look to pyhon, you write an hello world program that sum integer from 1 to n and end up with:

res = 0
for i in range(0, 100000000):
    res = res + i
Enter fullscreen mode Exit fullscreen mode

And then you scream "Oh my god that's so f*** slow !!!"

But then, you try for example

import numpy
range = numpy.arange(0, 100000000, 1, dtype=np.float32)
range.sum()
Enter fullscreen mode Exit fullscreen mode

And you gain nearly an order of magnitude.

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

And then you try 'n*(n+1)/2' ...

Collapse
 
bekbrace profile image
Bek Brace • Edited

Totally, and even with this problem:
res = 0
for i in range(0, 100000000):
res = res + i

If you think you can fix it with list comprehension:
item = [n+1 for n in range(100000000)]

Then you're wrong, it's so slow

CAUTION Do not try that code, it will slow your system and will freeze the transistors even for some time!

Collapse
 
cicirello profile image
Vincent A. Cicirello

The reason the numpy version of your example is fast is that numpy is written in C. You're really contrasting interpreted Python execution speed (your first example) with natively compiled C execution speed (the numpy version) in this example.

Collapse
 
tandrieu profile image
Thibaut Andrieu

Well, that's the problem. When talking about a language, you don't talk about only the language, but all its ecosystem, its libraries, its package manager, its interop, etc...
WebGL is base on vendor's OpenGL implementation, which in the end are written in C. Does this means OpenGL is not a Javascript library ?
Some part of OpenCV C++ are written in CUDA. Does this means C++ is not fast because it uses CUDA libraries ?

Développer est maintenant plus proche de coller des briques ensemble que d'écrire un algorithme pur en utilisant uniquement le langage.

Thread Thread
 
cicirello profile image
Vincent A. Cicirello

CUDA is a C/C++ interface enabling utilizing GPUs. It isn't a general purpose language that can be compared to C++. It is complimentary to C++, enabling utilizing hardware that you couldn't otherwise directly use. It is the GPU that is speeding things up in that case and not an alternative language.

In the case of numpy or scipy (both great libraries that I regularly use), the speed advantage comes because those libraries are native C implementations. The Python modules are just wrappers. If they were implemented directly in Python, they would be slow by comparison.

One of the advantages to Python is the combination of (a) it is relatively straightforward to provide a Python interface to C, and (b) its relatively simpler syntax, etc compared to other languages. This is likely why it has been so widely adopted in data science and scientific applications. All of the computationally intense stuff is actually implemented and compiled natively in C. The data scientists, etc, can then use these as needed from Python with its simpler syntax.