Advertisement
rjcostales

test_alloc.c

Feb 15th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <locale.h>
  6.  
  7. #define MAXLINE 1024
  8. #define MAXSIZE 65536
  9.  
  10. #define ELAPSE_TIME(X) ((float) (X) / (float) CLOCKS_PER_SEC)
  11.  
  12. typedef char *str;
  13.  
  14. int read(str strings[])
  15. {
  16.     int i;
  17.     char line[MAXLINE];
  18.  
  19.     clock_t start, end;
  20.     start = clock();
  21.  
  22.     for (i = 0; fgets(line, MAXLINE, stdin); i++) {
  23. #ifdef ZERO
  24.         strings[i] = calloc(strlen(line) + 1, sizeof(char));
  25. #else
  26.         strings[i] = malloc(strlen(line) + 1);
  27. #endif
  28.         strcpy(strings[i], line);
  29.     }
  30.     end = clock();
  31.  
  32.     setlocale(LC_NUMERIC, "");
  33.     printf("execution time: %0.6f secs.\n", ELAPSE_TIME(end - start));
  34.  
  35.     return i;
  36. }
  37.  
  38. int main(int argc, char *argv[])
  39. {
  40.     str page[MAXSIZE];
  41.     int size = read(page);
  42.     printf("%d\n", size);
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement