strptime performance

George Trojan george.trojan at noaa.gov
Tue Nov 4 00:24:58 CET 2003


Is time.strptime() intrinsically slow and should be avoided whenever 
possible? I have the following code in my application:

def string2time(s):
    # Converts string %y%m%d%H%M to Unix time
    y = int(s[:2])+2000
    m = int(s[2:4])
    d = int(s[4:6])
    H = int(s[6:8])
    M = int(s[8:10])
    return time.mktime((y, m, d, H, M, 0, 0, 0, 0))
#    return time.mktime(time.strptime(s[:10], '%y%m%d%H%M'))

Output from pstats is:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     2215    0.030    0.000    0.030    0.000 
/awips/adapt/avnfps/CE/py/Avn.py:41(string2time)

When I use the commented out line instead, the code is about 60 times 
slower:

     1456    0.080    0.000    1.270    0.001 
/awips/adapt/avnfps/CE/py/Avn.py:41(string2time)
...    
   1456    0.020    0.000    0.770    0.001 
/usr/local/python2.3/lib/python2.3/_strptime.py:396(compile)
   1456    0.360    0.000    0.720    0.000 
/usr/local/python2.3/lib/python2.3/_strptime.py:374(pattern)

George





More information about the Python-list mailing list