[gtk-list] Re: how can I trust glib when it has so many mem leaks?



marc@bowtie.nl
>the added advantage of having a "normal" view of one's memory consumption
>should easily outweigh the speed advantage, if there is any at all.

Ok, here's a glib vs malloc/free benchmark. On Solaris 2.6 with Sun cc I get
glib about 40% faster than malloc/free. It's not a very good benchmark :)

So, glib is faster (on Solaris anyway), and you get no heap fragmentation.

John
--
john@cima% a.out 100
Starting with malloc/free ...
... 1000000 nodes in 3.35596 secs
Starting with GSList ...
... 1000000 nodes in 1.9168 secs
john@cima% 
--
#include <stdio.h>
#include <gtk/gtk.h>

typedef struct _lnode {
        struct _lnode *next;
        void *data;
} Lnode;

GTimer *timer = NULL;

int
main( int argc, char **argv )
{
        Lnode *base = NULL;
        GSList *gbase = NULL;
        int max = atoi( argv[1] );
        Lnode *p;
        int i, j;

        timer = g_timer_new();

        printf( "Starting with malloc/free ...\n" );

        g_timer_reset( timer );

        for( j = 0; j < max; j++ ) {
                for( i = 0; i < 10000; i++ ) {
                        p = (Lnode *) malloc( sizeof( Lnode ) );
                        p->next = base;
                        base = p;
                }

                while( base ) {
                        p = base->next;
                        free( base );
                        base = p;
                }
        }

        printf( "... %d nodes in %g secs\n", 
                max * 10000, g_timer_elapsed( timer, NULL ) );

        printf( "Starting with GSList ...\n" );

        g_timer_reset( timer );

        for( j = 0; j < max; j++ ) {
                for( i = 0; i < 10000; i++ ) 
                        gbase = g_slist_prepend( gbase, NULL );

                while( gbase ) {
                        GSList *t = gbase->next;

                        g_slist_free_1( gbase );
                        gbase = t;
                }
        }

        printf( "... %d nodes in %g secs\n", 
                max * 10000, g_timer_elapsed( timer, NULL ) );

        return( 0 );
}
--
John Cupitt, john.cupitt@ng-london.org.uk, +44 (0)171 930 2108
VASARI Lab, The National Gallery, Trafalgar Square, London, WC2N 5DN



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