This can be dealt with faster:
ClearAll[f]; SetAttributes[f, HoldFirst] f[x_] = 0; f[s_Symbol] /; OwnValues[s] =!= {} = 1;
To compare, here is the one you used:
ClearAll[ff]; SetAttributes[ff, HoldFirst] ff[x_] = 0; ff[s_] /; Head[s]
Now:
In[30]:= f /@ Range@1 *^6; // Timing Out[30]= {0.719, Null} In[56]:= ff /@ Range@1 *^6; // Timing Out[56]= {1.25, Null}
This will be more efficient if your arguments are mostly non-characters, and the reason why this happens faster is that you can still use the _Symbol template to filter them. For character lists only, this can be slower:
symbTest = Table[ToExpression["sym" <> ToString[i]], {i, 100000}]; MapIndexed[If[OddQ[ First@
Leonid Shifrin
source share