What is the fastest way to convert a string timestamp to an era using python?

I need to make a lot of talk from a string timestamp, for example "2012-09-08 12:23:33", in seconds, which is based on the time of an era. Then I need to get the time interval between two timestamp.I tried two different ways:

date1 = '2012-09-08' time2 = '12:23:33' timelist1 = map(int, date1.split('-') + time1.split(':')) date2 = '2012-09-08' time2 = '12:23:33' timelist2 = map(int, date2.split('-') + time2.split(':')) delta = datetime.datetime(*timelist2) - datetime.datetime(*timelist1) print delta.seconds 

The second way:

 date1 = '2012-09-08' time1 = '12:23:33' d1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") seconds1 = time.mktime(d1.timetuple()) .... seconds2 = time.mktime(d2.timetuple()) print seconds2-deconds1 

However, these two methods are not fast enough, because I have almost 100 million actions. Any suggestion?

+4
source share
1 answer

You will be much better off using the datetime.datetime.strptime() function, and then subtract the two results:

 import datetime date1, time1 = '2012-09-08', '12:23:33' date2, time2 = '2012-09-08', '12:23:33' dt1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") dt2 = datetime.datetime.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S") print (dt1 - dt2).total_seconds() 

Note that datetime.timedelta.seconds gives you the rest in seconds, there is also a .days attribute. Use .total_seconds() , it is much more convenient and saves your value to accept the value of .days .

Using the datetime.datetime.strptime() method, plus the timedelta methods, save most of the work in C code and should be faster.

Note that none of the methods is very fast due to the parsing step. Time Tests:

 >>> import timeit >>> def parse_datetime(): ... date1, time1 = '2012-09-08', '12:23:33' ... date2, time2 = '2012-09-08', '12:23:33' ... dt1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") ... dt2 = datetime.datetime.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S") ... (dt1 - dt2).total_seconds() ... >>> def parse_time(): ... d1 = time.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S") ... d2 = time.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S") ... seconds1 = time.mktime(d1) ... seconds2 = time.mktime(d2) ... seconds1 - seconds2 ... >>> timeit.timeit('test()', 'from __main__ import parse_datetime as test', number=10000) 0.6590030193328857 >>> timeit.timeit('test()', 'from __main__ import parse_time as test', number=10000) 0.7742340564727783 

where the second method uses only the time.strptime() function (which outputs the time set directly).

There are no faster ways to parse the date-time syntax that I know of.

+5
source

Source: https://habr.com/ru/post/1434359/


All Articles