Based on your comment, it sounds like you're reading in an array of int16
s and then "de-interleaving" them into complex numbers.
You're currently doing something like:
d_short = np.fromfile(filename, dtype=np.int16)
data = np.array([np.complex(d_short[i], d_short[i+1]) for i in np.arange(...)])
The slow part is the second line.
You're creating a big temporary list, and creating it by iterating through a numpy array. Iterating through a numpy array in Python is much slower than iterating through a list. Always avoid it where you can. Furthermore, the list comprehension will result in a temporary list that's much larger in memory than the original array.
Instead of iterating through, use slicing. In this case, it's equivalent to:
data = d_short[::2] + d_short[1::2] * 1j
This will create a temporary array, but it shouldn't be an issue. However, if you're really concerned about memory usage, you might consider something like:
data = np.zeros(d_short.size // 2, dtype=np.complex64)
data.real, data.imag = d_short[::2], d_short[1::2]
While this is considerably less readable, it does have some advantages.
- No temporary array is created, so we only need the amount of memory used by
d_short
and data
- Instead of creating a
np.complex128
array, we're creating a np.complex64
array (two 32-bit floats), which will use half the memory. Because you're inputting 16-bit ints, there's no loss of precision.