Timing: extracting from List VS extracting from Association
Recall that Mathematica is an interpreter language, so there is significant overhead in each single call. Only bulk access to data can show that List
s are significantly faster than Association
s.
n = 1000;
assoc = AssociationThread[Range[n], Range[n]^2];
list = Range[n]^2;
idx = RandomInteger[{1, n}, 100000];
Do[x = list[[i]], {i, idx}]; // AbsoluteTiming
Do[y = assoc[i], {i, idx}]; // AbsoluteTiming
a = Table[assoc[i], {i, idx}]; // AbsoluteTiming
b = Table[list[[i]], {i, idx}]; // AbsoluteTiming
c = list[[idx]]; // AbsoluteTiming
d = Lookup[assoc, idx]; // AbsoluteTiming
a == b == c == d
{0.047621, Null}
{0.044828, Null}
{0.029103, Null}
{0.001618, Null}
{0.000347, Null}
{0.008003, Null}
True
(Mathematica 11.3 for macos)