⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmemperf.c

📁 Extended C/C++ Dynamic Memory Control And Debug Library
💻 C
字号:
/*****************************************************************************/
/*
    XMEMPERF.C - Performance tests for extended C/C++ Dynamic Memory Control And Debug Library

    Copyright (C) Juergen Mueller (J.M.) 1987-2008
    All rights reserved.

    You are expressly prohibited from selling this software in any form
    or removing this notice.

    THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
    EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE
    IMPLIED WARRANTIES OF MERCHANTIBILITY, FITNESS FOR A PARTICULAR
    PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
    ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
    OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. THE ENTIRE RISK
    AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM AND DOCUMENTATION
    IS WITH YOU.

    Permission to modify the code and to distribute modified code is granted,
    provided the above notices are retained, and a notice that the code was
    modified is included with the above copyright notice.

    written by: Juergen Mueller, D-70806 Kornwestheim, GERMANY

    FILE       : XMEMPERF.C
    REVISION   : 05-Nov-2008
                 21:50:51
 */
/*****************************************************************************/

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

#if _MSC_VER
#define ARRAYLENGTH             100
#define LOOPS_C                 1000
#define LOOPS_CPP               1000
#elif __GNUC__
#define ARRAYLENGTH             100
#define LOOPS_C                 200
#define LOOPS_CPP               200
#else
#define ARRAYLENGTH             100
#define LOOPS_C                 1000
#define LOOPS_CPP               1000
#endif

#define MAXMEMTRACE             2       /* 0 (disabled), 1 (enabled) or 2 (enabled with copy) */

double elapsed_time_lib_c = 0.0;
double elapsed_time_lib_cpp = 0.0;
double elapsed_time_xmem_c = 0.0;
double elapsed_time_xmem_cpp = 0.0;

/*****************************************************************************/
/* */
/*****************************************************************************/
int alloc_test_direct_call(void)
{
  int   i, j;
  char *ptr_array[ARRAYLENGTH];
  clock_t  start, finish;
  double   elapsed_time;

  printf("\nXMEM test: **** direct C/C++ LIB library calls (XMEM disabled) ****");

  /****************************************************/
  /**** first loops without time measurement ****/
  /****************************************************/
  for (j = LOOPS_C; j > 0; --j)
  {
    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if ((ptr_array[i] = (char *)malloc((i + 1) * 5)) == NULL)
      {
        printf("\nerror malloc()");
      }
    } /* end for() */

    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if (ptr_array[i] != NULL)
      {
        free(ptr_array[i]);
      }
    } /* end for() */
  } /* end for() */

#ifdef __cplusplus
  for (j = LOOPS_CPP; j > 0; --j)
  {
    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if ((ptr_array[i] = (char *)new char [(i + 1) * 5]) == NULL)
      {
        printf("\nerror new []");
      }
    } /* end for() */

    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if (ptr_array[i] != NULL)
      {
        delete [] ptr_array[i];
      }
    } /* end for() */
  } /* end for() */
#endif

  printf("\nC language");
  printf("\nXMEM test: malloc/free");

  /****************************************************/
  /* */
  /****************************************************/
  start = clock();
  finish = clock();

  /****************************************************/
  /**** time measurement ****/
  /****************************************************/
  start = clock();

  for (j = LOOPS_C; j > 0; --j)
  {
    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if ((ptr_array[i] = (char *)malloc((i + 1) * 5)) == NULL)
      {
        printf("\nerror malloc()");
      }
    } /* end for() */

    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if (ptr_array[i] != NULL)
      {
        free(ptr_array[i]);
      }
    } /* end for() */
  } /* end for() */

  finish = clock();
  elapsed_time = (double)(finish - start) / (double)CLOCKS_PER_SEC;
  elapsed_time_lib_c = elapsed_time;

  printf("\n  Elapsed time for %lu malloc/free (t(LIB)): %.4f seconds.",
         (unsigned long)LOOPS_C * (unsigned long)ARRAYLENGTH,
         elapsed_time
        );
  fflush(stdout);

#ifdef __cplusplus
  printf("\nC++ language");
  printf("\nXMEM test: new/delete");

  /****************************************************/
  /* time measurement */
  /****************************************************/
  start = clock();

  for (j = LOOPS_CPP; j > 0; --j)
  {
    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if ((ptr_array[i] = (char *)new char [(i + 1) * 5]) == NULL)
      {
        printf("\nerror new []");
      }
    } /* end for() */

    for (i = 0; i < ARRAYLENGTH; ++i)
    {
      if (ptr_array[i] != NULL)
      {
        delete [] ptr_array[i];
      }
    } /* end for() */
  } /* end for() */

  finish = clock();
  elapsed_time = (double)(finish - start) / (double)CLOCKS_PER_SEC;
  elapsed_time_lib_cpp = elapsed_time;

  printf("\n  Elapsed time for %lu new/delete (t(LIB)): %.4f seconds.",
         (unsigned long)LOOPS_CPP * (unsigned long)ARRAYLENGTH,
         elapsed_time
        );
  fflush(stdout);
#endif

  printf("\n");
  return(0);
}

/*****************************************************************************/
/* redirect allocation to XMEM */
/*****************************************************************************/

#include "xmem.h"

#if (XMEM == 0)
#error XMEM disabled, performance test not possible
#endif

