NumericArray is a relatively new option in Mathematica. It is certainly clear that this kind of data saves memory. But what about performance of access to the array?
I am currently engaged in mass computing on discretized mesh regions. To save memory (and as a greeting to new features in Mathematica) I decided to use numeric arrays.
But before doing that, one should have checked their performance!
Moreover, Association
have been around for a long time, but I didn’t really trust them (-_-)
They’re pretty vivid, but I was afraid they were slowing down access to data.
So let's create some (not very) big arrays:
arr1 = RandomReal[{0, 1}, {1000, 1000}];
arr2 = RandomInteger[{1, 10}, {1000, 1000}];
Put them in this wrapper:
testData = <|
"Ints" -> NumericArray[arr2, "Integer8"],
"Reals" -> NumericArray[arr1, "Real32"]
|>;
Firstly about memory:
ByteCount /@ {testData["Ints"], arr2}
{1000208, 8000208}
ByteCount /@ {testData["Reals"], arr1}
{4000208, 8000208}
A least 2x economy.
Now apply some operations to arrays.
Timing[Table[
testData["Ints"][[i]] + testData["Ints"][[j]], {i,
Length@testData["Ints"]}, {j, i}];]
{3.70313, Null}
Timing[Table[arr2[[i]] + arr2[[j]],
{i, Length@arr2}, {j, i}];]
{6.57813, Null}
Surprisingly access to NumericArray
inside Association
2x faster, than "direct"!
Well, what about Reals?
Timing[Table[
testData["Reals"][[i]] + testData["Reals"][[j]], {i,
Length@testData["Reals"]}, {j, i}];]
{4.88134, Null}
Timing[Table[arr1[[i]] + arr1[[j]], {i, Length@arr1}, {j, i}];]
{7.01563, Null}
Not so cool, but anyway faster than "direct" access.
So, I vote to NumericArray
!