Sum of all matrix entries
You can use Total
. Here's a matrix of the given size:
m = RandomReal[1, {10000, 10000}];
And here's the output of Total:
Total[m, 2] // AbsoluteTiming
{0.041005, 4.99984*10^7}
Here is another solution that performs similarly to Total
:
v = ConstantArray[1, 10000];
m = RandomReal[1, {10000, 10000}];
v.m.v // AbsoluteTiming
{0.040816, 4.99968*10^7}
Total[m, 2] // AbsoluteTiming
{0.041774, 4.99968*10^7}
The reason that these solutions are fast is that they use vectorization. Using Compile
is not quite as fast, especially not when compiling loops:
sum = Compile[{{m, _Real, 2}},
Sum[Compile`GetElement[m, i, j], {i, Length[m]}, {j, Length[m]}],
CompilationTarget -> "C",
RuntimeOptions -> "Speed"
];
sum[m] // AbsoluteTiming
{0.092583, 5.00016*10^7}
sum = Compile[{{m, _Real, 2}}, Module[{tot},
tot = 0.;
Do[
Do[
tot += Compile`GetElement[m, i, j],
{i, j + 1, Length[m]}
],
{j, Length[m] - 1}
];
2 tot + Tr[m]
],
CompilationTarget -> "C",
RuntimeOptions -> "Speed"
]
sum[m] // AbsoluteTiming
{1.1837, 4.99976*10^7}