Overview
Features
Download
Documentation
Community
Add-Ons & Services

HashMap performance worse than std::map?

A general discussion forum.

HashMap performance worse than std::map?

Postby budric » 06 Jul 2011, 22:57

Hi,
I wanted to use a HashMap to get better performance for data lookup. However the following test shows HashMap running 4x slower than std::map.

Code: Select all
std::map<std::string, int> stdMap;
Poco::HashMap<std::string, int> pocoMap;
std::vector<std::string> keys;

 //populate maps
Poco::Random randGenerator;
for (int i = 0; i < 30000; i++)
{
   std::stringstream ss;
   for (int j = 0; j < 20; j++)   //generate 20 character key string
   {
      int charIndex = (int)(randGenerator.nextDouble() * 25.0);
      ss << (char)('a' + charIndex);
   }
   stdMap[ss.str()] = i;
   pocoMap[ss.str()] = i;
   keys.push_back(ss.str());
}

//run performance test
Poco::Stopwatch sw;
sw.start();
for (unsigned int i = 0; i < 30000; i++)
{
   std::map<std::string, int>::const_iterator it = stdMap.find(keys[i]);
}
cout << "stdMap: " << sw.elapsed() << endl;
sw.reset();
sw.start();
for (unsigned int i = 0; i < 30000; i++)
{
   Poco::HashMap<std::string, int>::ConstIterator it = pocoMap.find(keys[i]);
}
cout << "pocoMap: " << sw.elapsed() << endl;


As you can see I'm using default hash implementation for strings - I don't know how it's implemented in Poco.
budric
 
Posts: 24
Joined: 13 May 2009, 17:14

Re: HashMap performance worse than std::map?

Postby budric » 07 Jul 2011, 16:30

I've tried changing the hash function from default to: http://www.azillionmonkeys.com/qed/hash.html, and also comparing it to google::dense_hash_map with the same hash function implementation. The timing results were:

Code: Select all
stdMap: 20000
pocoMap: 14000
googleMap: 6000


With the default hash function pocoMap ran at 91000 uS. So at least it's a little faster than std::map now, otherwise what's the point.
budric
 
Posts: 24
Joined: 13 May 2009, 17:14


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron