c++ stl binary_search和lower_bound的效率有关问题

c++ stl binary_search和lower_bound的效率问题
本帖最后由 nigmare 于 2014-03-06 22:18:18 编辑
发现binary_search的效率要比lower_bound好,有点想不通,
binary_search调的lower_bound,在vs2012上编译的。
有高手帮忙解答下吗?



#include <time.h>
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main ()
{
clock_t t1,t2;
double t;
list<long> l1;
for (long i = 0; i < 20000; ++i)
{
l1.insert (l1.end (), i);
}

t1 = clock ();
for (long i = 0; i < 50000; ++i)
{
binary_search (l1.begin (), l1.end (), i);
}
t2 = clock ();
t = double (t2 - t1)/CLOCKS_PER_SEC;
cout<<t<<endl;


t1 = clock ();
for (long i = 0; i < 50000; ++i)
{
lower_bound (l1.begin (), l1.end (), i);
}
t2 = clock ();
t = double (t2 - t1)/CLOCKS_PER_SEC;
cout<<t<<endl;

cin.get ();
}


输出结果:
0
2.821
------解决方案--------------------
说明你其实没掌握性能测试的本领呗。
你这样的测只是毛估估,环境影响太大。
------解决方案--------------------
试了下 两个差不多 
都是二分查找
------解决方案--------------------
都是2分算法,只不过终结条件不同而已。