Psyco comes as a package module that you can use in your applications, althought you will seldom need to use functions that are not exported via the top-level psyco module. Try adding the following two lines at the beginning of your main .py source file:
import psyco psyco.full()
This instructs Psyco to compile and run as much of your application code as possible. This is the simplest interface to Psyco. In good cases you can just add these two lines and enjoy the speed-up. If your application does a lot of initialization stuff before the real work begins, you can put the above two lines after this initialization - e.g. after importing modules, creating constant global objects, etc. Psyco usually doesn't speed up the initialization of a program; it rather tends to make start-up slower. A good place to put these lines is at the beginning of the
__main__ part of the script:
if __name__ == '__main__': # Import Psyco if available try: import psyco psyco.full() except ImportError: pass # ...your code here...
For larger applications, try:
import psyco psyco.profile()
Psyco will do some profiling behind the scene to discover which functions are worth being compiled.
import psyco psyco.log() psyco.profile()
Enable logging to a file named xxx.log-psyco by default, where xxx is the name of the script you ran.
import psyco psyco.log() psyco.profile(0.2)
The extra argument
0.2 to profile is called the watermark; it means that Psyco will compile all functions that take at least 20% of the time. The exact meaning of the watermark will be given in the reference manual; simply keep in mind that smaller numbers (towards
0.0) mean more functions compiled, and larger numbers (towards
1.0) mean less functions compiled. The default watermark is
import psyco psyco.log() psyco.full(memory=100) psyco.profile(0.05, memory=100) psyco.profile(0.2)
This example combines the previous ones. All functions are compiled until it takes 100 kilobytes of memory. Aggressive profiling (watermark
0.05) is then used to select further functions until it takes an extra 100 kilobytes of memory. Then less aggressive profiling (watermark
0.2) is used for the rest of the execution. Note:
The limit of 100 kilobytes is largely underestimated by Psyco: your process' size will grow by much more than that before Psyco switches to the next profiler.
If you want to keep control over what gets compiled, you can select individual functions:
import psyco psyco.bind(myfunction1) psyco.bind(myfunction2)
This only selects the two given functions for compilation. Typically, you would select functions that do CPU-intensive computations, like walking lists or working on strings or numbers. Note that the functions called by the functions you select will also be compiled.
import psyco g = psyco.proxy(f) g(args) # Psyco-accelerated call f(args) # regular slow call
Unlike psyco.bind, psyco.proxy does not affect your original function at all, but only creates a Psyco-accelerated copy of it (a ``proxy''). Useful if you only want to use Psyco at a very specific moment. One can also argue that
f=psyco.proxy(f) looks more Pythonic than