A custom function is too slow when I first time to run it

I make a function like this

LookupOptionFunction[option_] :=
Select[Select[Names[“System`*”], StringFreeQ[#1, “$”] &],
KeyExistsQ[Options[ToExpression[#1]], option] &]

Its can help uw select some function contain a certain option.But when I start the Mathematica and first time to use it,it will be very very slow like

LookupOptionFunction[SameTest] // AbsoluteTiming

{58.6652,{AntihermitianMatrixQ,AntisymmetricMatrixQ,Complement,ContainsAll,ContainsAny,ContainsExactly,ContainsNone,ContainsOnly,Entropy,FindTransientRepeat,FixedPoint,FixedPointList,HermitianMatrixQ,Intersection,KeyComplement,NormalMatrixQ,OrthogonalMatrixQ,SymmetricMatrixQ,TestReport,Union,UnitaryMatrixQ,VerificationTest}}

I Can’t Stand it cost my almost 1 minute.
but when we run it second time or afterwards.It will very speed.

LookupOptionFunction[SameTest] // AbsoluteTiming

{0.520688,{AntihermitianMatrixQ,AntisymmetricMatrixQ,Complement,ContainsAll,ContainsAny,ContainsExactly,ContainsNone,ContainsOnly,Entropy,FindTransientRepeat,FixedPoint,FixedPointList,HermitianMatrixQ,Intersection,KeyComplement,NormalMatrixQ,OrthogonalMatrixQ,SymmetricMatrixQ,TestReport,Union,UnitaryMatrixQ,VerificationTest}}

It is not enough one second.What something I have meet?Or any method can do this thing?

I’m use it in 10.4 and 10.3.1 will be in a same situation.

=================

  

 

I just ran your code on 10.4 (which I think you did as well) and I’m not seeing the speed-up at all. The first run was 70 seconds, the second 64. And I’m also getting “iteration limit exceeded” message (although the result seems correct).
– Martin Ender
Mar 23 at 12:43

  

 

@MartinBüttner I have update my condition.Sounds your situation is worse than me.:)
– yode
Mar 23 at 12:47

=================

1 Answer
1

=================

$Version

(* “10.4.0 for Mac OS X x86 (64-bit) (February 26, 2016)” *)

The iterations are using most of the time. Set a lower $IterationLimit (lowest allowable value is 20).

lookupOptionFunction[option_] := Block[{$IterationLimit = 20},
Select[Names[“System`*”],
StringFreeQ[#1, “$”] && KeyExistsQ[Options[ToExpression[#1]], option] &]]

lookupOptionFunction[SameTest] // AbsoluteTiming

(* $IterationLimit::itlim: Iteration limit of 20 exceeded. >>

{8.42141, {“AntihermitianMatrixQ”, “AntisymmetricMatrixQ”, “Complement”,
“ContainsAll”, “ContainsAny”, “ContainsExactly”, “ContainsNone”,
“ContainsOnly”, “Entropy”, “FindTransientRepeat”, “FixedPoint”,
“FixedPointList”, “HermitianMatrixQ”, “Intersection”, “KeyComplement”,
“NormalMatrixQ”, “OrthogonalMatrixQ”, “SymmetricMatrixQ”, “TestReport”,
“Union”, “UnitaryMatrixQ”, “VerificationTest”}} *)

The second call is faster presumably because of some caching.

lookupOptionFunction[SameTest] // AbsoluteTiming

(* $IterationLimit::itlim: Iteration limit of 20 exceeded. >>

{0.548943, {“AntihermitianMatrixQ”, “AntisymmetricMatrixQ”, “Complement”,
“ContainsAll”, “ContainsAny”, “ContainsExactly”, “ContainsNone”,
“ContainsOnly”, “Entropy”, “FindTransientRepeat”, “FixedPoint”,
“FixedPointList”, “HermitianMatrixQ”, “Intersection”, “KeyComplement”,
“NormalMatrixQ”, “OrthogonalMatrixQ”, “SymmetricMatrixQ”, “TestReport”,
“Union”, “UnitaryMatrixQ”, “VerificationTest”}} *)

To identify which symbol is exceeding the iteration limit:

lookupOptionFunction2[option_] := Block[{$IterationLimit = 20},
Select[Names[“System`*”],
Check[StringFreeQ[#1, “$”] &&
KeyExistsQ[Options[ToExpression[#1]], option],
Print[StringForm[““ exceeded iteration limit”, #1]]] &]]

lookupOptionFunction2[SameTest] // AbsoluteTiming

(* $IterationLimit::itlim: Iteration limit of 20 exceeded. >>

AllowTransliteration exceeded iteration limit

{0.559639, {“AntihermitianMatrixQ”, “AntisymmetricMatrixQ”,
“Complement”, “ContainsAll”, “ContainsAny”, “ContainsExactly”,
“ContainsNone”, “ContainsOnly”, “Entropy”, “FindTransientRepeat”,
“FixedPoint”, “FixedPointList”, “HermitianMatrixQ”, “Intersection”,
“KeyComplement”, “NormalMatrixQ”, “OrthogonalMatrixQ”,
“SymmetricMatrixQ”, “TestReport”, “Union”, “UnitaryMatrixQ”,
“VerificationTest”}} *)

Options[AllowTransliteration] // AbsoluteTiming

(* $IterationLimit::itlim: Iteration limit of 4096 exceeded. >>

{77.3179, {}} *)

This exceeds the iteration limit even though it has no options.

  

 

Can we found which function result the $IterationLimit surpass 20?
– yode
Mar 23 at 13:35