This question, It's more for a discussion about the
array
vslist
in python, and If It's worth the while changing a code base fromlist
's fo numbers toarrays
's. This is using python standard modules notnumpy
.
I was looking at the array
module, from the python standard library, and It caught my attention, that I could very well do some "simple" numerical analysis and number crunching replacing my lists of numbers with arrays of doubles or floats, (depending on the case).
Does anyone that have experience with python's array
object could share a comparison or why they choose to use them? I'm still having trouble with this decision.
My concern here, is If I can use in some way arrays of arrays, or list of arrays, and if that would boost my performance, right now I have lists of lists of numbers, and I'm trying to use python with no dependencies so no numpy.
If I'm correct the python list
object internally It's a dynamic array, I'm not sure about that.
I ran this test using size = sys.getsizeof
and array = array.array
, I know that this may not be great comparison, but still It raises some questions.
>>> for i in range(0, 100, 5):
... test = [1.0*j for j in range(i)]
... a = array('f', test)
... print(f"{i} | {size(a)} | {size(test)}")
len|array|list
---|-----|----
0 | 32 | 36
5 | 52 | 68
10 | 72 | 100
15 | 92 | 100
20 | 112 | 136
25 | 132 | 136
30 | 152 | 176
35 | 172 | 176
40 | 192 | 220
45 | 212 | 220
50 | 232 | 268
55 | 252 | 268
60 | 272 | 324
65 | 292 | 324
70 | 312 | 324
75 | 332 | 388
80 | 352 | 388
85 | 372 | 388
90 | 392 | 460
95 | 412 | 460