Can I force python array elements to have a specific size? -


i using arrays modules store sizable numbers (many gigabytes) of unsigned 32 bit ints. rather using 4 bytes each element, python using 8 bytes, indicated array.itemsize, , verified pympler.

eg:

>>> array("l", range(10)).itemsize 8 

i have large number of elements, benefit storing them within 4 bytes.

numpy let me store values unsigned 32 bit ints:

>>> np.array(range(10), dtype = np.uint32).itemsize 4 

but problem operation using numpy's index operator twice slow, operations aren't vector operations supported numpy slow. eg:

python3 -m timeit -s "from array import array; = array('l', range(1000))" "for in range(len(a)): a[i]" 10000 loops, best of 3: 51.4 usec per loop 

vs

python3 -m timeit -s "import numpy np; = np.array(range(1000), dtype = np.uint32)" "for in range(len(a)): a[i]" 10000 loops, best of 3: 90.4 usec per loop 

so forced either use twice memory like, or program run twice slow like. there way around this? can force python arrays have use specified itemsize?

if want stick using array, set typecode i (unsigned int) rather l (unsigned long):

>>> array.array("i", range(10)).itemsize 4 

that said, surprised if there wasn't way speed calculations way more 2x losing using numpy. hard tell without knowing doing values.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -