delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/05/23/18:11:47

From: "Mark E." <snowball3 AT bigfoot DOT com>
To: djgpp-workers AT delorie DOT com
Date: Wed, 23 May 2001 18:11:43 -0400
MIME-Version: 1.0
Subject: ehhanced realloc test program
Message-ID: <3B0BFD5F.25329.475F21@localhost>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp-workers AT delorie DOT com

Hello,

I created a test program to try and get a rough idea of the effect on performance of the proposed 
change to realloc. After comparing the two methods, I have to wonder if there is a flaw in my 
test.

I wrote a program that performs a lot of reallocs to measure the difference with and without the 
new code. The code attempts to mimic readline in how it grows input buffers and bash in how it 
grows arrays and other things it uses realloc on.

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

/* Emulator of the method used by djgpp 2.03's realloc. */
static void *
old_realloc (void *ptr, size_t old_size, size_t new_size)
{
  void *new_ptr = malloc (new_size);
  memcpy (new_ptr, ptr, old_size);
  free (ptr);
  return new_ptr;
}

int main(int argc, char **argv)
{
  int realloc_delta, realloc_method;
  char *ptr;
  size_t alloc_size;
  int i;
  time_t elapsed;

  /* The first argument holds the amount to increase the allocation by.  */
  realloc_delta = (argc > 1) ? atoi (argv[1]) : 32;
  /* The second argument when present dictates which realloc method is used. */
  realloc_method = (argc > 2 && argv[2][0] == 'n') ? 1 : 0;
  alloc_size = realloc_delta;

  ptr = malloc (alloc_size);

  i = 0;
  clock();
  
  while (i < 10000)
  {
    if (realloc_method)
      ptr = realloc (ptr, alloc_size + realloc_delta);
    else
      ptr = old_realloc (ptr, alloc_size, alloc_size + realloc_delta);
    ++i;
    alloc_size += realloc_delta;
  }

  elapsed = clock();

  printf("Time spent in realloc: %f\n", (float)elapsed / (float)CLOCKS_PER_SEC);
  return 0;
}

(time is in seconds)

Current realloc method output (32 byte increase):
Time spent in realloc: 11.428571

New realloc method output (32 byte increase):
Time spent in realloc: 0.054945

Increasing the delta to 128 bytes made the difference even more dramatic:
Time spent in realloc: 50.549451 (old)

Time spent in realloc: 0.054945 (new)

The improvement is so dramatic it seems too good to be believe without confirmation. So I'd like 
to know if there's anything obviously wrong with the test. If not, I can post an updated realloc 
patch for review.

Mark

- Raw text -


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