Scenario - a series of distances between GPS points. (Some overhead in getting there too). Fold the list for accumulated distances along a path. How costly are units? About 3 orders of magnitude after realizing that 0.0 is converted on every function call.
(* series of distances between points, will accumulate the path length *)
points = 30000;
distances =
Table[Quantity[RandomReal[{10., 100.}], "Meters"], {points}];
Timing[
path = FoldList[Plus, 0.0, distances];
]
(* Lets not forget to put the same units on that zero - could be the source of a gotcha *)
distances =
Table[Quantity[RandomReal[{10., 100.}], "Meters"], {points}];
Timing[
path = FoldList[Plus, Quantity[ 0.0, "Meters"], distances];
]
(* what if I stick to unit-less data? *)
rawdistances = Table[RandomReal[{10., 100.}], {points}];
Timing[
path = FoldList[Plus, 0.0, rawdistances];
]
{28.984986, Null}
{7.597249, Null}
{0.015600, Null}
What about Accumulate? (Only 1000 points - far worse)
points = 1000;
distances =
Table[Quantity[RandomReal[{10., 100.}], "Meters"], {points}];
Timing[
path = Accumulate[distances];
]
{23.868153, Null}