Message Boards Message Boards

2
|
3284 Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

same as DateDifference, but faster?

Posted 12 years ago
Given that:
In[201]:= DateDifference[{2011, 1, 19}, {2011, 1, 18}]
Out[201]= -1
any alternate/creative methods to do the same thing faster?
In[195]:= Timing[Do[DateDifference[{2011, 1, 19}, {2011, 1, 18}], {1000}];]
Out[195]= {1.934, Null}
POSTED BY: Josh Lietz
2 Replies
If you're only interested in number of days and the dates in question don't go through any historical calendar changes, you could use AbsoluteTime to convert to seconds between and then convert seconds to days:
In[11]:= DateDifference[{2011, 1, 19}, {2011, 1, 18}]
Out[11]= -1
In[12]:= Timing[Do[DateDifference[{2011, 1, 19}, {2011, 1, 18}], {1000}];]
Out[12]= {2.074813, Null}
In[13]:= newDateDiff[d1_, d2_]:= Round[(AbsoluteTime[d2] - AbsoluteTime[d1])/(24*60*60)]
In[14]:= newDateDiff[{2011, 1, 19}, {2011, 1, 18}]
Out[14]= -1
In[15]:= Timing[Do[newDateDiff[{2011, 1, 19}, {2011, 1, 18}], {1000}];]
Out[15]= {0.015600, Null}
POSTED BY: Vitaliy Kaurov
There was a detailed discussion about this on StackExchange.  You'll find fast implementations in the answers there.

There were also some reports that this function got slower in 9 compared to 8.

I haven't personally tested any of this.
POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract