question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Big speed up in searchsorted if second input is also sorted

See original GitHub issue

I have been leaning on searchsorted heavily for one of my projects and was playing around with how to speed things up. I found out that pre-sorting the second input for large arrays greatly speeds up the computation (even taking into account the up front cost of sorting).

x = np.random.randn(5000000)
y = np.random.randn(5000000)
x.sort()

%time np.searchsorted(x, y)
CPU times: user 10.3 s, sys: 36.7 ms, total: 10.4 s
Wall time: 10.4 s

%time y.sort(); np.searchsorted(x, y)
CPU times: user 959 ms, sys: 12.4 ms, total: 971 ms
Wall time: 971 ms

I was surprised because from the documentation it didn’t seem like searchsorted made any assumptions about whether y was sorted or not. Is this speedup purely because data locality is better after sorting y? Or is there actually an algorithmic reason for this speed up? I tried digging into the C code but couldn’t follow where this would be implemented.

Would it make sense to note and explain this behavior in the documentation? Would it make sense to add an additional argument to searchsorted(x, y, both_sorted=True), or something like merge_sorted(x, y) which assumes both x and y are sorted?

See also: https://stackoverflow.com/questions/27916710/numpy-merge-sorted-array-to-an-new-array

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
juliantaylorcommented, Apr 21, 2018

What is actually providing the speedup here is the reduction in branch misses. With this dense list of keys to search the wanted next key is usually very close to the beginning of the remaining search space so the branch results are mostly going the same direction. As searching is all just comparing and branching, allowing the cpu to correctly predict most branches greatly improves performance.

0reactions
juliantaylorcommented, Apr 21, 2018

the optimization of reducing the search space is actually harmful for the unsorted performance here, we knew these cases exist when it was implemented, but at the time the binsearch was also untyped so in total the performance was still better for all cases. It might be worthwhile to tune this code again a bit for unsorted keys.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Faster method for searchsorted when both arrays are sorted
basically finds the indices of the first elements in a sorted array a that are larger than or equal to the elements in...
Read more >
numpy.searchsorted — NumPy v1.24 Manual
Find the indices into a sorted array a such that, if the corresponding elements in v were inserted before the indices, the order...
Read more >
Supported NumPy features - Numba
NumPy arrays provide an efficient storage method for homogeneous sets of data. NumPy dtypes provide type information useful when compiling, and the regular, ......
Read more >
Algorithms: Searching and Sorting Cheatsheet | Codecademy
The Linear Search algorithm has a Big-O (worst case) runtime of O(N). This means that as the input size increases, the speed of...
Read more >
SORTING AND SEARCHING
shows disappointing performance when run on a large data set. Special Topic 14.2 discusses insertion sort, another simple sorting algorithm.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found