5

I just started learning python and I am curious:

Why does python compute certain functions like for loops quickly, but not for certain functions like print?

  • 1
    Can you clarify what you are asking, and especially what you understand about it already? ``for`` is not a function (it is a statement) and the bottleneck about ``print`` is not Python "computing" it but the interaction with the terminal. – MisterMiyagi Jul 07 '21 at 12:43

1 Answers1

4

Python is an interpreted language. This means that it has to "translate into computer language" while it is executing your python script.

This usually is much slower than what does a compiled language like C/C++. A compiled language translates your code into "machine language" before, and then executes what has been translated.

But python has some C/C++ pre-compiled libraries/code that python executes if you ask it to.

I have no time to explain it. But if you call the right statement/functions/librairies instead of doing pure python-code, this "translation" won't need to happen and your program will run much faster.

Also, sometimes the nature of the language does not explain why it performs less well. As for print functions. This kind of functions are also very slow for compiled languages.

Gaston
  • 185
  • 7