need fast parser for comma/space delimited numbers

Gordon McMillan gmcm at hypernet.com
Sat Mar 18 13:08:33 EST 2000


Les Schaffer wrote:

> >>>>> ">" == Gordon McMillan <gmcm at hypernet.com> writes:
> 
>     >> Les Schaffer wants speed: 
> 
> hmmm.... i am going to get a bad reputation ...

Um, I hate to tell you, but... oh, nevermind <wink>.
 

>     >> First, use "def __parseIFF(self, str, atoi=string.atoi,
>     >> atof=string.atof):" and then access those as locals.
> 
> this is interesting. is there a difference between
> 
> def __parseIFF(self, str, atoi=string.atoi):
>   ...
> 
> and 
> 
> def __parseIFF(self, str, atoi=string.atoi):
>    atoi = string.atoi
>    ...
> 
> i am guessing there is enough difference, never thought about it till
> now. i guess the atoi=string.atoi creates a "static" local copy for
> this function, the assignment done only once, whereas the
> atoi=string.atoi in the body of the def gets executed every stinkin
> time, correct?

Absolutely. This cheap trick is frequently worth 30% to 60% in 
a moderate to tight loop.
 
> > For the all floats, all whitespace case, this would just be
> >  num = map(float, split(strLines[i])) 
> > and that might get you the speed you want.
> 
> okay. is there a big difference between the string.ato[if] and
> float/int?

I *think* I recall that int / float were somewhat faster, but I 
can't be sure. The major thing here is to get away from extra 
function calls. Flatten your loop as much as possible - 
function calls are (relatively) expensive.
 


- Gordon




More information about the Python-list mailing list