Tuning






Tuning

Tweaking the hash table's load factor can create a better distribution of objects among the buckets, but it won't help if your hash function isn't doing a good job. This isn't the place to talk about the details of good hash functionsthat's a large topic in itself. When you're storing data in a hash table, what you need to know is how well the hash function distributes the data that you have. You can look at the distribution with the member functions bucket_count(), which tells you how many buckets the hash table has; bucket_size(n), which tells you how many objects are in the nth bucket; and with the member functions begin(n) and end(n), which return iterators that you can use to look at the objects in the nth bucket. If your hash table isn't getting the performance you expect, this information can point you to the culprit. You may need to replace the hash function.

Figure. Tuning a Hash Table (contunord/tuning.cpp)

#include <unordered_set>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <iterator>
using std::cout; using std::setw;
using std::copy; using std::ostream_iterator;

typedef std::tr1::unordered_set <int> iset;
typedef iset::value_type elt;

static  void show_buckets (const iset& set)
  { // show details of buckets in set
  cout << setw (3) << set.size () << "elements,"
    << setw (3) << set.bucket_count () << "buckets,"
    << "load factor" << set.load_factor () << ".\n";
  for (int i = 0; i < set.bucket_count (); ++ i)
    cout << i << ':' << set.bucket_size (i) << "";
  cout << '\n';
  ostream_iterator <elt> output (cout, "");
  for (int i = 0; i <set.bucket_count (); ++i)
    { // show contents of bucket i
    cout << setw (3) << i << ":";
    copy (set.begin (i), set .end (i), output);
    cout << '\n';
    }
  }

int main ()
  { // demonstrate use of bucket functions
  iset  set;
  for (int i = 0; i <100; ++i)
    set.insert (i);
  show_buckets (set);
  return  0;
  }



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows