Advertisement
drankinatty

C - timed read of lines from file into dynamic memory

Oct 8th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. /* C best of 10 reads (7200 rpm laptop drive):
  2.  * read/stored 25481 lines in 0.003850 sec.
  3.  * total heap usage: 25,494 allocs, 25,494 frees, 733,979 bytes allocated
  4.  * (reading, e.g. /usr/share/dict/words)
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10.  
  11. enum { MAXP = 8, MAXC = 512 };
  12.  
  13. /** reallocate block of 'psz' objects to twice 'nelem'.
  14.  *  'nelem' is updated to reflect the new allocation size.
  15.  *  exits on allocation failure.
  16.  */
  17. void *xrealloc2 (void *ptr, size_t psz, size_t *nelem)
  18. {
  19.     void *memptr = realloc ((char *)ptr, *nelem * 2 * psz);
  20.    
  21.     if (!memptr) {
  22.         fprintf (stderr, "realloc() error: virtual memory exhausted.\n");
  23.         exit (EXIT_FAILURE);
  24.     }  
  25.    
  26.     /* zero new memory (optional) */
  27.     memset ((char *)memptr + *nelem * psz, 0, *nelem * psz);
  28.     *nelem *= 2;
  29.    
  30.     return memptr;
  31. }
  32.  
  33. /** return the number of seconds (nanosecond resolution)
  34.  *  using clock_gettime/CLOCK_REALTIME
  35.  */
  36. double clkrtv (void)
  37. {
  38.     struct timespec ts;
  39.     double sec;
  40.  
  41.     clock_gettime (CLOCK_REALTIME, &ts);
  42.     sec = ts.tv_nsec;
  43.     sec /= 1e9;
  44.     sec += ts.tv_sec;
  45.  
  46.     return sec;
  47. }
  48.  
  49. int main (int argc, char **argv) {
  50.  
  51.     size_t n = 0,
  52.         size = MAXP;        /* initialize 'size' to MAXP */
  53.     double t1, t2;
  54.     char tmp[MAXC] = "",    /* tmp buffer for read of MAXC chars */
  55.         **buf = NULL;
  56.     FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
  57.  
  58.     if (!fp) {  /* validate file open for reading */
  59.         fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
  60.         return 1;
  61.     }
  62.    
  63.     /* allocate/validate 'size' ptrs to char */
  64.     if (!(buf = calloc (size, sizeof *buf))) {
  65.         fprintf (stderr, "error; virtual memory exhausted.\n");
  66.         return 1;
  67.     }
  68.    
  69.     t1 = clkrtv();
  70.     while (fgets (tmp, MAXC, fp)) {     /* read each line */
  71.         size_t l = strlen (tmp);        /* get length */
  72.         if (l && tmp[l - 1] == '\n')    /* trim '\n' */
  73.             tmp[--l] = 0;
  74.         if (!(buf[n] = malloc (l + 1))) /* allocate for line */
  75.             goto memfull;
  76.         strcpy (buf[n++], tmp);         /* copy to buf[n] */
  77.         if (n == size)                  /* realloc as required */
  78.             buf = xrealloc2 (buf, sizeof *buf, &size);
  79.     }
  80.     memfull:;
  81.     t2 = clkrtv();
  82.    
  83.     printf ("read/stored %zu lines in %.6f sec.\n", n, t2-t1);
  84.    
  85.     if (fp != stdin) fclose (fp);   /* close file if not stdin */
  86.    
  87.     for (size_t i = 0; i < n; i++)  /* free allocated memory */
  88.         free (buf[i]);
  89.     free (buf);
  90.    
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement