Dear All,
I often use Accumulate, but found that it is very slow once you accumulate a list with units.
You would expect that adding 500 lengths up would take a fraction of a second:
AbsoluteTiming[Accumulate[Quantity[RandomReal[1, {500}], "Meters"]];]
{4.29581, Null}
However this takes a solid 4.3 seconds on my macbook pro retina!
Something without units goes very fast:
AbsoluteTiming[Accumulate[RandomReal[1, {500}]];]
{0.00002, Null}
Just a few micro-seconds!
Ok, so what if we get the number, get the magnitude, accumulate it, and give it back the units?
anums=bnums=Quantity[RandomReal[1,{500}],"Meters"];
AbsoluteTiming[
anums=Accumulate[anums];
]
AbsoluteTiming[
bnums=QuantityMagnitude[bnums,"Meters"];
bnums=Accumulate[bnums];
bnums=Quantity[bnums,"Meters"];
]
{4.30476,Null}
{0.032635,Null}
So over a 100x faster than the built-in Accumulate with units! I guess there is some checking done that slows down everything a lot (too much!)
Anyone another solution? Or some option to pass to Accumulate?