Advertisement
avp210159

malltime.c test malloc strategies

Feb 7th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. // читаем построчно файлы из арг. ком. стр. 2-мя разными способами malloc/realloc
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <sys/time.h>
  7.  
  8. #define LIMIT 100000000
  9.  
  10. double getusec()
  11. {
  12.   struct timeval tm;
  13.  
  14.   gettimeofday(&tm, 0);
  15.   return tm.tv_sec * 1.0e6 + tm.tv_usec;
  16. }
  17.  
  18. char *
  19. getaline (FILE *in)
  20. {
  21. // ./a.out /usr/include/*.h
  22. #if QUICK // read 1403382 bytes in 39654 lines in 145 files (131.525 msec) maxcap: 3415 (2796)
  23.   static int cap = 0;
  24.   static char *str = 0;
  25. #else // read 1403382 bytes in 39654 lines in 145 files (152.583 msec) maxcap: 3415 (2796)
  26.   int cap = 0;
  27.   char *str = 0;
  28. #endif // сам удивился, что не влияет
  29.   int c,  n = 0, incr = 10;
  30.  
  31.   if (feof(in))
  32.     return 0;
  33.   while ((c = fgetc(in)) != EOF) {
  34.     if (n + 2 > cap) {
  35.       str = realloc(str, (cap += incr));
  36. #if QUICK
  37.       incr <<= 1;
  38. #endif
  39.     }
  40.     str[n++] = c;
  41.     if (c == '\n')
  42.       break;
  43.   }
  44.   if (n) {
  45.     str[n] = 0;
  46. #if QUICK
  47.     char *t = malloc(n + 1);
  48.     memcpy(t, str, n + 1);
  49.     return t;
  50. #else
  51.     return realloc(str, n + 1);
  52. #endif
  53.   }
  54.   return 0;
  55. }
  56.  
  57. int
  58. read_filines (FILE *in, int *lines, int *pcap, int *pn)
  59. {
  60.   int i, size = 0, acap = 0, n = 0, incr = 300;
  61.   char *str, **arr = 0;
  62.  
  63.   while (str = getaline(in)) {
  64.     size += strlen(str);
  65.     if (n == acap) {
  66.       arr = realloc(arr, (acap += incr) * sizeof(*arr));
  67.       if ((incr = acap / 2) > 10000)
  68.     incr = 16000;
  69.     }
  70.     arr[n++] = str;
  71.   }
  72.   (*lines) += n;
  73.  
  74.   for (i = 0; i < n; i++)
  75.     free(arr[i]);
  76.   free(arr);
  77.  
  78.   *pn = n;
  79.   *pcap = acap;
  80.   return size;
  81. }
  82.  
  83. int
  84. main (int ac, char *av[])
  85. {
  86. #if QUICK
  87.   puts("Quick test");
  88. #else
  89.   puts("Slow test");
  90. #endif
  91.   double t1 = getusec();
  92.   FILE *in;
  93.   int i, n = 0, tot = 0, size = 0, cap, maxcap = 0, na, maxna = 0;
  94.  
  95.  
  96.   for (i = 1; i < ac; i++) {
  97.     if (in = fopen(av[i], "r")) {
  98.       n++;
  99.       size += read_filines(in, &tot, &cap, &na);
  100.       if (cap > maxcap)
  101.     maxcap = cap;
  102.       if (na > maxna)
  103.     maxna = na;
  104.       if (size > LIMIT)
  105.     break;
  106.       fclose(in);
  107.     } else
  108.       perror(av[i]);
  109.   }
  110.  
  111.   printf ("read %d bytes in %d lines in %d files (%.3f msec) maxcap: %d (%d)\n",
  112.       size, tot, n, (getusec() - t1) / 1000, maxcap, maxna);
  113.   return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement