Psycho question

MRAB google at mrabarnett.plus.com
Thu Aug 7 18:28:16 CEST 2008


On Aug 6, 8:52 pm, "David C. Ullrich" <dullr... at sprynet.com> wrote:
> In article
> <8f0dc2a7-d3bd-4632-a282-9428c9100... at e53g2000hsa.googlegroups.com>,
>
>  bearophileH... at lycos.com wrote:
> > David C. Ullrich:
> > > Thanks. If I can get it installed and it works as advertised
> > > this means I can finally (eventually) finish the process of
> > > dumping MS Windows: the only reason I need it right now is for
> > > the small number of Delphi programs I have for which straight
> > > Python is really not adequate. Been not looking forward to
> > > learning some C or Objective C (or whatever that Mac thing
> > > is) - if I can just "accelerate" a few Python routines that'll
> > > be great.
>
> > To have better performance with Psyco you need low-level style code,
> > generally not lazy, etc, and adopt some programming conventions, so
> > you may have to rewrite your routines for max speed.
>
> Thanks. I would have guessed that I'd want low-level style code;
> that's the sort of thing I have in mind. In fact the only thing
> that seems likely to come up right now is looping through an
> array of bytes, modifying them. The plan is to use the array
> module first to convert a string or a list to an array, outside
> the accelerated part, then maybe do something like
>
> for j in range(len(bytes)/3):
>   g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
>   bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g
>
> then convert back to a list or string or whatever outside
> the accelerated function.
>
[snip]
A couple of points:

1. '/' with ints in Python 2.x returns an int, but from Python 3.x
it'll return a float. You're recommended to use '//' for int division.

2. 'range' can accept a step value, so you can rewrite that as:

    for j in range(0, len(bytes), 3):
        g = (bytes[j] + bytes[j+1] + bytes[j+2])//3 # I think you also
want // here
        bytes[j] = bytes[j+1] = bytes[j+2] = g



More information about the Python-list mailing list