Associative array/std::map/dense_hash_map slow

May 20, 2010 at 12:49pm
Hi,

I've some trouble implementing a kind of associative array.

Basicly, the structure is like the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace mine {
		
	struct myHash
	{
		size_t operator()(std::string s) const {
			size_t h=__gnu_cxx::hash<const char *>()(s.c_str());
			return h;
		}
	};

	namespace {
		typedef std::map<std::string, std::pair<std::string, int> > QP;
		//typedef google::dense_hash_map<std::string, std::pair<std::string, int>, myHash > QP;
	}

	class myClass {
		private:
			static google::dense_hash_map<std::string, QP, myHash> QPs;
 [...]
} 
}

I tried to use both std::map and google's dense_hash_map, but none solved the problem.

QPs contains very few elements, (initially, 1 or 2), but I need it as a (kinda) map because of its flexibility in implementation and execution.

QPs has "QP" has values, and QP contain much more data (~10'000).

Strange thing is that accessing QPs (even with one single value!) is very slow, requiring 3msecs, while accessing QP requires much less (about 1ms). These timings happen both using map or dense_hash_map.

Why??

Probably, there's some better structure for associative arrays like those (with very few elements), can you suggest me some?

Thanks!
grana
Topic archived. No new replies allowed.