Advertisement
dmilicev

measuring_the_print_speed_of_a_string.c

Apr 15th, 2021
448
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     measuring_the_print_speed_of_a_string.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <time.h>               // clock_t, clock(), CLOCKS_PER_SEC
  14.  
  15. int main(void)
  16. {
  17.     char string[] = "This is a string.\n";
  18.     clock_t start_time, end_time;
  19.     double elapsed_time1, elapsed_time2, elapsed_time3, elapsed_time4 ;
  20.  
  21.     int i;
  22.     unsigned sum=1, n=10000;    // n is the number of terms
  23.  
  24. // 1
  25.     start_time = clock();       // start measuring time
  26.     for(i=1; i<=n; i++)
  27.     {
  28.         printf("%s", string);
  29.     }
  30.     end_time = clock();         // end measuring time
  31.     elapsed_time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  32.     printf("\n 1. printf , elapsed_time  =  %lf s \n", elapsed_time1);
  33.  
  34.  
  35. // 2
  36.     start_time = clock();       // start measuring time
  37.     for(i=1; i<=n; i++)
  38.     {
  39.         fprintf(stdout,"%s",string);
  40. //      putc('\n',stdout);
  41.     }
  42.     end_time = clock();         // end measuring time
  43.     elapsed_time2 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  44.     printf("\n 2. fprintf , elapsed_time     =  %lf s \n", elapsed_time2);
  45.  
  46.  
  47. // 3
  48.     start_time = clock();       // start measuring time
  49.     for(i=1; i<=n; i++)
  50.     {
  51.         puts(string);
  52.     }
  53.     end_time = clock();         // end measuring time
  54.     elapsed_time3 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  55.     printf("\n 3. puts , elapsed_time    =  %lf s \n", elapsed_time3);
  56.  
  57.  
  58. // 4
  59.     start_time = clock();       // start measuring time
  60.     for(i=1; i<=n; i++)
  61.     {
  62.         fputs(string,stdout);
  63.     }
  64.     end_time = clock();         // end measuring time
  65.     elapsed_time4 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  66.     printf("\n 4. fputs , elapsed_time =  %lf s \n\n", elapsed_time4);
  67.  
  68.  
  69.     printf("\n    Results : \n");
  70.     printf("\n 1. printf , elapsed_time  =  %lf s \n", elapsed_time1);
  71.     printf("\n 2. fprintf , elapsed_time =  %lf s \n", elapsed_time2);
  72.     printf("\n 3. puts , elapsed_time    =  %lf s \n", elapsed_time3);
  73.     printf("\n 4. fputs , elapsed_time   =  %lf s \n", elapsed_time4);
  74.  
  75.  
  76.     return 0;
  77.  
  78. } // main()
  79.  
Advertisement
RAW Paste Data Copied
Advertisement