I have an numpy array of complex numbers. So I want to insert zero at start of the array,
and shift the rest of the array one place forward.
example:
I timed all the five different methods to insert an element at the beginning of an array. Here are the results:
In [20]: %timeit np.hstack([1, [1, 2, 3]])
10000 loops, best of 3: 30.4 µs per loop
In [21]: %timeit np.insert([1, 2, 3], 0, 1)
10000 loops, best of 3: 46.6 µs per loop
In [22]: %timeit np.r_[[1], [1, 2, 3]]
10000 loops, best of 3: 32.8 µs per loop
In [28]: %timeit np.append(1, [1, 2, 3])
10000 loops, best of 3: 23.4 µs per loop
In [29]: %timeit np.concatenate([[1], [1, 2, 3]])
The slowest run took 6.43 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 8.79 µs per loop