If we want to save some outputs of the function, we often use the memoization pattern as follows,
f[x_]:=f[x]=x
f/@Range[10^5]
If I call this function many times for different arguments, Mathematica has to first find the best pattern defined so far i.e., choose the right overload. Is there any performance guarantee if there are $N$ overloaded functions in terms time complexity? As we now have Association, we have another natural way to memoize even though using Association looks uglier. I also want to know which way is more efficient given that there are $N$ overloaded functions with the same name.
Associationhas constant access time so my belief (without any further research or justification) is thatAssociationwould be the fastest for even moderately sized $N$. – IPoiler Sep 24 '15 at 18:55Associationcertainly provides attractive time complexity however in terms of usability, I still think the conventional memoization pattern is more readable and easier. So, if the conventional way is still reasonably fast, I would like to stick to using it. That's the whole point of my question. – Sungmin Sep 24 '15 at 19:11DownValuesandAssociation. For non-pattern arguments,DownValuesstore results in a hash-table. If the number of entries becomes really large, you may notice that lookup becomes slower, but it should not be more than a factor of 2 or so, for realistic number of entries. As for ugliness, here I have suggested a solution based on pure functions, which might be ok, and has additional advantage overDownValues-based method: automatic garbage collection. – Leonid Shifrin Sep 24 '15 at 20:14