14
$\begingroup$

It seems like there is some sort of evaluation overhead related to the ResourceFunction wrapper that I'd like to avoid. This is especially pronounced for functions with attributes. Take, for example, SymbolQ, which is essentially a wrapper for a built-in function from the Developer context:

Attributes[mySymbolQ] = {HoldAllComplete};
mySymbolQ[x_] := Developer`HoldSymbolQ[x];
x = 1;
ResourceFunction["SymbolQ"][x] // RepeatedTiming
mySymbolQ[x] // RepeatedTiming

{0.00068, True}

{5.2*10^-7, True}

Is there an easy way to avoid this slowdown without having to copy the source code from the resource function completely?

$\endgroup$

1 Answer 1

25
$\begingroup$

You can access the local version of the function directly with ResourceFunction["SymbolQ", "Function"] to avoid the evaluation semantics associated with ResourceFunction. The easiest way to do this, is to "clone" the function into a symbol convenient for you:

symbolQ = ResourceFunction["SymbolQ", "Function"];
x = 1;
ResourceFunction["SymbolQ"][x] // RepeatedTiming
symbolQ[x] // RepeatedTiming

{0.00076, True}

{5.*10^-7, True}

This also avoids problems that can occur when you clone the function name naively. See, e.g., this answer.

$\endgroup$
2
  • $\begingroup$ Good answer. Related: Is there a way to then persistently store symbolQ, perhaps using Put or Save, so that it could be used without any internet access in another notebook? (Perhaps on an airplane or in my remote lair?) Please show both proper "put" and "get". $\endgroup$ Commented Jul 28, 2020 at 23:22
  • 1
    $\begingroup$ @PaulCommentary Once you use a repository function once, you can use it without internet connection in the future. The first time you use it, a local copy will be saved automatically. You can also download the source notebook from the website (top right button). You can then open this and click "Deploy" -> "Locally on this computer" on the orange top bar. This will also save a local copy of the function. $\endgroup$ Commented Jul 29, 2020 at 7:15

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.