| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 
 |  
#include <hash_map>
#include <time.h>
#include <iostream>
 
struct greater_str {
	bool operator()(const std::string x, const std::string y) const {
		if ( x.compare(y) > 0)
         return true;
 
      return false;
   }
};
 
 
int main()
{
 
	unsigned intmax = 1e07;
 
 
 
	//declare maps
	typedef stdext::hash_map<std::string, int, stdext::hash_compare<std::string, greater_str> > HashMap;
	typedef std::map<std::string , int, greater_str > Map;
 
	HashMap hmap;
	Map map;
 
 
	//add a lot of content
 
	for(unsigned int i=0 ; i < intmax ; ++i)
	{
		srand (osg::Timer::instance()->tick());
 
 
		//generate a string
		std::string string(10,' ');
		for(unsigned int j =0 ; j<9 ; ++j)
			string[j] = (char)(rand() % 30 + 'a');
 
		if(i == intmax*0.9)
			string = "trouvemoi";
 
		hmap.insert(std::make_pair(string, i));
		map.insert(std::make_pair(string, i));
 
	}
 
 
	osg::Timer_t hstart = osg::Timer::instance()->tick();
	Map::iterator it = map.find("trouvemoi");
	double hres = osg::Timer::instance()->delta_u(hstart, osg::Timer::instance()->tick());
 
	std::cout << "Hmap : " << hres << "µs, index " << it->second << std::endl;
 
	osg::Timer_t mstart = osg::Timer::instance()->tick();
	HashMap::iterator it2 = hmap.find("trouvemoi");
	double mres = osg::Timer::instance()->delta_u(mstart, osg::Timer::instance()->tick());
 
	std::cout << "map : " << mres << "µs, index " << it2->second << std::endl;
 
 
	return 1;
} | 
Partager