Constructing expressions using Sum vs Array[... Plus] vs Plus@@Table[...]
This is something that you could have tested yourself, quite easily. I sugest the use of RepeatedTiming
for more reliable measurements.
BarChart[
{
First@RepeatedTiming[
Sum[f[i], {i, 1, 100}]
],
First@RepeatedTiming[
Array[f, 100, 1, Plus]
],
First@RepeatedTiming[
Total@Table[f[i], {i, 1, 100}]
],
First@RepeatedTiming[
Plus @@ Table[f[i], {i, 1, 100}]
],
First@RepeatedTiming[
Plus @@ (f /@ Range[100])
],
First@RepeatedTiming[
Total[f /@ Range[100]]
]
} 10^6
, ChartLabels -> {
"Sum",
"Array[_,Plus]",
"Total@Table",
"Plus@@Table",
"Plus@@Range",
"Total-Range"
}
, PlotTheme -> "Scientific"
, AspectRatio -> 1/2
, FrameLabel -> {"Method", "Time \[Mu]s"}
, ImageSize -> 600
]
Sum
isn't really for constructing expressions: it's primarily for finding an analytic formula that represents a sum. It's mostly useful when bounds are unknown or infinite:
Sum[1/2^i, {i, 1, n}]
(* 2^-n (-1 + 2^n) *)
Mere expression construction can't do this.