By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Compiled bytecode

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.