Although announced for 10.0.2 the functionality below works from 10.0.0 onward.
Although apparently undocumented Replace
and ReplaceAll
work with Association
and this combination is considerably faster than Map
. Further it appears to be somewhat faster than using a Dispatch
table as well.
Update: it seems Lookup
is faster still. See additional timing result.
Setup:
rules = Thread[Range @ 26 -> CharacterRange["a", "z"]];
asc = <|rules|>;
d1 = Dispatch @ rules;
d2 = Dispatch @ asc;
Note another undocumented functionality: you can Dispatch
an Association
.
Test:
{{11, 13, 2}, {19, 23, 16}} /. asc
Replace[{{11, 13, 2}, {19, 23, 16}}, asc, {2}]
{{"k", "m", "b"}, {"s", "w", "p"}}
{{"k", "m", "b"}, {"s", "w", "p"}}
Timings:
time = Function[x, NumberForm[x // Timing // First // AbsoluteTiming, {4, 3}], HoldAll]
m = RandomInteger[{1, 26}, {2500, 2500}];
Map[asc, m, {2}] // time
m /. asc // time
Replace[m, asc, {2}] // time
Replace[m, d1, {2}] // time
Replace[m, d2, {2}] // time
Lookup[asc, #] & /@ m // time
{1.318, 1.248}
{0.843, 0.827}
{0.477, 0.468}
{0.576, 0.562}
{0.576, 0.562}
{0.380, 0.359}
Notes:
Replace
at levelspec {2}
is almost three times faster than the equivalent Map
ReplaceAll
is not as fast but still faster than Map
The origin of the Dispatch
table appears to have no effect on performance
Although not included in the example relative timings hold with few or many rules
Map
anAssociation
like that when the natural thing to use here isReplace
orReplaceAll
. $\endgroup$Association
object to work with and converting to Rules and then Dispatch is slow (and pointless). In fact my experimentation indicatesAssociation
is faster than `Dispatch in this application. $\endgroup$Association
is not included in the documentation forReplace
that I can see. $\endgroup$Association
objects, I thought I saw somewhere that almost all functions were updated to work withAssociation
s so I just used them, didn't know this was not documented. Thanks. $\endgroup$