1
$\begingroup$

Say I run the following:

PowersRepresentations[4782969,4,2] 

and it takes about 2 minutes. If I call it again it takes only about 0.0005 seconds.

What determines this? If I was crazy how do I stop this and have it run much longer as in the first call?

Is there a list of functions that do this? Is there a criteria for a built-in function to run faster on subsequent calls?

$\endgroup$
11
  • 4
    $\begingroup$ Many functions in Mathematica have some sort of in-session caching of results for results corresponding to specific inputs. Another such function is FullSimplify. The purpose of this, unsurprisingly, is to trade memory usage for running time - that is getting repeated results faster while using a bit more memory in order to provide them quickly. $\endgroup$ Commented Feb 15, 2023 at 17:56
  • 3
    $\begingroup$ I don't believe providing an exhaustive list is possible because functions call other functions in the system... $\endgroup$ Commented Feb 15, 2023 at 18:02
  • 2
    $\begingroup$ @1729taxi nothing so obvious on Kirma's answer given that you didn't say so in your question. A bit more appreciation for the free help you are getting wouldn't harm anybody. Moving the goalpost on the other hand, no so nice. $\endgroup$ Commented Feb 15, 2023 at 18:19
  • 1
    $\begingroup$ Relying on timings of symbolic functions is often hard due to this - just restarting kernel gives a better picture of the situation. In the case of machine-precision evaluation it tends to be much more predictable. $\endgroup$ Commented Feb 15, 2023 at 18:46
  • 2
    $\begingroup$ @kirma and 1729: if you suspect caching is involved somewhere in your timing tests, ClearSystemCache[] might be useful in checking. $\endgroup$ Commented Feb 15, 2023 at 20:59

1 Answer 1

6
$\begingroup$

Try unsetting the internal memoization:

NumberTheory`PowersRepresentationsDump`powersRepresentation[4782969, 4, 2] =. 

When PowersRepresentations[4782969,4,2] is first called, it autoloads some code and saves the result in the internal function. The code for powersRepresentation may be inspected after the package it loaded.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.