Suppose I have an array my_array
and a singular value my_val
. (Note that my_array
is always sorted).
my_array = np.array([1, 2, 3, 4, 5])
my_val = 1.5
Because my_val
is 1.5, I want to put it in between 1 and 2, giving me the array [1, 1.5, 2, 3, 4, 5]
.
My question is: What's the fastest way (i.e. in microseconds) of producing the ordered output array as my_array
grows arbitrarily large?
The original way I though of was concatenating the value to the original array and then sorting:
arr_out = np.sort(np.concatenate((my_array, np.array([my_val]))))
[ 1. 1.5 2. 3. 4. 5. ]
I know that np.concatenate
is fast but I'm unsure how np.sort
would scale as my_array
grows, even given that my_array
will always be sorted.
Edit:
I've compiled the times for the various methods listed at the time an answer was accepted:
Input:
import timeit
timeit_setup = 'import numpy as np\n' \
'my_array = np.array([i for i in range(1000)], dtype=np.float64)\n' \
'my_val = 1.5'
num_trials = 1000
my_time = timeit.timeit(
'np.sort(np.concatenate((my_array, np.array([my_val]))))',
setup=timeit_setup, number=num_trials
)
pauls_time = timeit.timeit(
'idx = my_array.searchsorted(my_val)\n'
'np.concatenate((my_array[:idx], [my_val], my_array[idx:]))',
setup=timeit_setup, number=num_trials
)
sanchit_time = timeit.timeit(
'np.insert(my_array, my_array.searchsorted(my_val), my_val)',
setup=timeit_setup, number=num_trials
)
print('Times for 1000 repetitions for array of length 1000:')
print("My method took {}s".format(my_time))
print("Paul Panzer's method took {}s".format(pauls_time))
print("Sanchit Anand's method took {}s".format(sanchit_time))
Output:
Times for 1000 repetitions for array of length 1000:
My method took 0.017865657746239747s
Paul Panzer's method took 0.005813951002013821s
Sanchit Anand's method took 0.014003945532323987s
And the same for 100 repetitions for an array of length 1,000,000:
Times for 100 repetitions for array of length 1000000:
My method took 3.1770704101754195s
Paul Panzer's method took 0.3931240139911161s
Sanchit Anand's method took 0.40981490723551417s