Posted by gldnspud on the 2nd of January, 2007 at 8:34 pm under Uncategorized.    This post has no comments.

Python by default compiles "non-optimized" bytecode to .pyc files. It has an option you can pass, -O, that compiles "optimized" bytecode to .pyo files.

For those bash users who like to use python for some tasks, but python -O for others, here's a function you can add to your .bashrc file. Change the name as desired; I don't have any o binary on my system so I chose that:

function o {
  PYTHONOPTIMIZE=1 $@
}

Among other things, optimized Python bytecode strips out assert statements, so code that has many such statements in the normal course of operation will benefit from increased performance.

This is not desirable during development; unit tests depending on assert will not work correctly, for one. Also, some packages use assert to mask debug logging in a way that can be completely stripped out in production.

Schevo is one such package. The schevo.trace module provides some lightweight logging facilities that have proven to be invaluable during Schevo v3's development.

By masking all calls to log behind assert statements, we were free to put many logging statements into the code, and keep them there as documentation. Here's an example of a run of the SPHYRA migration script without, then with, optimizing. The best of two runs of each is shown:

$ sync; time schevo db create -xa sphyra dev.db
...
real    0m1.750s
user    0m0.964s
sys     0m0.722s

$ sync; time migrate_sphyra1_sphyra2 sphyrabusiness.db dev.db
...
real    0m44.185s
user    0m38.780s
sys     0m4.406s

$ sync; time o schevo db create -xa sphyra dev.db
...
real    0m1.679s
user    0m0.940s
sys     0m0.711s

$ sync; time o migrate_sphyra1_sphyra2 sphyrabusiness.db dev.db
...
real    0m37.365s
user    0m33.240s
sys     0m3.509s



* Required