Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created July 10, 2012 04:53
GNU hash_set vs std::set
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#if defined(__GNUC__) && defined(WITH_HASHSET)
#include <ext/hash_set>
namespace __gnu_cxx
{
/* hack from http://www.moosechips.com/2008/10/using-gcc-c-hash-classes-wit
h-strings/ */
template<> struct hash< std::string >
{
size_t operator()( const std::string& x ) const
{
return hash< const char* >()( x.c_str() );
}
};
}
typedef __gnu_cxx::hash_set<std::string, __gnu_cxx::hash<std::string> > set_of_string;
#else
#include <set>
typedef std::set<std::string> set_of_string;
#endif
using namespace std;
int main(int argc,char** argv)
{
set_of_string rs_list;
for(int t=0;t<2;++t)
{
srand(0U);
for(int i=0;i< 10000000;++i)
{
ostringstream os;
os << "rs" << rand();
string rs(os.str());
if(t==0)
{
rs_list.insert(rs);
}
else
{
set_of_string::iterator r=rs_list.find(rs);
if(r!=rs_list.end()) rs_list.erase(r);
}
}
}
cout << "Time: "<< (((float)std::clock())/CLOCKS_PER_SEC) << "seconds.\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment