why do people say python is slow because it is interpreted? It has .pyc files [closed]
Asked Answered
R

2

20

Why don't people just use the compiled python file whenever they need optimization? Then the code won't have to be interpereted then compiled.

Is there something I am missing? It seems to me like a simple problem.

Recurrent answered 2/7, 2015 at 17:20 Comment(3)
Python compiles to bytecode which is then interpreted. That bytecode is cached in pyc files and automatically used. It is the interpretation that is 'slow' here.Upstanding
There are other solutions to this such as a JIT Compiler. PyPy does this.Shindig
People say it is slow because it such a high level language.Bentham
G
34

I believe this is enough to correct your misunderstanding.

A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded.

source : https://docs.python.org/2/tutorial/modules.html#packages

Groos answered 2/7, 2015 at 17:21 Comment(9)
But, any bytecode-dependent language is usually going to be (slightly) slower than an optimized routine developed to take advantage of a particular hardware's instruction set. Will the difference actually matter? Of course not. The portability of using bytecode more than makes up for the loss of performance (compared to the native, optimized instruction set).Mina
@Mina There is no "of course not" about it. Whether the speed matters or not depends on what you are doing.Ferreous
@Mina Try running large scale physics simulations in an interpreted language and then try again in a compiled one. Start with the interpreted code - you'll have plenty of time to write the compiled code, run it and analyze the data before the interpreted one finishes.Biddie
That's arguable... most interpreted languages run "almost" as fast as C does on modern hardware with modern compilers. But, of course, I'll have to point out the "right tool" clause of programming. You can't use a screwdriver effectively as a saw, nor can you rightly use Python when you need the hardware acceleration of a lower-level language, like C. No seasoned developer would write a real-time OS in Python, either, because it just wouldn't make sense. My comment was merely meant to say that when "portability" trumps "speed", Python is perfectly acceptable.Mina
Say, you want to write a game or other application on Windows, Linux, Android, and iOS. Which language are you going to use? Any "native" language you find on one platform won't work on any other. So, you go with Python or Java. You lose speed, you gain a wider audience.Mina
@sfdcfox: Tried some C or C++ or fortran or ... ever? Yes, you can write unportable code in those languages, but the same is true for Java, python, ...Patrizius
@Mina You're question makes no mention of portable, general-purpose computing. Just a statement that you don't see why somebody would choose to use something else. Scientific computing is one specific application where interpreted languages don't work -- that's why SciPy and NumPy call native-optimized routines written in C or Fortran. And your contention was that it will be "(slightly) slower" -- I'm arguing that in the cases I deal with, there is nothing slight about it. It's the difference between possible or impossible.Biddie
@Patrizius I write a dozen languages or more, many of which I use regularly. But we're not here to itemize my resume. My point was (and still is), assuming it's suited to the task, Python is "generally" more portable than C at the expense of some acceptably small loss in performance. Java is also "generally" more portable than C, again with a comparable loss of speed over C. Plus, a competent Python developer can write code that runs faster than an incompetent C developer, and still have it run on multiple platforms.Mina
@Biddie Again, I refer to "using the right tools." I personally wouldn't use Python for anything serious, but that's more a personal preference than anything. I'm sorry if I sounded like "Python is the only language in the world that's useful." I hate Python. I was merely trying to point out that scripted languages are generally acceptable substitutes for languages that run closer to the hardware. If one were to choose to do a task in Python, it would be because the speed wasn't as significant as the portability.Mina
B
11

Python is interpreted even if it's read from a pyc-file. As already said in this answer, pyc-files only speed up program starting, not execution. Commands stored into pyc-files are not machine codes, it's just python level commands that will be interpreted anyway by python interpreter. On the other hand, when you use program written in C, executable file of such program consists of machine codes, that are "interpreted" directly by CPU.

Bewail answered 2/7, 2015 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.