delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1997/11/10/10:57:33

From: "Tom Demmer" <DEMMER AT brain1 DOT lstm DOT ruhr-uni-bochum DOT de>
Organization: Lehrstuhl Stroemungsmechanik, RUB
To: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>,
Charles Sandmann <sandmann AT clio DOT rice DOT edu>, djgpp-workers AT delorie DOT com
Date: Mon, 10 Nov 1997 11:06:38 GMT-1
MIME-Version: 1.0
Subject: Re: malloc()
Reply-to: Demmer AT lstm DOT ruhr-uni-bochum DOT de
Message-ID: <88B961724A1@brain1.lstm.ruhr-uni-bochum.de>

[...]
> > Things change when not only malloc but also 
> > realloc is used, because this sometimes means touching the data as 
> > well.
> 
> Change--how?  Can you give us some quantitative idea?
> 
Check
http://www.lstm.ruhr-uni-bochum.de/~demmer/djgpp/malloc.html
and have a look. 
This is the test program:

#ifdef NEW_MALLOC
#include "malloc.h"
#endif


#include <time.h>
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
#include <dpmi.h>
#include <string.h>


void *foo[1024];

long Show_mallinfo(void){
   _go32_dpmi_meminfo info;
   _go32_dpmi_get_free_memory_information(&info);
   fprintf(stderr,"Physical mem left:%ld\n",
           info.available_physical_pages * 4096);
   return info.available_physical_pages * 4096;
}
void give_back_test(void){
int i;
  for(i=0; i< 16; ++i){
    foo[i] = calloc(1,65536);
  }
  Show_mallinfo();
  for(i=15; i>=0; --i){
     free(foo[i]);
//     malloc_trim(0);
     Show_mallinfo();
  }


}
void Random_Test1(long repeat, int maxsize){
  long i;
  memset(foo,0,sizeof(foo));
  for(i=0; i< repeat; ++i){
     int k = random() % maxsize +1;
     int j = random() % 1024;
     if( foo[j] ){
       free(foo[j]);
     }
     foo[j] = malloc(k);
  }

  for(i=0; i< 1024; ++i){
     free(foo[i]);
     foo[i] = NULL;
  }
}
void Random_Test2(long repeat, int maxsize){
  long i;
  memset(foo,0,sizeof(foo));
  for(i=0; i< repeat; ++i){
     int k = random() % maxsize+1;
     int j = random() % 1024;
     if( foo[j] )
       foo[j]=realloc(foo[j],k);
     else
       foo[j] = malloc(k);
  }

  for(i=0; i< 1024; ++i){
     free(foo[i]);
     foo[i] = NULL;
  }
}

void Random_Test3(long repeat, int maxsize){
  long i;
  memset(foo,0,sizeof(foo));
  for(i=0; i< repeat; ++i){
     int k = (random() % maxsize)+1;
     int j = random() % 1024;
     if( foo[j] ){
        free(foo[j]);
        foo[j]=NULL;
     }
     else{
       foo[j] = calloc(k,1);
       memset(foo[j],20,k);
     }
  }

  for(i=0; i< 1024; ++i){
     if(foo[i])
        free(foo[i]);
     foo[i] = NULL;
  }
}
long BlowUpTest1(int blksize){
  int count = 0;
  while( foo[0] = malloc(blksize)){
    ++count;
    memset(foo[0],20,blksize);
  }
  free(foo[0]);
  return count;
}
int main(int argc, char **argv){
uclock_t start, end;
long rep;
int blk;
long ph_fre;
int  tst;
  srandom(42);
  tst = atoi(argv[1]);
  rep = atol(argv[2]);
  blk = atoi(argv[3]);

  if(tst==0){
     start = uclock();
     Random_Test1(rep,blk);
     end = uclock() - start;
  }
  else if( tst==1){
     start = uclock();
     Random_Test2(rep,blk);
     end = uclock() - start;
  }
  else if (tst==2){
     start = uclock();
     Random_Test3(rep,blk);
     end = uclock() - start;
  }
  else if(tst==3){
    rep = BlowUpTest1(blk);
    fprintf(stdout,"%8d %12ld %12ld\n",blk,rep, rep*blk);
    return 0;
  }
  ph_fre = Show_mallinfo();
  fprintf(stdout,"%d %ld %f %ld\n",blk,rep, (double)end /UCLOCKS_PER_SEC, 
          ph_fre); 
  //  give_back_test();  
  return 0; 
}

Test 1 is the malloc/realloc case.
Up to an max blocksize of 4, libc is faster, then DL's version is slightly 
faster, but still comparable. At an max blks of 32 libc becomes faster 
again, but is outperformed by DL at a maximum blks of about 1000. 

More interesting is Test 3, up to a max blks of about 100, libc is faster,
a DL run takes about 0.3s, a libc about 0.2s. then the DL version is always 
faster. 
In terms of memory consumption, the DL version is always more efficient, except 
in Test 0 and large blocksizes. I don't have an idea why. 
Because in the blow up test, which just mallocs blocks until NULL is returned, 
the DL version is always more efficient.

You can find the graphs on the URL given above.

Ciao
Tom

  ******************************************************************
  *  Thomas Demmer                     * Phone : +49 234 700 6434  *
  *  Universitaetsstr. 150             * Fax   : +49 234 709 4162  *
  *  Lehrstuhl fuer Stroemungsmechanik *                           *
  *  D-44780 Bochum                    *                           *
  ******************************************************************
  *  Email: demmer AT LStM DOT Ruhr-Uni-Bochum DOT De                         *
  *  WWW:   http://www.lstm.ruhr-uni-bochum.de/~demmer             *
  ******************************************************************

    You've got to stand for something
    Or you're gonna fall for anything
                                    --- John Mellencamp

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019