Advertisement
Guest User

Final version

a guest
Apr 3rd, 2016
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <stdint.h>
  6.  
  7. #define IMAX 262144
  8. #define PAGE_SIZE 4096
  9.  
  10. static size_t bump_size(char ** buffer, size_t existing, size_t additional) {
  11.     size_t desired;
  12.     size_t ideal;
  13.  
  14.     if (existing + additional > (existing << 1)) {
  15.         desired = existing + additional;
  16.     } else {
  17.         desired = existing << 1;
  18.     }
  19.  
  20.     ideal = ((desired + PAGE_SIZE) >> 12) << 12;
  21.  
  22.     if (!((*buffer) = realloc(*buffer, ideal))) {
  23.         perror("Error increasing memory allocation. Aborting.");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.     return ideal;
  27. }
  28.  
  29.  
  30. int main() {
  31.     setbuf(stdout, NULL); //disable output buffering
  32.  
  33.     /* this is known at compile time, why not:
  34.      * const char str[] = "abcdefghefghefgh";
  35.      * ? Still, won't "cheat"... */
  36.  
  37.     /* stupid unneeded mess */
  38.     size_t str_mem = 9;
  39.     char * str = malloc(str_mem);
  40.     if(str == NULL) {
  41.         perror("Failed to allocate initial memory for str.");
  42.         return EXIT_FAILURE;
  43.     }
  44.  
  45.     (void) strcpy(str, "abcdefgh");
  46.  
  47.     /* not worth optimizing, since it's not in the loop, but could
  48.      * also, wouldn't really use bump_size here, just raw realloc
  49.      * but I am to demonstrate this is a viable general technique */
  50.     str_mem = bump_size(&str, str_mem, 8);
  51.     strcat(str, "efghefgh");
  52.    
  53.     /* end stupid unneeded mess that should have been statically declared */
  54.  
  55.     printf("%s", "exec.tm.sec\tstr.length\n");
  56.  
  57.     time_t starttime = time(NULL);
  58.     char * gstr = malloc(1);
  59.     if (!gstr) {
  60.         perror("Error allocating memory for gstr. Exiting.");
  61.         return EXIT_FAILURE;
  62.     }
  63.     gstr[0] = '\0';
  64.  
  65.     size_t gstr_len = 0;
  66.     size_t gstr_size = 1;
  67.  
  68.     char * pos_c = gstr;
  69.     size_t str_len = strlen(str);
  70.     size_t str_size = str_len + 1;
  71.  
  72.     size_t efgh_len = strlen("efgh"); // 4. To keep it general and avoid magic constants
  73.  
  74.     size_t i = 0;
  75.     while (i++ < IMAX+1000) {
  76.         if ((gstr_len + str_size) > gstr_size) {
  77.             gstr_size = bump_size(&gstr, gstr_size, str_len);
  78.         }
  79.         pos_c = gstr + gstr_len;
  80.         memcpy(pos_c, str, str_size); //strcat(gstr, str);
  81.         gstr_len += str_len;
  82.  
  83.         while((pos_c = strnstr(pos_c, "efgh", str_len))) {
  84.             memcpy(pos_c, "____", efgh_len);
  85.             pos_c += efgh_len;
  86.         }
  87.  
  88.         if (gstr_len % IMAX == 0) {
  89.             printf("%ldsec\t\t%lukb\n", time(NULL) - starttime, gstr_len / 1024);
  90.         }
  91.     }
  92.  
  93.     //printf("%s\n",gstr);
  94.     free(gstr);
  95.  
  96.     return EXIT_SUCCESS;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement