网点二进制搜索 [英] Branchless Binary Search

查看:128
本文介绍了网点二进制搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果任何人都可以解释一个网点的二进制搜索实现了我。我看到它在最近提到的<一个href="http://stackoverflow.com/questions/4930307/fastest-way-to-get-the-integer-part-of-sqrtn">question但我怎么也想不到会实现。我想这可能是有用的,以避免树枝,如果项目的数量是相当大的。

I'm curious if anyone could explain a branchless binary search implementation to me. I saw it mentioned in a recent question but I can't imagine how it would be implemented. I assume it could be useful to avoid branches if the number of items is quite large.

推荐答案

我会假设你在说了一句做的静态常量阵列所有完美的正方形要支持,并在其上​​进行一个快速的网点二进​​制搜索的域名。在<一个发现href="http://stackoverflow.com/questions/4930307/fastest-way-to-get-the-integer-part-of-sqrtn/4930346#4930346">this回答。

I'm going to assume you're talking about the sentence "Make a static const array of all the perfect squares in the domain you want to support, and perform a fast branchless binary search on it." found in this answer.

一个网点二进制搜索基本上是一个展开的二进制搜索循环。如果你事先知道数组中的项目要搜索的数量这仅适用(因为你会如果它是静态常量)。您可以编写一个程序写的展开code。如果它太长做手工。

A "branchless" binary search is basically just an unrolled binary search loop. This only works if you know in advance the number of items in the array you're searching (as you would if it's static const). You can write a program to write the unrolled code if it's too long to do by hand.

然后,你必须的基准的解决方案,看看它是否真的比一个循环更快。如果你的网点code是太大了,它不适合CPU的快速指令高速缓存内,将需要更长的时间比同等的循环运行。

Then, you must benchmark your solution to see whether it really is faster than a loop. If your branchless code is too big, it won't fit inside the CPU's fast instruction cache and will take longer to run than the equivalent loop.

这篇关于网点二进制搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