Python datetime.strptime() Eating lots of CPU Time

后端 未结 4 1148
栀梦
栀梦 2021-01-04 18:09

I have some log parsing code that needs to turn a timestamp into a datetime object. I am using datetime.strptime but this function is using a lot of cputime according to cPr

4条回答
  •  一向
    一向 (楼主)
    2021-01-04 18:53

    What's a "lot of time"? strptime is taking about 30 microseconds here:

    from datetime import datetime
    import timeit
    def f():
        datetime.strptime("01/Nov/2010:07:49:33", "%d/%b/%Y:%H:%M:%S")
    n = 100000
    print "%.6f" % (timeit.timeit(f, number=n)/n)
    

    prints 0.000031.

提交回复
热议问题