Map*_*p X 6 c++ algorithm profiling stl binary-search
一个简单的自制二进制搜索算法被std::binary_search(再次)击败:
// gcc version 4.8.2 X86_64
#ifndef EXAMPLE_COMPARE_VERSION
# define EXAMPLE_COMPARE_VERSION 0
#endif
static const long long LOOPS = 0x1fffffff;
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#if EXAMPLE_COMPARE_VERSION
#include <algorithm>
inline bool stl_compare(const int l, const int r) {
return l < r;
}
#else
inline bool compare(const int *beg, const int *end, int v) {
for (const int *p; beg <= end;) {
p = beg + (end - beg) / 2;
if (*p < v) beg = p + 1;
else if (*p > v) end = p - 1;
else return true;
}
return false;
}
#endif
int main() {
const int arr[] = {
1784, 1785, 1787, 1789, 1794, 1796, 1797, 1801,
1802, 1805, 1808, 1809, 1912, 1916, 1918, 1919,
1920, 1924, 1925, 1926, 1929, 1930, 2040, 2044,
2047, 2055, 2057, 2058, 2060, 2061, 2064, 2168,
2172, 2189, 2193, 2300, 2307, 2309, 2310, 2314,
2315, 2316, 2424, 2429, 2432, 2433, 2438, 2441,
2448, 2552, 2555, 2563, 2565, 2572, 2573, 2680,
2684, 2688, 2694, 2697, 2699, 2700, 2704, 2705,
2808, 2811, 2813, 2814, 2816, 2818, 2822, 2826,
2827, 2828, 2936, 2957, 3064, 3070, 3072, 3073,
3074, 3075, 3076, 3077, 3078, 3081, 3082, 3084,
3085, 3086, 3088, 3192, 3196, 3198, 3200, 3205,
3206, 3211, 3212, 3213, 3326, 3327, 3328, 3330,
3331, 3333, 3337, 3338, 3339, 3344, 3448, 3449,
3451, 3452, 3454, 3459, 3461, 3462, 3465, 3469,
3472, 3578, 3585, 3588, 3593, 3594, 3704, 3712,
3715, 3722, 3723, 3852, 3972, 3973, 3974, 3980,
3982, 4088, 4090, 4091, 4092, 4094, 4096, 4098,
4099, 4100, 4101, 4102, 4103, 4105, 4106, 4107,
4108, 4109, 4110, 4216, 4220, 4222, 4223, 4224,
4226, 4227, 4229, 4230, 4233, 4234, 4235, 4238,
4240, 4350, 4354, 4361, 4369, 4476, 4480, 4486,
4600, 4614, 4735, 4864, 4870, 4984, 4991, 5004,
};
clock_t t = clock();
const size_t len = sizeof(arr) / sizeof(arr[0]);
for (long long i = 0; i < LOOPS; i++) {
int v = arr[rand() % len];
#if EXAMPLE_COMPARE_VERSION >= 2
assert(std::binary_search(arr, arr + len, v, stl_compare));
#elif EXAMPLE_COMPARE_VERSION
assert(std::binary_search(arr, arr + len, v));
#else
assert(compare(arr, arr + len, v));
#endif
}
printf("compare version: %d\ttime: %zu\n",
EXAMPLE_COMPARE_VERSION, (clock() - t) / 10000);
}
Run Code Online (Sandbox Code Playgroud)
t.cc)g++ t.cc -O3 -DEXAMPLE_COMPARE_VERSION=0 -o t0
g++ t.cc -O3 -DEXAMPLE_COMPARE_VERSION=1 -o t1
g++ t.cc -O3 -DEXAMPLE_COMPARE_VERSION=2 -o t2
Run Code Online (Sandbox Code Playgroud)
./t2 ; ./t0 ; ./t1
Run Code Online (Sandbox Code Playgroud)
在我的机器上输出(时间越短越快):
compare version: 2 time: 3533
compare version: 0 time: 4074
compare version: 1 time: 3968
Run Code Online (Sandbox Code Playgroud)
设置为时EXAMPLE_COMPARE_VERSION,0我们使用自制的二进制搜索算法.
inline bool compare(const int *beg, const int *end, int v) {
for (const int *p; beg <= end;) {
p = beg + (end - beg) / 2;
if (*p < v) beg = p + 1;
else if (*p > v) end = p - 1;
else return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
设置EXAMPLE_COMPARE_VERSION为1我们使用时:
template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last,
const T& val);
Run Code Online (Sandbox Code Playgroud)
设置EXAMPLE_COMPARE_VERSION为2我们使用时:
template <class ForwardIterator, class T, class Compare>
bool binary_search (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
// the Compare function:
inline bool stl_compare(const int l, const int r) {
return l < r;
}
Run Code Online (Sandbox Code Playgroud)
这两个std::binary_search函数bits/stl_algo.h在gcc头文件目录中定义.
std::binary_search使用比较函数(t2)比没有它的版本(t1)快得多?更新:
替换random()为rand(),另请参阅rand()和random()函数之间的区别?
对此没有明确的答案,但我可以尝试给出一些观点。
如果指定 stl_compare,则 std::binary_search 首先调用您 stl_compare,然后调用实际运算符 < 导致额外调用。否则它可以简单地调用运算符<。
你的算法有机会改进。例如,您在比较时取消引用 p 2 次。您可以将 *p 保存到 const 或寄存器或 const 寄存器类型中以加快速度。
您可以修改您的比较函数如下并尝试一下吗
inline bool compare(const int *beg, const int *end, int v) {
while (beg <= end) {
const int *const p = beg + ((end - beg) >> 1);
const int z = *p;
if (z < v) beg = p + 1;
else if (z > v) end = p - 1;
else return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
它在我的机器上显示的结果比 stl 二进制搜索更好。(海湾合作委员会4.6.3)
编辑
完全尊重 Matthieu M 的评论。我尝试重新编写您的搜索食谱。在我的设置中,我仍然得到与 stl 相当的结果。
$ ./t0
compare version: 0 time: 3088
$ ./t1
compare version: 1 time: 3113
$ ./t2
compare version: 2 time: 3115
$ ./t0;./t1;./t2
compare version: 0 time: 3082
compare version: 1 time: 3116
compare version: 2 time: 3042
Run Code Online (Sandbox Code Playgroud)
按照我使用的修改后的功能
inline bool compare(const int *beg, const int *end, int v) {
if(end <= beg) return false; // Comment this line if you are sure end is always greater than beg
int count = end - beg;
while (count > 0) {
const int half = count >> 1;
const int *const p = beg + half;
if (*p < v) {
beg = p + 1;
count -= half + 1;
} else {
count = half;
}
}
return *beg == v;
}
Run Code Online (Sandbox Code Playgroud)