Bonjour,
Je suis actuellement en train de tester les hashmap pour essayer d'améliorer du code basé sur des grandes map dont le temps de recherche commence a devenir tres long...
J'ai trouvé que sous VS, il suffit de se faire un petit include de hash_map et d'utiliser stdext::hash_map
Par contre, rien à voir avec ce que SGI propose a savoir : hash_map
Dans la hash_map de VS, il n'est meme pas fait reference à la fonction de hashage, alors que l'implementation de SGI demande une fonction de hashage directement dans le constructeur.
D'apres mes tests, la hash_map de VS est meme 2 fois moins performante qu'une simple map...
Note : ce code utilise les timer d'OpenScenGraph pour plus de simplicité, mais le résultat est sans appels :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
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; }
Alors avant tout, est ce que vous etes d'accord avec moi sur le fait que la hashmap de Visual Studio n'a rien d'une hashmap ?Hmap : 6.50609µs, index 9000000
map : 1.80725µs, index 9000000
Où puis-je trouver une hashmap digne de ce nom en c++ ? (opensource necessairement)
Merci.
Cordialement,
Ange_blond.
Partager