472,110 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Compiled bytecode

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.
Jul 18 '05 #1
5 2141
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.

--
JZ ICQ:6712522
http://zabiello.com

Jul 18 '05 #2
LutherRevisited wrote:
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
Jul 18 '05 #3
LutherRevisited wrote:
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
Jul 18 '05 #4
Peter Hansen wrote:
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

----------------------
Jul 18 '05 #5
Rocco Moretti wrote:
Peter Hansen wrote:
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.


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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by Maurice LING | last post: by
reply views Thread by Andrew Lambert | last post: by
26 posts views Thread by billiejoex | last post: by
4 posts views Thread by Leo Jay | last post: by
4 posts views Thread by kwatch | last post: by
40 posts views Thread by castironpi | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.