small medium large xlarge

10 Apr 2015, 14:49
Iain Beeston (3 posts)

I’ve just finished reading chapter 2 of the beta, which has a section explaining that Date.parse() is slow and if you know the format it’s faster to use a regexp and manually construct a Date. However, I believe Date.parse() is partly slow because it has to dynamically determine the date format used in the string before interpreting it. I would be curious to see how Date.strptime() compares to the performance of a regexp. I suspect it might be even faster, and simpler to use as it returns a Date.

16 Apr 2015, 18:28
Alexander Dymo (9 posts)

Hi Iain,

Good catch! That’s indeed faster, ~4.x faster than Date.parse.

Even faster (7x) would be to avoid regular expressions at all, for example:

date = "2014-05-23"
Date.civil(date[0,4].to_i, date[5,2].to_i, date[8,2].to_i)

I’ll update this section. Thanks!

You must be logged in to comment