在我检测到std :: upper_bound之前,我实现了自己的binarySearch版本来确定所需元素的索引。实现有效,但与线性搜索相比,我的binarySearch只有一点点快。随着搜索区域的增长,我的实现和std lib之间的因素也会增加。
为了快速自我测试,我在本文末尾插入了完整的代码。快速浏览一下我的searchBinary实现:
template<typename T> T searchBinary(const std::vector<std::vector<T> > vectorList, const std::vector<T> compareVector) {
long iteration = 0;
size_t leftIndex = 0;
size_t rightIndex = vectorList.size()-1;
size_t pos;
while (leftIndex <= rightIndex) {
iteration++;
pos = (leftIndex + rightIndex) / 2;
if (compareVector < vectorList[pos]) {
rightIndex = pos - 1;
} else if (compareVector > vectorList[pos]) {
leftIndex = pos + 1;
} else {
cout << "Match at binary search after " << iteration << " iterations.\n";
return pos;
}
}
cout << "No match at binary search after " << iteration << " iterations.\n";
return -1;
}
这就是我弄乱运行时的原因:
void searchBinaryOwn_messure(std::vector<std::vector<u_char> > vectorList, std::vector<u_char> compareVector) {
struct timeval begin, end;
long seconds, useconds;
if (gettimeofday(&begin,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
searchBinary(vectorList, compareVector);
if (gettimeofday(&end,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
seconds = end.tv_sec - begin.tv_sec;
useconds = end.tv_usec - begin.tv_usec;
if(useconds < 0) {
useconds += 1000000;
seconds--;
}
printf("searchBinaryOwn(): %ld sec %ld usec\n\n", seconds, useconds);
return;
}
不要在这里看到任何问题。如果我用8 000运行程序 000元素:
那么为什么两个二进制搜索之间存在如此巨大的差异?(用gcc 4.8.2编译) 注意:因为&#34; cout ...&#34;需要大约30usec,std :: binarySearch实际上比显示的更快
这里是完整的代码:
#include <iostream>
#include <vector>
#include <sys/time.h>
#include <algorithm>
#include <string>
#include <stdio.h>
using namespace std;
template<typename T> T searchBinary(const std::vector<std::vector<T> > vectorList, const std::vector<T> compareVector) {
long iteration = 0;
size_t leftIndex = 0;
size_t rightIndex = vectorList.size()-1;
size_t pos;
while (leftIndex <= rightIndex) {
iteration++;
pos = (leftIndex + rightIndex) / 2;
if (compareVector < vectorList[pos]) {
rightIndex = pos - 1;
} else if (compareVector > vectorList[pos]) {
leftIndex = pos + 1;
} else {
cout << "Match at binary search after " << iteration << " iterations.\n";
return pos;
}
}
cout << "No match at binary search after " << iteration << " iterations.\n";
return -1;
}
size_t searchLinear(std::vector<std::vector<u_char> > vectorList, std::vector<u_char> compareVector) {
size_t vectorListSize = vectorList.size();
for (size_t i = 0; i < vectorListSize; i++) {
if (vectorList[i] == compareVector) {
return i;
}
}
return (size_t)-1;
}
void searchLinear_messure(std::vector<std::vector<u_char> > vectorList, std::vector<u_char> compareVector) {
struct timeval begin, end;
long seconds, useconds;
if (gettimeofday(&begin,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
//search
cout << "\nPos: " << searchLinear(vectorList, compareVector) << endl;
if (gettimeofday(&end,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
seconds = end.tv_sec - begin.tv_sec;
useconds = end.tv_usec - begin.tv_usec;
if(useconds < 0) {
useconds += 1000000;
seconds--;
}
printf("searchLinear(): %ld sec %ld usec\n\n", seconds, useconds);
return;
}
void searchBinaryStd_messure(std::vector<std::vector<u_char> > vectorList, std::vector<u_char> compareVector) {
struct timeval begin, end;
long seconds, useconds;
if (gettimeofday(&begin,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
//search
cout << "found: " << std::binary_search(vectorList.begin(), vectorList.end(), compareVector) << endl;
if (gettimeofday(&end,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
seconds = end.tv_sec - begin.tv_sec;
useconds = end.tv_usec - begin.tv_usec;
if(useconds < 0) {
useconds += 1000000;
seconds--;
}
printf("searchBinaryStd(): %ld sec %ld usec\n\n", seconds, useconds);
return;
}
void searchBinaryOwn_messure(std::vector<std::vector<u_char> > vectorList, std::vector<u_char> compareVector) {
struct timeval begin, end;
long seconds, useconds;
if (gettimeofday(&begin,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
searchBinary(vectorList, compareVector);
if (gettimeofday(&end,(struct timezone *)0)) {
fprintf(stderr, "can not get time\n");
exit(1);
}
seconds = end.tv_sec - begin.tv_sec;
useconds = end.tv_usec - begin.tv_usec;
if(useconds < 0) {
useconds += 1000000;
seconds--;
}
printf("searchBinaryOwn(): %ld sec %ld usec\n\n", seconds, useconds);
return;
}
int main() {
std::vector<u_char> compareVector;
compareVector.clear();
compareVector.push_back(0xF8);
compareVector.push_back(0xD1);
compareVector.push_back(0x11);
compareVector.push_back(0xFF);
std::vector<std::vector<u_char> > vectorList;
vectorList.clear();
std::vector<u_char> temp;
for (unsigned int i = 0; i < ((unsigned int)-1); i++) {
if (i == 8000000) {
// if (i == 15000000) {
break;
}
temp.clear();
temp.push_back(0x11);
temp.push_back(0x22);
temp.push_back(0x33);
temp.push_back(0x44);
vectorList.push_back(temp);
}
vectorList[7999999] = compareVector;
cout << "Elements in vectorList: " << vectorList.size() << endl;
searchLinear_messure(vectorList, compareVector);
searchBinaryStd_messure(vectorList, compareVector);
searchBinaryOwn_messure(vectorList, compareVector);
return 0;
}
答案 0 :(得分:1)
template<typename T> T searchBinary(const std::vector<std::vector<T> >& vectorList, const std::vector<T>& compareVector) {
即。通过常量引用而不是通过值传递。这将避免两个矢量副本。
您可以在每次迭代时使用单个条件测试<
进行重构。 (您还需要更改while
条件)。
iteration
需要long
吗?可以不短吗?收敛的最坏情况是什么?
第1点是重要的一点。 2非常重要,3是微观优化,在某些系统上根本不会产生任何影响。
答案 1 :(得分:1)
向量按值传递给searchBinary
,因此将创建需要时间的副本。
如果您将签名更改为
template<typename T> T searchBinary(const std::vector<std::vector<T> >& vectorList, const std::vector<T>& compareVector)
它与标准实施一样快:http://melpon.org/wandbox/permlink/qozapTfn3MrGv5JA
答案 2 :(得分:0)
这个template<typename T> T searchBinary(const std::vector<std::vector<T> > vectorList, const std::vector<T> compareVector)
制作输入向量的副本(当你按值传递时),它是时间线性的。所以你得到的结果实际上是预期的。
顺便说一下。一个标志性的答案可能是标准库是由相当优秀的开发人员编写的,预计它几乎不会出类拔萃。