/*****************************************************************************/
/* */
/*****************************************************************************/
int alloc_test_xmemdebuglib_call(void)
{
  int   i, j, memtrace;
  char *ptr_array[ARRAYLENGTH];
  clock_t  start, finish;
  double   elapsed_time, factor;

  printf("\nXMEM test: **** calls via XMEM debug library (XMEM enabled) ****");

  /****************************************************/
  /**** loop over all memtrace values ****/
  /****************************************************/
  for (memtrace = 0; memtrace <= MAXMEMTRACE; ++memtrace)
  {
    xmem_set_memtrace(memtrace);        /* enable XMEM */
    printf("\nC language");
    printf("\nXMEM test: malloc/free, memtrace = %d (%s)", 
           memtrace,
           (memtrace == 0) ? "disabled"
                           : (memtrace == 1) ? "enabled"
                                             : "enabled with copy"
          );

    /****************************************************/
    /**** time measurement ****/
    /****************************************************/
    start = clock();

    for (j = LOOPS_C; j > 0; --j)
    {
      for (i = 0; i < ARRAYLENGTH; ++i)
      {
        if ((ptr_array[i] = (char *)malloc((i + 1) * 5)) == NULL)
        {
          printf("\nerror malloc()");
        }
      } /* end for() */

      for (i = 0; i < ARRAYLENGTH; ++i)
      {
        if (ptr_array[i] != NULL)
        {
          free(ptr_array[i]);
        }
      } /* end for() */
    } /* end for() */

    finish = clock();
    elapsed_time = (double)(finish - start) / (double)CLOCKS_PER_SEC;
    elapsed_time_xmem_c = elapsed_time;

    printf("\n  Elapsed time for %lu malloc/free (t(XMEM)): %.4f seconds.",
           (unsigned long)LOOPS_C * (unsigned long)ARRAYLENGTH,
           elapsed_time
          );
    printf("\n  Performance factor: t(XMEM)/t(LIB) = ");

    if (elapsed_time_lib_c != 0.0)
    { /* factor > 1: slower, factor < 1: faster */
      factor = elapsed_time_xmem_c / elapsed_time_lib_c;
      printf("%.3f", factor);

      if ( factor > 1.0)
      {
        printf(" (%.2f%% slower)", (factor - 1.0) * 100.0);
      }
      else if ( factor < 1.0)
      {
        printf(" (%.2f%% faster)", (1.0 - factor) * 100.0);
      }
      else
      {
        printf(" (identical)");
      }
    }
    else
    {
      printf("not available");
    }

    fflush(stdout);
    xmem_disable_print();
    xmem_check_free();            /* clean XMEM */
    xmem_enable_print();
  } /* end for() */

#ifdef __cplusplus
  /****************************************************/
  /**** loop over all memtrace values ****/
  /****************************************************/
  for (memtrace = 0; memtrace <= MAXMEMTRACE; ++memtrace)
  {
    xmem_set_memtrace(memtrace);        /* enable XMEM */
    printf("\nC++ language");
    printf("\nXMEM test: new/delete, memtrace = %d (%s)", 
           memtrace,
           (memtrace == 0) ? "disabled"
                           : (memtrace == 1) ? "enabled"
                                             : "enabled with copy"
          );

    /****************************************************/
    /**** time measurement ****/
    /****************************************************/
    start = clock();

    for (j = LOOPS_CPP; j > 0; --j)
    {
      for (i = 0; i < ARRAYLENGTH; ++i)
      {
        if ((ptr_array[i] = (char *)new char [(i + 1) * 5]) == NULL)
        {
          printf("\nerror new []");
        }
      } /* end for() */

      for (i = 0; i < ARRAYLENGTH; ++i)
      {
        if (ptr_array[i] != NULL)
        {
          delete [] ptr_array[i];
        }
      } /* end for() */
    } /* end for() */

    finish = clock();
    elapsed_time = (double)(finish - start) / (double)CLOCKS_PER_SEC;
    elapsed_time_xmem_cpp = elapsed_time;

    printf("\n  Elapsed time for %lu new/delete (t(XMEM)): %.4f seconds.",
           (unsigned long)LOOPS_CPP * (unsigned long)ARRAYLENGTH,
           elapsed_time
          );
    printf("\n  Performance factor: t(XMEM)/t(LIB) = ");

    if (elapsed_time_lib_cpp != 0.0)
    { /* factor > 1: slower, factor < 1: faster */
      factor = elapsed_time_xmem_cpp / elapsed_time_lib_cpp;
      printf("%.3f", factor);

      if ( factor > 1.0)
      {
        printf(" (%.2f%% slower)", (factor - 1.0) * 100.0);
      }
      else if ( factor < 1.0)
      {
        printf(" (%.2f%% faster)", (1.0 - factor) * 100.0);
      }
      else
      {
        printf(" (identical)");
      }
    }
    else
    {
      printf("not available");
    }

    fflush(stdout);
    xmem_disable_print();
    xmem_check_free();            /* clean XMEM */
    xmem_enable_print();
  } /* end for() */
#endif

  printf("\n");
  return(0);
}

/*****************************************************************************/
/* XMEM main test function */
/*****************************************************************************/
int main(int argc, char *argv[])
{
  argc = argc;
  argv = argv;

  alloc_test_direct_call();
  alloc_test_xmemdebuglib_call();

  return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -