# Computation time changes depending on how many times the command is called

I just noticed something weird (or at least unexpected).

If I run this code:

a = RandomReal[{0, 1}, 10^7];
b = RandomReal[{0, 1}, 10^7];

AbsoluteTiming[a*b][[1]] (*0.037927*)
AbsoluteTiming[a*b][[1]] (*0.026529*)
AbsoluteTiming[a*b][[1]] (*0.021243*)
AbsoluteTiming[a*b][[1]] (*0.024168*)


the first computation takes always at least twice the time of the following ones. Why is that?

PS If I run this code:

Table[
a = RandomReal[{0, 1}, 10^7];
b = RandomReal[{0, 1}, 10^7];
{AbsoluteTiming[a*b][[1]],
AbsoluteTiming[a*b][[1]],
AbsoluteTiming[a*b][[1]],
AbsoluteTiming[a*b][[1]]}, {i, 1, 5}]


the effect I saw before disappears, and all the calculations take more or less the same time (this is even weirder):

{{0.023043, 0.023097, 0.020062, 0.020079}, {0.021496, 0.022427,
0.020621, 0.018961}, {0.022911, 0.023001, 0.021831,
0.020697}, {0.021581, 0.021491, 0.021912, 0.021389}, {0.020699,
0.021129, 0.020169, 0.020651}}


Anyone has any idea of what is going on?

EDIT: test on Mathematica 11.3 on macOs, later I'll try on my linux machine

EDIT 2: Surprisingly, I can't reproduce the effect on Mathematica 11.3 on linux

• You will get more stable timing results with RepeatedTiming. It executes the code several times an averages the timings. May 17, 2018 at 22:13
• But using RepeatedTiming erases the effect I'm seeing (if that's not just an artifact), because indeed it will repeat the computation and average, while what I see is that the first time the computation takes longer than the following ones. May 17, 2018 at 22:18
• Are you sure it's not caching the results? Try using ClearSystemCache[] between your repeated calls to AbsoluteTiming. May 17, 2018 at 23:02
• @BenKalziqi Nope, I just tried and I still see that effect. It's not very important for me, but I'm curious to know the reason now :) I also guess that in very computationally heavy problems this could make the difference! May 18, 2018 at 6:02
• I'm also curious to know if anyone can reproduce what I see. May 18, 2018 at 7:12

a := RandomReal[{0, 1}, 10^7];