用于说明动态分配内存运行时间有效的反例实验 【2】

文章讨论了如何重写C程序,使用数组而非动态内存分配函数malloc来存储从输入文件中读取的行,以提高程序速度。作者提到malloc在实际操作中的性能可能比传统观念认为的要好。实验中,对malloc调用的频率小,大部分是简单的指针操作,这挑战了关于避免malloc的常规建议。
摘要由CSDN通过智能技术生成

从《The.C.Programming.Language.2Nd.Ed 》5.7题出发,用参考答案开始。

“Exercise 5−7. Rewrite readlines to store lines in an array supplied by main, rather than calling alloc to maintain storage. How much faster is the program?” 

/* K&R Exercise 5-7 */
/* Steven Huang */
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
 
#define TRUE     1
#define FALSE    0
 
#define MAXLINES 5000       /* maximum number of lines */
#define MAXLEN   1000       /* maximum length of a line */
char *lineptr[MAXLINES];
char lines[MAXLINES][MAXLEN];
 
/* K&R2 p29 */
int getline(char s[], int lim)
{
  int c, i;
 
  for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
    s[i] = c;                                                         
  if (c == '\n') {
    s[i++] = c;   
  }
  s[i] = '\0';
  return i;
}
 
/* K&R2 p109 */
int readlines(char *lineptr[], int maxlines)
{
  int len, nlines;
  char *p, line[MAXLEN];
 
  nlines = 0;
  while ((len = getline(line, MAXLEN)) > 0)
    if (nlines >= maxlines || (p = malloc(len)) == NULL)
      return -1;
    else {
      line[len - 1] = '\0';  /* delete the newline */
      strcpy(p, line);
      lineptr[nlines++] = p;
    }                       
  return nlines;  
}
 
int readlines2(char lines[][MAXLEN], int maxlines)
{
  int len, nlines;
 
  nlines = 0;
  while ((len = getline(lines[nlines], MAXLEN)) > 0)
    if (nlines >= maxlines)                         
      return -1;           
    else
      lines[nlines++][len - 1] = '\0';  /* delete the newline */
  return nlines;
}
 
int main(int argc, char *argv[])
{
  /* read things into cache, to be fair. */
  readlines2(lines, MAXLINES);
 
  if (argc > 1 && *argv[1] == '2') {
    puts("readlines2()");           
    readlines2(lines, MAXLINES);
  } else {
    puts("readlines()");
    readlines(lineptr, MAXLINES);
  }
   
  return 0;
}

”Steven writes: "Unfortunately, the follow-up question here on which version is faster is difficult to determine on my machine, because the difference is very small. I can call malloc() one million times in under a second - this suggests that the conventional wisdom that malloc() is slow and should be avoided may need some more adjustment."

[Editor's note: That's probably because malloc is actually taking memory requests to the system as infrequently as possible, so that most of the calls invoke little more than pointer arithmetic. This suggests that the conventional wisdom may be based on real world programs, rather than artificial "how many mallocs per second can I do" benchmarks. :-) ]“ 

修改上面的参考答案源码,使之可以正确运行,代码如下。

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

#define TRUE     1
#define FALSE    0

#define MAXLINES 10000300     /* maximum number of lines */
#define MAXLEN   1000       /* maximum length of a line */
char *lineptr[MAXLINES];
char lines[MAXLINES][MAXLEN];

///* K&R2 p29 */
//int my_getline(char s[], int lim)
//{
//  int c, i;
//
//  for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
//    s[i] = c;
//  if (c == '\n') {
//    s[i++] = c;
//  }
//  s[i] = '\0';
//  return i;
//}

/* K&R2 p109 */
int readlines(char *lineptr[], int maxlines, FILE * file)
{
  int len, nlines;
  char *p, line[MAXLEN];
  int maxlen = MAXLEN;

  nlines = 0;
  while ((len = getline((char **)&line, (size_t *)&maxlen, file)) > 0)
    if (nlines >= maxlines || (p = malloc( len + 1 )) == NULL)
    {
      return -1;
    }
    else {
      line[len - 1] = '\0';  /* delete the newline */
      strcpy(p, line);
      lineptr[nlines++] = p;
    }
  return nlines;
}

int readlines2(char lines[][MAXLEN], int maxlines, FILE * file)
{
  int len, nlines;
  int maxlen = MAXLEN;
  nlines = 0;
  while ((len = getline((char **)lines, (size_t *)&maxlen, file)) > 0)
    if (nlines >= maxlines)
      return -1;
    else
      lines[nlines++][len - 1] = '\0';  /* delete the newline */
  return nlines;
}

int main(int argc, char *argv[])
{
  FILE * strm_file = fopen("input_data.txt", "r");
  if( strm_file == NULL )
  {
      perror("Failed to open file.");
      return 1;
  }
  /* read things into cache, to be fair. */
  printf("readlines2()=%d\n",readlines2(lines, MAXLINES, strm_file));

  rewind(strm_file);
  if (argc > 1  && *argv[1] == '2') {
    printf("readlines2()=%d\n",readlines2(lines, MAXLINES, strm_file));
  } else {
    printf("readlines()=%d\n",readlines(lineptr, MAXLINES, strm_file));
  }

  if(strm_file != NULL)
  {
    fclose(strm_file);
  }
  return 0;
}

目前还有段错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Emilin Amy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值