From: bt@irfu.se (Bo Thide')
Subject: A little malloc speed test.
Date: Tue, 15 Sep 1992 19:01:06 GMT
Organization: Swedish Institute of Space Physics, Uppsala, Sweden


I was curious to see the improvement in malloc speed between HP-UX 8.05
and HP-UX 8.07 on our Snakes (720 and 730) and ran the test below.  It
seems that the 8.07 malloc compares favourably to the GNU malloc on the
HP and to the Suns I had available.  I would be interested in
comparisons with other platforms and malloc's.  For those systems that
support mallopt uncommenting (and possible modification of) the mallopt
line in the code can be elucidating.

==============================CUT HERE================================
/* Results
SparcStation 1 SunOs 4.1.1 libc malloc:				18.6 s
SparcStation 1 SunOs 4.1.1 gnu malloc:				 5.6 s
SparcStation IPX SunOs 4.1.1 libc malloc:			 9.7 s
SparcStation IPX SunOs 4.1.1 gnu malloc:			 3.0 s
HP9000/730 HPUX 8.05 libc malloc:				65.3 s
HP9000/730 HPUX 8.05 gnu malloc:				 1.4 s
HP9000/720 HPUX 8.07 libc malloc:			         6.82 s
HP9000/730 HPUX 8.07 libc malloc:			         5.20 s
HP9000/720 HPUX 8.07 libc malloc & mallopt:		         1.57 s
HP9000/730 HPUX 8.07 libc malloc & mallopt:			 1.19 s
*/
#include <stdio.h>
#include <malloc.h>
#include <sys/time.h>

#define ENTRIES 1000

typedef struct _Entry {
    char *p;
    int size;
} Entry;

int sum;

memprobe(p)
char *p;
{
    *p = 0x12345678;
}

growmem(size)
int size;
{
    int i;
    char *ptr;

    ptr = malloc(size);
    for (i = 0; i < size; i += 1024) memprobe(ptr + i);
    free(ptr);
}

free_half(tab)
Entry *tab;
{
    int i;

    for (i = 0; i < ENTRIES; i++) {
	if ((i & 2) && tab[i].p) {
	    free(tab[i].p);
	    tab[i].p = NULL;
	    sum -= tab[i].size;
	}
    }
}

free_all(tab)
Entry *tab;
{
    int i;

    for (i = 0; i < ENTRIES; i++) {
	if (tab[i].p) {
	    free(tab[i].p);
	    tab[i].p = NULL;
	    sum -= tab[i].size;
	}
    }
}

free_big(tab, size)
Entry *tab;
int size;
{
    int i;

    for (i = 0; i < ENTRIES; i++) {
	if (tab[i].p && tab[i].size >= size) {
	    free(tab[i].p);
	    tab[i].p = NULL;
	    sum -= tab[i].size;
	}
    }
}

inittab(tab)
Entry *tab;
{
    int i;

    for (i = 0; i < ENTRIES / 10; i++) {
	tab[i * 10 + 0].size = 5;
	tab[i * 10 + 1].size = 15;
	tab[i * 10 + 2].size = 30;
	tab[i * 10 + 3].size = 55;
	tab[i * 10 + 4].size = 60;
	tab[i * 10 + 5].size = 80;
	tab[i * 10 + 6].size = 260;
	tab[i * 10 + 7].size = 300;
	tab[i * 10 + 8].size = 600;
	tab[i * 10 + 9].size = 1500;
    }
}

test1(tab)
Entry *tab;
{
    int i;

    for (i = 0; i < ENTRIES; i++) {
	tab[i].p = malloc(tab[i].size);
	sum += tab[i].size;
    }
    for (i = 0; i < ENTRIES; i++) {
	if (i & 2) {
	    if (tab[i].p) {
		free(tab[i].p);
		sum -= tab[i].size;
	    }
	    tab[i].size *= 2;
	    tab[i].p = malloc(tab[i].size);
	    sum += tab[i].size;
	}
    }
    for (i = 0; i < ENTRIES; i++) {
	if (i & 2) {
	    if (tab[i].p) {
		free(tab[i].p);
		sum -= tab[i].size;
	    }
	    tab[i].size /= 2;
	    tab[i].size = 1502 - tab[i].size;
	    tab[i].p = malloc(tab[i].size);
	    sum += tab[i].size;
	}
    }
}

main()
{
    int i;
    int tabsize;
    double t0;
    double t1;
    Entry *tab1;
    Entry *tab2;
    Entry *tab3;
    struct timeval tv_start;
    struct timeval tv_stop;

    printf("allocate memory\n");
    /*mallopt(M_MXFAST,16384);*/
    growmem(4000000);
    printf("malloc test\n");
    gettimeofday(&tv_start, NULL);
    tabsize = ENTRIES * sizeof(Entry);
    sum = 0;
    tab1 = (Entry *)malloc(tabsize);
    sum += tabsize;
    tab2 = (Entry *)malloc(tabsize);
    sum += tabsize;
    tab3 = (Entry *)malloc(tabsize);
    sum += tabsize;
    for (i = 0; i < 50; i++) {
	inittab(tab1);
	test1(tab1);
#ifdef DEBUG
	printf("tab1 sum = %d\n", sum);
#endif
	inittab(tab2);
	test1(tab2);
#ifdef DEBUG
	printf("tab2 sum = %d\n", sum);
#endif
	free_half(tab2);
	free_half(tab1);
	inittab(tab3);
	test1(tab3);
#ifdef DEBUG
	printf("tab3 sum = %d\n", sum);
#endif
	free_big(tab1, 100);
	free_big(tab2, 100);
	free_big(tab3, 100);
#ifdef DEBUG
	printf("free sum = %d\n", sum);
#endif
    }
    gettimeofday(&tv_stop, NULL);
    t0 = (double)tv_start.tv_sec + tv_start.tv_usec * 1.0e-6;
    t1 = (double)tv_stop.tv_sec + tv_stop.tv_usec * 1.0e-6;
    printf("test time is %f seconds\n", t1 - t0);
#ifdef DEBUG
    sleep(100);
#endif
}
-- 
   ^   Bo Thide'--------------------------------------------------------------
  |I|        Swedish Institute of Space Physics, S-755 91 Uppsala, Sweden
  |R|      Phone: (+46) 18-303671.  Fax: (+46) 18-403100.  IP: 130.238.30.23
 /|F|\          INTERNET: bt@irfu.se      UUCP: ...!mcvax!sunic!irfu!bt  
 ~~U~~ ----------------------------------------------------------------sm5dfw-
Newsgroups: comp.benchmarks