I was wondering, why is extracting from an Association
slightly faster than extracting from a List
?
assoc = Association[Table[i -> i^2, {i, 1000}]];
list = Table[i^2, {i, 1000}];
list[[10]] // RepeatedTiming
assoc[10] // RepeatedTiming
(* I'm not posting outputs because of the variance but the 2nd is usually faster *)
I would have expected nothing to be faster than extraction from a List
since it should be just a memory access vs a more computationally-expensive lookup for Association
.
MMA 11.2 Linux.
ulst = Developer`FromPackedArray[list]
is even slower. Huh. $\endgroup$list[[#]] & /@ Range[100]
is 9 times faster on my machine thanassoc /@ Range[100]
. $\endgroup$Part
is faster because of auto-compilation. ChangeRange[100]
toRange[99]
and association lookup becomes almost precisely 2x faster than list indexing. I wonder if it has any significance that the factor is almost precisely 2. $\endgroup$Lookup[assoc, 10]
is consistently a little faster thanassoc[10]
for me. $\endgroup$