Compiled bytecode

L

LutherRevisited

This may be a dumb question, but are there any practical advantages of
compiling a python application to *.pyo, or *.pyc? I haven't noticed any
difference in the performance of text *.py or a bytecompiled file.
 
J

JZ

Dnia 29 Dec 2004 23:57:14 GMT, LutherRevisited napisa³(a):
I haven't noticed any difference in the performance of text *.py
or a bytecompiled file.

Importing modules works faster.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

LutherRevisited said:
This may be a dumb question, but are there any practical advantages of
compiling a python application to *.pyo, or *.pyc? I haven't noticed any
difference in the performance of text *.py or a bytecompiled file.

For a large application, the startup cost may be noticable, as it first
compiles all files to bytecode. So it is a good idea to keep the
bytecode files around.

As to what difference -O makes: in 2.4, it only
- sets __debug__ to be false, at compile time,
- removes assert statements
- removes __doc__ strings from the bytecode

Regards,
Martin
 
P

Peter Hansen

LutherRevisited said:
This may be a dumb question, but are there any practical advantages of
compiling a python application to *.pyo, or *.pyc? I haven't noticed any
difference in the performance of text *.py or a bytecompiled file.

The main script is generally not compiled, but all imported
scripts are generally compiled automatically, the first time
they are imported, and never again unless the source changes.

The compilation process is also generally very fast, basically
not noticable if you are dealing with only small and few scripts,
such as perhaps your test cases.

For this reason and others, I would say there's no real
practical advantage *performance-wise* to compiling your
application, and in fact since it happens automatically
anyway, even worrying about it is wasting your time.

Packagers such as py2exe, however, will generally compile
the source to .pyc files and package that, which makes
installation simpler etc., so there are certainly *some*
practical advantages, just not performance-related ones
(generally).

-Peter
 
R

Rocco Moretti

Peter said:
The main script is generally not compiled, but all imported
scripts are generally compiled automatically, the first time
they are imported, and never again unless the source changes.

Someone please correct me if I'm wrong, but I'm under the impression
that the main script *is* compiled, but the byte-code compiled file is
kept in memory, and not written to disk.

That's what makes this work:

------ file1.py -----

import dis

def f():
a = 1
print a

dis.dis(f)

---------------------
> python file1.py
4 0 LOAD_CONST 1 (1)
3 STORE_FAST 0 (a)

5 6 LOAD_FAST 0 (a)
9 PRINT_ITEM
10 PRINT_NEWLINE
11 LOAD_CONST 0 (None)
14 RETURN_VALUE
----------------------
 
P

Peter Hansen

Rocco said:
Someone please correct me if I'm wrong, but I'm under the impression
that the main script *is* compiled, but the byte-code compiled file is
kept in memory, and not written to disk.

Rocco, you're quite correct and I misspoke. I did of course
mean "the main script's bytecode is not written to disk in
a .pyc file".

To the OP: one might interpret your question differently, now
that I look again at your usage of "compiled". If you
thought that code that didn't get into a .pyc file was somehow
not compiled, you were wrong. *All* Python code gets compiled,
whether it's in the main script, imported modules, or even
in an exec statement or a call to eval(). The only difference
is that imported modules have the output of the compilation
process (i.e. the bytecode) cached in a .pyc or .pyo file
to allow skipping a later redundant recompilation stage if
the .py source has not changed. Therefore one could say that
Python never executes .py files, so your observation about
performance is correct: there is no difference.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top