# performance tuning – Why is extracting values from an Association with 1 level slower than extracting values from a list of depth 1?

I am not looking for a solution or work-around, but an explanation.

Here are six examples of doing a trivial operation on an association and a list.

Initially, I was curious about the cost of calling a function for which the matched pattern is a large association:

``````rAssoc = AssociationMap(RandomReal({0, #}) &, Range(10^7));
fA(r_) := Total(r)
gA() := Total(rAssoc)

AbsoluteTiming(fA(rAssoc);) (*about 3.38 seconds*)
AbsoluteTiming(gA();)  (*slightly longer 3.42, expense of passing association not large*)
``````

What about doing the same thing for a list:

``````rList = RandomReal({0, #}) & /@ Range(10^7);
fL(r_) := Total(r)
gL() := Total(rList)

AbsoluteTiming(fL(rList);) (*about .005 seconds*)
AbsoluteTiming(gL();)  (*about .007 seconds*)
``````

The list is much much faster. Why?

There is some discussion in this April 2018 StackExchange:

Let’s and see if the same holds for an unpacked array:

``````Needs("Developer`")
PackedArrayQ(rList) (*True*)

AbsoluteTiming(rListUnpacked = FromPackedArray(rList);) (*0.13 seconds*)
fLU(r_) := Total(r)
gLU() := Total(rListUnpacked)

AbsoluteTiming(fL(rListUnpacked);) (*0.57 seconds*)
AbsoluteTiming(gLU();) (*0.58 seconds*)
``````

It appears that the culprit may be Values:

``````AbsoluteTiming(Values(rAssoc);) (*3.37 seconds*)
``````

I am curious why extracting the values for the Association is slower.

Posted on Categories Articles