Message Boards Message Boards

1 Reply
3 Total Likes
View groups...
Share this post:

Pattern match speed question

Posted 12 years ago
If f takes 2 arguments, is either of these pattern match forms faster?
f[x_,___] := expr
f[x_,_] := expr 
POSTED BY: Joel Klein
Best not to use a BlankNullSequence unless you have to because the pattern matcher will spend forever trying to match anything you throw at it. Not to mention, if 2 args is all f takes, you would need an additional check on the length in order to determine if it has the proper number of arguments.
f[x_, ___] := 1;
g[x_, _] := 1;
In[14]:= args = Sequence @@ Range[10^5];
In[15]:= Do[f[args], {1000}] // AbsoluteTiming
Out[15]= {3.135620, Null}
In[16]:= Do[g[args], {1000}] // AbsoluteTiming
Out[16]= {1.934412, Null}

POSTED BY: Andy Ross
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract