# Very Slow Accumulate of random time segments

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. ?

• 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. Mar 12, 2018 at 19:05
• @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. Mar 12, 2018 at 19:23

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[
"Seconds"];


Compare DatePlus

AbsoluteTiming[FoldList[DatePlus[##] &, FromUnixTime@0, dtimqr];]

{0.00311076, Null}

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

{9.56173, Null}


Hope this helps.