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:

 a = RandomReal[{0, 1}, 10^7];
 b = RandomReal[{0, 1}, 10^7];
  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

  • $\begingroup$ You will get more stable timing results with RepeatedTiming. It executes the code several times an averages the timings. $\endgroup$ May 17, 2018 at 22:13
  • $\begingroup$ 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. $\endgroup$
    – Fraccalo
    May 17, 2018 at 22:18
  • 2
    $\begingroup$ Are you sure it's not caching the results? Try using ClearSystemCache[] between your repeated calls to AbsoluteTiming. $\endgroup$ May 17, 2018 at 23:02
  • $\begingroup$ @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! $\endgroup$
    – Fraccalo
    May 18, 2018 at 6:02
  • $\begingroup$ I'm also curious to know if anyone can reproduce what I see. $\endgroup$
    – Fraccalo
    May 18, 2018 at 7:12

1 Answer 1


I think you are seeing the effects of caching. In your code you are multiplying the exact same (random) numbers each time, and I suspect Mathematica remembers (some part of this) calculation. If you change the definitions to

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

so that now you multiply by different a's and b's each time, the timing is more consistent.

  • $\begingroup$ Yes, that's probably what is happening! I just checked and using the := gets rid of the effect. However, it's still weird that ClearSystemCache didn't have any effect... $\endgroup$
    – Fraccalo
    May 18, 2018 at 14:51
  • $\begingroup$ Machine number products are not likely to be cached. Might be the effect of some memory movement on the first call. Also note the random generation time is somewhat larger than the multiplication so that will tend to have a stabilizing effect. $\endgroup$ May 18, 2018 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.