4
$\begingroup$

Even for short list (15 elem) of random time segments of DateObjects, Accumulate consumes unexpectedly long times. But, when time segments are not random, or are Accumulated as a Quantity, prior adding to DateObject (conversion to DateObject), there are no such long computation long times. For list of length few dozens computation time approaches infinity. Eg.

Delta times before accumulation:

dtimqr = Quantity[IntegerPart[RandomVariate[GammaDistribution[9, 0.1], 15]*86400], "Seconds"];
dtimq = Quantity[Table[135000, 15], "Seconds"];

Accumulation as a DateObjects:

{Timing[ftimr = Accumulate@({FromUnixTime[0]}~Join~dtimqr);], 
 Timing[ftim = Accumulate@({FromUnixTime[0]}~Join~dtimq);]}
(* {{14.0625, Null}, {0.0625, Null}} *)

{Timing[ftimrP=FromUnixTime[0]+Accumulate@({Quantity[0, "Seconds"]}~Join~dtimqr);],
 Timing[ftimP=FromUnixTime[0]+Accumulate@({Quantity[0, "Seconds"]}~Join~dtimq);]}
(* {{0.0625, Null}, {0.046875, Null}} *)

Is there reasonable explanation for such a long computation time when Accumulating lists with random dateObjects, as short as 15 elements. ?

$\endgroup$
2
  • 2
    $\begingroup$ There's a lot of overhead involved in carrying around and manipulating DateObject[]s. I would suggest keeping things in UnixTime[] or AbsoluteTime[], and convert to explicit dates only when you need to display them. $\endgroup$ Mar 12, 2018 at 19:05
  • $\begingroup$ @J.M. but why when DateObjects are random, only? I have few thousands objects from TimeSeries data, and if this is the case, this will complicate my code a lot. $\endgroup$
    – Dragutin
    Mar 12, 2018 at 19:23

1 Answer 1

4
$\begingroup$

You should use Date Operations functions as found in the Date & Time guide to operate on DateObjects. For example, you may use DatePlus with FoldList.

SeedRandom[978];
dtimqr = Quantity[
   IntegerPart[RandomVariate[GammaDistribution[9, 0.1], 15]*86400], 
   "Seconds"];

Compare DatePlus

AbsoluteTiming[FoldList[DatePlus[##] &, FromUnixTime@0, dtimqr];]
{0.00311076, Null}

to Accumulate

AbsoluteTiming[Accumulate@({FromUnixTime[0]}~Join~dtimqr);]
{9.56173, Null}

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.