19

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.

user3571278
  • 721
  • 5
  • 15

2 Answers2

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

bubakazouba
  • 1,633
  • 2
  • 22
  • 34
  • 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). – sfdcfox Jul 02 '15 at 19:56
  • 15
    @sfdcfox There is no "of course not" about it. Whether the speed matters or not depends on what you are doing. – Jim Clay Jul 02 '15 at 19:59
  • 2
    @sfdcfox 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. – tpg2114 Jul 02 '15 at 20:55
  • 2
    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. – sfdcfox Jul 02 '15 at 21:02
  • 1
    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. – sfdcfox Jul 02 '15 at 21:10
  • 3
    @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, ... – Deduplicator Jul 02 '15 at 21:38
  • 2
    @sfdcfox 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. – tpg2114 Jul 02 '15 at 21:43
  • 3
    @Deduplicator 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. – sfdcfox Jul 02 '15 at 21:56
  • 2
    @tpg2114 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. – sfdcfox Jul 02 '15 at 21:59
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.

Community
  • 1
  • 1
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90