5
$\begingroup$

I was developing a program and noted that Grad[0,{x,y,..}] was taking too much time to return zero vector (I do this many times, so the performance matters to me). I applied Trace to it and saw that Grad is actually written in the Wolfram Language. I already knew that some built-in functions are, and because of that, they may be slower.

Is there any command to get a list of built-in functions that are written in WL, so that one can know what to avoid when seeking performance?

$\endgroup$
2
  • 2
    $\begingroup$ If you think all functions written in the language have poor performance then I can't help but wonder why you bother to use it? $\endgroup$
    – Jason B.
    Feb 22 at 15:13
  • $\begingroup$ Not an answer to what I asked, but maybe I didn't expressed the point clearly. If there are faster alternatives, why would I use the slow ones in code where performance matters? Grad is constructed on top of the WL which is written in C, so it is obviously slower. If you have any doubts run "D[0, {{x, y}}] // Trace" and "Grad[0, {x, y}] // Trace" to see the difference in steps that the system does. I don't want to apply trace every time to figure it out which ones are the faster, so a list of the ones which are slower is welcome. $\endgroup$
    – Felipe
    Feb 22 at 15:22

1 Answer 1

6
$\begingroup$

I think your approach of "avoiding" certain functions will not really help you, and you should optimize your code performance in a more targeted, case-by-case manner.

However, to answer your question, you can use System`Private`HasDownCodeQ[sym] to determine whether there is a kernel function associated with a symbol sym. Note that there are functions which may first do some preprocessing in Wolfram Languange, and only then call a kernel function, so you might mistakenly consider them as """fast""", even though they """may not be""".

System`Private`HasDownCodeQ[Integrate]
(* True *)

System`Private`HasDownCodeQ[Grad]
(* False *)

{#, With[{sym = Symbol[#]}, System`Private`HasDownCodeQ[sym]]} & /@ 
  RandomSample[Names["*"], 10] // Grid
(*  StarData            False
    PartLayer           False
    Previous            False
    Position            True
    ReverseSortBy       True
    WeierstrassSigma    False
    Precedes            False
    DirichletCharacter  False
    VideoMapList        False
    GridBoxSpacings     False *)

res = ParallelMap[{#, With[{sym = Symbol[#]}, System`Private`HasDownCodeQ[sym]]} &, 
 Names["*"]]
Cases[res, {sym_, True} :> sym] // Short
(* {Abort, AbortProtect, Abs, <<1599>>, $DisplayFunction, $SoundDisplayFunction} *)

Note that some of the results for non-function symbols in my last list are wrong, because they get evaluated prematurely. For example, it says $DisplayFunction has a kernel function, even though it doesn't have it. This is because $DisplayFunction is evaluated to Identity. I haven't been able to come up with a working code for defering the premature evaluation in Map.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you, of course it may vary from case to case. But this information can at least give me a heads up. $\endgroup$
    – Felipe
    Feb 22 at 16:30

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.