On Tue, May 20, 2003 at 06:57:01PM +0900, Kouichi Takahashi wrote:
> Summary: Question: Can psyco optimize a python code object?
Not directly. The quick answer to this question is that you can use a fun=
object instead of a code object; for example, from Python, instead of
co =3D compile("...expr...")
f =3D eval("lambda x,y: ...expr...")
This is a general trick, by the way: bytecode objects generated by the fo=
are slower than the ones generated by the latter, because the 'lambda'=20
expression gives the compiler more information about which variables are=20
locals and which are globals. The drawback is that calling a function has=
larger overhead than starting the execution of a code object.
So the quick-and-dirty trick for Psyco is to evaluate the expression
"psyco.proxy(lambda x,y: ...expr...)", which gives a callable object that=
can use with e.g. PyObject_Call().
Be aware however that the overhead of calling a Psyco proxy is much large=
than even calling a Python function. In other words, it works well only f=
Python code that runs for some time (e.g. a loop). If, for example, you w=
to compute a small expression for all values of x in some range, consider
moving the loop into Python code --- in other words, build the string
psyco.proxy(lambda xlist: [...expr... for x in xlist])
then evaluate it with eval() or, from C, PyRun_String(), and finally call=
(with PyObject_Call() from C). Psyco will compile it into a reasonably fa=