Obviously (?) I have a problem with precision or something but MMA seems to be able to take the mean squared of a 1e7 long series of random reals in literally no time, unless I use RepeatedTiming which reports that the average time to do the mean squared is 10 nanoseconds. Since this machine is not Deep Thought, I'd would love to know what I have done wrong - and how to do it right.
Since my series is very long I typically use a compiled function to generate it, but for those without a C compiler, I have included an uncompiled definition that can be substituted (with appropriate mods), though it takes ~30s to generate 1e7 rands on this machine.
randCList[seed_Integer, length_Integer] :=
Block[{}, SeedRandom[seed];
Table[RandomReal[{$MinMachineNumber, Pi - $MinMachineNumber}], {n,
1, length}]];
randCListCompiled =
Compile[{{seed, _Integer}, {length, _Integer}, {min, _Real}, {max, _Real}}, Module[{n}, SeedRandom[seed];
Table[RandomReal[{min, max}], {n, 1, length}]],
CompilationTarget -> "C", "RuntimeOptions" -> "Speed"];
Here are the timing tests - note that I used different series and different seeds in an attempt to avoid any cacheing issues. Recall that Timing returns a two element list whose first element is the time in seconds for the operation timed (second is result, if any; otherwise Null).
First[Timing[aSeries = randCListCompiled[45872, 10000000, $MinMachineNumber,Pi - $MinMachineNumber]]]
First[Timing[bSeries = randCListCompiled[45873, 10000000, $MinMachineNumber, Pi - $MinMachineNumber]]]
Timing[Mean[aSeries]^2]
Timing[Mean[bSeries]^2]
cSeries = randCListCompiled[45874, 10000000, $MinMachineNumber, Pi - $MinMachineNumber];
RepeatedTiming[100, Mean[cSeries]^2]
And the results...
0.514803 (* Timing for generation of aSeries *)
0.499203 (* Timing for generation of bSeries *)
{0., 2.46784} (* Timing for mean squared of aSeries *)
{0., 2.46767} (* Timing for mean squared of bSeries *)
{1.*10^-8, 100} (* Average timing for mean squared of cSeries, 100 iterations *)
I checked the precision of the "0." above and it said MachinePrecision, so really zero.
Back of envelope calculation says according to RepeatedTiming that MMA is summing 1e7 random reals in 10ns, i.e. each addition is about 1e-15s (1 femtosecond).
I am not inclined to believe this ;) What's going on?
Timing
is a legacy function from the single-core single-CPU days.AbsoluteTiming
was introduced in V5, and I don't know of case when I would useTiming
instead ofAbsoluteTiming
. Maybe there is one.... $\endgroup$