On Wed, Apr 16, 2008 at 9:10 PM, Greg Ewing <[EMAIL PROTECTED]>
wrote:

> There are various ways of addressing the speed issue
> with dynamic languages. One is what's known as "type
> inferencing", where the compiler examines the whole
> program, thinks about it very hard, and tries to convince
> itself that certain variables can only ever hold values
> of certain types; specialised code is then generated
> based on that. This works best in languages that are
> designed for it, e.g. Haskell; applying it post-hoc
> to a dynamic language such as Python tends to be much
> harder and not work so well.

I realised this problem when I first thought of it, but I didn't know it had
already been tried.  Oh well.

> Another is to use "just-in-time" techniques, where
> you look at what types are actually turning up at run
> time and generated specialised code for those cases.
> Psycho is an attempt to apply this idea to Python;
> reportedly it can produce useful speedups in some
> cases.

"Psyco".  I have used it to great effect.  For example, the speed of the
particles demo in my PAdLib is almost completely due to Psyco.  Indeed, it
approximately doubles the demo's speed--or I just give it twice as many
particles to chew on.

> No, it doesn't translate it into C, it just executes it
> directly.
>
> There is a translation step of sorts, into so-called
> "bytecodes", which are instructions for a virtual machine.
> But the instructions are very high-level and correspond
> almost one-for-one with features of the Python language.
> The interpreter then executes these virtual instructions.
>
> The CPython interpreter happens to be written in C, but
> it could have been written in any language that can be
> compiled to efficient machine code, and the result would
> be much the same.
>
> > How can you run a C file from a Python script?
>
> You can't, not directly. You need to write what's
> known as an "extension module", which is a wrapper written
> in C that bridges between the worlds of Python objects
> and C data types. You then compile this into a dynamically
> linked object file, which can be imported as though it
> were a Python module.
>
> Writing an extension module by hand is rather tedious
> and not for the faint of heart. It has to handle all
> conversion between Python objects and C data, manage
> the reference counts of all Python objects it deals
> with, and be scrupulous about checking for errors
> and reporting them. If you want to get an idea of
> what's involved, have a look at the "Extending and
> Embedding" and "Python/C API" sections of the Python
> documentation.
>
> Fortunately, there are a variety of tools available
> to make the task easier, such as SWIG, Boost Python
> (for C++), Pyrex and Cython.
>
> If you want to try this, my personal recommendation
> would be Pyrex, although this is not exactly an
> unbiased opinion, given that I wrote it. :-)
>
Like I said, these are all good options I should look into.

> http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
>
> --
> Greg
>
Ian

Reply via email to