strptime() in _strptime.py vs lib-dynload/time.so

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Dec 18 03:42:08 EST 2007


En Tue, 18 Dec 2007 04:45:25 -0300, <igor.tatarinov at gmail.com> escribió:

> On Dec 17, 8:01 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>> En Mon, 17 Dec 2007 01:53:24 -0300, <igor.tatari... at gmail.com> escribió:
>>
>> > actually, the C-version of strptime() is also getting called:
>> >     57765    0.960    0.000   13.940    0.000 :0(strptime)
>>
>> > that's pretty confusing. Does the Python version call the C-version
>> > (or the other way around)?
>>
>> The other way. The strptime function inside the time module  
>> (timemodule.c)
>> just calls the strptime function written in Python (_strptime.py).
>> I have no idea why it's so slow in your case.
>
> Thanks, that helps. Still I don't understand why the native (C)
> version isn't available.
> Perhaps, it's broken under Windows or something.
>
> In my test code, strptime() is called approx 60K times (5M times in
> the real code)
> and that takes 6 CPU secs. Thus, we get 100 usecs per call. That's
> much slower
> than running strptime from command line. Perhaps profiling slows
> things down.
>
> $ python -mtimeit -s 'from time import mktime, strptime'
> 'mktime(strptime("070501", "%y%m%d"))'
> 10000 loops, best of 3: 40.4 usec per loop
>
> An easy fix in my case is to avoid strptime() altogether:
>  python -mtimeit -s 'from time import mktime; s="070501"'
> 'mktime((int(s[0:2]), int(s[2:4]), int(s[4:6]),0,0,0,0,0,-1))'
> 100000 loops, best of 3: 8.6 usec per loop

I think that if your date format is simple enough, parsing it as above may  
be even faster than using the native strptime.

> It would be nice to have access to the native version of strptime()
> for performance reasons.

You can see the story of strptime at http://bugs.python.org/issue474274
Looks like a native strptime may not be always present, or unreliable.  
Perhaps if certain platform has strptime available and it's reliable and  
produces the right results, at configure time it could be detected and  
used instead of the pure Python implementation...

-- 
Gabriel Genellina




More information about the Python-list mailing list