Reputation: 3163
I'm building a generic custom strToDatetime(string)
function. The date string may be in some different formats. The 2 most popular alternatives seem datetime.strptime(string, format)
and dateutil.parser(string)
. It seems datetime.strptime()
requires a format and dateutil.parser()
does not, so the possible solutions seem to be:
datetime.strptime()
dateutil.parser()
Is this correct? Alternative 1 (harder and may require maintenance in the future) has advantages, such as performance?
Upvotes: 2
Views: 2750
Reputation: 375435
I would always go with the simplest (dateutil.parser), someone has always done the work for you and it's less likely to spit out an error at a malformed (according to your format) date.
Of course, sometimes you will want it to throw an error at a malformed, perhaps ambiguous date, and in this case you should use strptime
!
I called parse(d)
and datetime.datetime.strptime(d, f)
each 100,000 times.
parse(d) took 5.62201309204 seconds
datetime.datetime.strptime(d, f) took 1.78140687943 seconds
(where d = '11-02-1980' and f = '%m-%d-%Y')
It seems that if you know the precise date format then strptime
is around 3 times faster, granted this is not a very scientific experiment but I think it gives a good indication.
So is this slight speed improvement worth the additional unnecessary complication/headache? That's up to you (but probably not).
Upvotes: 1
Reputation: 1121486
The parse()
method of dateutil is very flexible and will parse almost anything you throw at it.
However, because of that flexibility, if your input is limited to a certain number of patterns, custom code that checks for those patterns then uses datetime.datetime.strptime()
could easily beat it.
Since this depends entirely on the number of patterns you need to test for, the only thing you can do is measure which one will be faster for your specific usecases.
Upvotes: 4