In playing with and comparing the various answers to my question here, I tried applying Timing[] to see how fast things ran, but it seemed that some of the answers (but not all of them) were a lot faster when run for a second time. How can I make sure that there isn't some kind of caching of intermediate results throwing off the Timing[]?
|
|
|||||||
|
|
Completely restarting the kernel will of course work. If we don't restart the kernel, then we need to clear all caches. The caches used for symbolic and some numeric calculations can be cleared using
I do not know if there are any other caches as well, not affected by this. |
|||
|
|
When experimenting with Mathematica's caching, sometimes it can be clearer to look at memory usage rather than timing. When looking at individual functions, Mathematica is able to measure memory much more accurately than timing. The are a number of reasons why timing accuracy is off, such as the system clock resolution and fluctuations in the system use while testing. For more of an example check out the answer to this question. When using One call
Results:
Iterating over the call 10 times. Not clearing the cache. FYI I am using map to collect the values for me.
Results:
Clearly we are seeing caching going on. Iterating over the call 10 times. Clearing the cache and m value.
Results:
Alternatively using
Results:
I have not been able to explain the difference in memory used, 42368 vs 42304 vs 42072. Maybe
And:
Result in:
|
|||||||||||||
|
|
It's just a shot in the wild, but I would guess that you can switch off the caching with appropriate values to the corresponding system options:
It's hard to say whether playing with these will in fact give more reliable results, I could well imagine that some stuff just gets very slow. But it's probably worth a try. A completeley different approach that I have used before is to ensure that there is nothing that can be cached, which often but not always is possible. One way is to achieve this is to use some randomness in the code you want to test, see e.g. my answer to this question. |
|||||||||||
|