Mathematica Asked on September 28, 2021
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?
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.
Correct answer by Sjoerd Smit on September 28, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP