#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <set>
#include <ctime>
#include <algorithm>
int main( )
{
using namespace std;
vector< string> vs( 2000000 ) ;
for ( int i = 0 ; i < vs.size ( ) ; ++ i)
{
vs[ i] = to_string( i) ;
}
set< string> ss( vs.begin ( ) , vs.end ( ) ) ;
{
cout << "linear search on vector" << endl;
time_t now = clock ( ) ;
auto s = find( vs.begin ( ) , vs.end ( ) , "1000000" ) ;
cout << "linear search for \" 1000000\" took: "
<< double ( clock ( ) - now) / CLOCKS_PER_SEC << " found " << * s << endl;
}
{
cout << "\n binary search on vector" << endl;
sort( vs.begin ( ) , vs.end ( ) ) ;
time_t now = clock ( ) ;
auto s = lower_bound( vs.begin ( ) , vs.end ( ) , "1000000" ) ;
cout << "binary search for \" 1000000\" took: "
<< double ( clock ( ) - now) / CLOCKS_PER_SEC << " found " << * s << endl;
}
{
cout << "\n binary search on set" << endl;
time_t now = clock ( ) ;
auto s = ss.find ( "1000000" ) ;
cout << "binary search for \" 1000000\" took: "
<< double ( clock ( ) - now) / CLOCKS_PER_SEC << " found " << * s << endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KCmludCBtYWluKCkKewogICAgdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiAgICB2ZWN0b3I8c3RyaW5nPiB2cygyMDAwMDAwKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdnMuc2l6ZSgpOyArK2kpCiAgICB7CiAgICAgICAgdnNbaV0gPSB0b19zdHJpbmcoaSk7CiAgICB9CgogICAgc2V0PHN0cmluZz4gc3ModnMuYmVnaW4oKSwgdnMuZW5kKCkpOwoKICAgIHsKICAgICAgICBjb3V0IDw8ICJsaW5lYXIgc2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