l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
April 21: Google Glass
Next Installfest:
TBD
Latest News:
Mar. 18: Google Glass at LUGOD's April meeting
Page last updated:
2010 Feb 14 10:39

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] stupid hash doesn't work with libglib-2.0
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] stupid hash doesn't work with libglib-2.0



Well, it's pretty fast so far. Ran it against a file with over 55 million
integers, and it only took 13 plus seconds to the 12 unique integers on
a 2.2 GHz with a 4 Megs cache.

brian@condios:~/school/Project/practice$ time ./hashtest3 
Size of hash table is 12
records read from file 55136256
First list Key is 1334, value is 1334
First list Key is 1, value is 1
First list Key is 4, value is 4
First list Key is 12222221, value is 12222221
First list Key is 32, value is 32
First list Key is 122334, value is 122334
First list Key is 412, value is 412
First list Key is 12, value is 12
First list Key is 43, value is 43
First list Key is 334, value is 334
First list Key is 56, value is 56
First list Key is 12334, value is 12334

real	0m13.187s
user	0m13.120s
sys	0m0.060s


On Sat, Feb 13, 2010 at 05:36:34PM -0800, Brian Lavender wrote:
> I figured it out! When I created the hash with g_hash_table_new_full,
> I provided NULL as the hash function. I changed this to g_int_hash,
> a built in function in glib that hashes a pointer to an integer.
> 
> I almost wonder if I wrote my own hash if it would have been faster? 
> 
> I also found a decent article on IBM regarding glib.
> http://www.ibm.com/developerworks/linux/library/l-glib2.html
> 
> brian
> 
> 
> On Fri, Feb 12, 2010 at 12:24:10AM -0800, Brian Lavender wrote:
> > I am trying to eliminate duplicates, so I figured I would just use a
> > hash from libglib-2.0. It worked fine with static pointers (or whatever
> > you call that portion oof code)
> > 
> > Problem is, once I try it with dynamically allocated pointers, it
> > doesn't eliminiate any duplicates. 
> > 
> > Anything obvious that I am missing?
> > 
> > Comple with the following.
> > 
> > gcc `pkg-config glib-2.0 --cflags` read_hash.c -o read_hash
> > 
> 
> -- 
> Brian Lavender
> http://www.brie.com/brian/
> 
> "About 3 million computers get sold every year in China, but people don't
> pay for the software. Someday they will, though. As long as they are going
> to steal it, we want them to steal ours. They'll get sort of addicted, and
> then we'll somehow figure out how to collect sometime in the next decade."
> 
> -- Bill Gates (Microsoft) 1998

> #include <stdio.h>
> #include <string.h>
> #include <glib.h>
> 
> #define BUF_SZ 11
> #define NUM_HTABLES 9
> 
> gboolean compare(gint *a, gint *b)
> {
>   gboolean rvalue;
>   if (*a == *b)
>     rvalue = TRUE;
>   else
>     rvalue = FALSE;
>   g_printf("The test ran! a: %d b: %d\n",*a,*b);
>   return rvalue;
> }
> 
> void destroy(gpointer foo) {
>   g_slice_free1(sizeof(int),foo);
> }
> 
> void printeach(gpointer a, gpointer b, gpointer userdata) {
>  
>   g_printf("%s Key is %d, value is %d\n",(char *)userdata,*(int *)a, *(int *)b);
> }
> 
> 
> int savestat(  GHashTable *myHTable[NUM_HTABLES],
> 	       char *myFile) {
> 
>   int i;
>   FILE *input;
>   gchar *rd_buf;
>   gint *c, *d;
>   int *old_key, *old_value;
>   int tmpInt;
>   int nchars;
>   int cur_sz;
>   int count=0;
> 
>   input = fopen(myFile, "r");
>   if (input == NULL)
>     {
>       perror("Failed to open file");
>       return -1;
>     }
>   // buffer for reading input
>   rd_buf = (gchar *) g_slice_alloc0( BUF_SZ * sizeof(char) );
> 
>   nchars = getline(&rd_buf, &cur_sz, input);
> 
>   while ( nchars != -1 ) {
>     if ( rd_buf[nchars-1] == '\n') 
>       rd_buf[nchars-1] = '\0';
> 
>     tmpInt = atoi(rd_buf);
>     
>     c = (int *)g_slice_alloc0(  sizeof(int) );
>     d = (int *)g_slice_alloc0(  sizeof(int) );
>     g_memmove(c, &tmpInt,sizeof(int));
>     g_memmove(d, &tmpInt,sizeof(int));
> 
> /*     if( g_hash_table_lookup_extended(myHTable[0], c,(gpointer) &old_key,(gpointer ) &old_value ) ) { */
> /*       g_hash_table_insert(myHTable[0], c, d); */
> /*       g_free(old_key); */
> /*       g_free(old_value); */
> /*     } else { */
>     count++;
>     g_hash_table_insert(myHTable[0], c, d);
> /*     } */
> 
>     nchars = getline(&rd_buf, &cur_sz, input);
>   } 
> 
>   close(myFile);
>   return count;
> }
> 
> 
> 
> int main() {
> 
>   GHashTable *myHTable[NUM_HTABLES];
> 
>   int nrecords;
>   int i;
>   char *sf = "myints.txt";
> 
>   guint size;
>   gboolean rVal;
> 
> 
>   myHTable[0] = g_hash_table_new_full(g_int_hash,(GEqualFunc)compare, 
> 				   (GDestroyNotify)destroy,
> 				   (GDestroyNotify)destroy);
> 
>   // We can pass by copy
>   nrecords = savestat( myHTable, sf);
> 
>   size = g_hash_table_size(myHTable[0]);
>   g_printf("Size of hash table is %d\n",size);
>   g_printf("records read from file %d\n",nrecords);
> 
> 
>   g_hash_table_foreach(myHTable[0], printeach, "First list");
> 
>   return 0;
> }

> 334
> 1
> 4
> 56
> 32
> 12
> 1
> 4
> 56
> 56
> 12

> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech


-- 
Brian Lavender
http://www.brie.com/brian/

"About 3 million computers get sold every year in China, but people don't
pay for the software. Someday they will, though. As long as they are going
to steal it, we want them to steal ours. They'll get sort of addicted, and
then we'll somehow figure out how to collect sometime in the next decade."

-- Bill Gates (Microsoft) 1998
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.