/*
 * $Header: /home/gene/library/website/docsrc/wgc/wgcperf/RCS/demo0001.c,v 395.1 2008/04/20 17:25:48 gene Exp $
 *
 * Compare the performances of malloc & Boehm when allocating
 * fixed-sized blocks of memory continuously in a loop.
 *
 * To use Boehm, compile with WITH_BOEHM.  Otherwise, you get
 * malloc/free.
 */

#include "this.h"

static int S_block_size = 28 * 1024;

static clock_t
S_Loop (void *(*alloc) (), void (*free) (), long count)
{
  clock_t start;
  long i;
  char *p;

  start = clock ();
  /*
   * Allocate blocks in a tight loop.  When compiled for
   * malloc/free, we free the block each time.  Otherwise,
   * we still call xfree (to keep the code clean)
   */
  for (i = 0; i < count; ++i) {
    p = (*alloc) (S_block_size);
    assert (p != NULL);
    (*free) (p);
  }
  return clock () - start;
}

int
main ()
{
  int rc = 0;
  long count;

  count = Iterations (S_block_size);
  ReportLine (S_Loop (&GC_malloc, &GC_free, count),
              S_Loop (&malloc, &free, count),
              count,
              "individual blocks",
              __FILE__);
  return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

/* --- end of file --- */
