g_str_hash




Hi,

g_str_hash is really slow. The Tcl string hash function is twice as
fast, literally. Here's a test program, and it also shows you the Tcl
function. Any objections to the Tcl version?

Havoc

#include <glib.h>
#include <stdio.h>
#include <time.h>

guint
copy_of_g_str_hash (gconstpointer v)
{
  const char *s = (char*)v;
  const char *p;
  guint h=0, g;

  for(p = s; *p != '\0'; p += 1) {
    h = ( h << 4 ) + *p;
    if ( ( g = h & 0xf0000000 ) ) {
      h = h ^ (g >> 24);
      h = h ^ g;
    }
  }

  return h /* % M */;
}

guint
tcl_hash(gconstpointer v)
{
  const char* string = (char*)v;
  unsigned int result = 0;
  int c;

  while (1) {
    c = *string;
    string++;
    if (c == 0) {
      break;
    }
    result += (result<<3) + c;
  }
  return result;
}

const char* test_strings[] = {
  "",
  "Hello World",
  "Longer string blah blah blah blah",
  "/filename/type/of/thing",
  "foo",
  "bar",
  ";;;;;;;;;;3525665;k7k6j7;67.",
  NULL
};

static void time_hashfunc(guint (*func)(gconstpointer))
{
  int i = 0;
  clock_t start;
  clock_t end;

  printf("Timing %s\n",
         func == tcl_hash ? "tcl function" : "glib function");
  
  start = clock();
  
  while (i < 100000)
    {
      const gchar** iter;

      iter = test_strings;

      while (*iter)
        {
          guint h;
          h = (*func)(*iter);
          ++iter;
        }

      ++i;
    }

  end = clock();

  printf(" --> Used %g seconds\n",
         ((double)(end - start))/(double)CLOCKS_PER_SEC);
}

int main(int argc,char** argv)
{
  time_hashfunc(copy_of_g_str_hash);
  time_hashfunc(tcl_hash);
  time_hashfunc(copy_of_g_str_hash);
  time_hashfunc(tcl_hash);
  time_hashfunc(tcl_hash);
  time_hashfunc(copy_of_g_str_hash);
  time_hashfunc(tcl_hash);
  time_hashfunc(copy_of_g_str_hash);  
  
  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]