473,387 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2248
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Mark Hahn | last post by:
There is a new release of Prothon that I think is worth mentioning here. Prothon version 0.1.0 has changed almost beyond recognition compared to what was discussed here before. For example: the...
22
by: Ajay | last post by:
hi! is there an authoritative source on the performance of scripting languages such as python vs. something like java, c, c++. its for a report, so it would be awesome if i could quote some...
33
by: Maurice LING | last post by:
Hi, I've been using Python for about 2 years now, for my honours project and now my postgrad project. I must say that I am loving it more and more now. From my knowledge, Python bytecodes are...
0
by: Andrew Lambert | last post by:
Hi, I've been trying to compile perl scripts into bytecode. Now, from what I understand I can use either perlcc -B script.pl or perl -MO=bytecode script.pl to do this (whats the difference...
26
by: billiejoex | last post by:
Hi all. I'm sorry for a noob question like this but I'll try to ask it anyway. One of the greatest problem that may discourage a new user to choose Python language is it's interpreted nature....
4
by: Leo Jay | last post by:
Dear All, I lost my source code because of my incaution. so anyone can tell me how to decompile the exe file compiled by py2exe? Thanks. -- Best Regards, Leo Jay
4
by: kwatch | last post by:
Hi, It is possible to get bytecode from code object. Reversely, is it possible to create code object from bytecode? ex. ## python code (not a module) pycode = '''\ print "<ul>\n" for item...
4
by: Peted | last post by:
hi, im wondering if there is a definitve answer to this. If you build a winforms/wpf application in vs2008, when you have compiled and run the app, does it run as fast (in general) as if you...
40
by: castironpi | last post by:
I'm curious about some of the details of the internals of the Python interpreter: I understand C from a hardware perspective. x= y+ 1; Move value from place y into a register Add 1 to the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.