Advertisement
dmilicev

measuring_the_print_speed_of_a_character.c

Apr 15th, 2021
835
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None
  1. /*
  2.  
  3.     measuring_the_print_speed_of_a_character.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.     clock_t start_time, end_time;
  18.     double elapsed_time1, elapsed_time2, elapsed_time3, elapsed_time4 ;
  19.  
  20.     int i;
  21.     unsigned sum=1, n=10000;    // n is the number of terms
  22.  
  23. // 1
  24.     start_time = clock();       // start measuring time
  25.     for(i=1; i<=n; i++)
  26.     {
  27.         putchar(218);
  28.         putchar(196);
  29.         putchar(191);
  30. //      putchar('\n');
  31.     }
  32.     end_time = clock();         // end measuring time
  33.     elapsed_time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  34.     printf("\n 1. putchar , elapsed_time  =  %lf s \n", elapsed_time1);
  35.  
  36.  
  37. // 2
  38.     start_time = clock();       // start measuring time
  39.     for(i=1; i<=n; i++)
  40.     {
  41.         putc(218,stdout);
  42.         putc(196,stdout);
  43.         putc(191,stdout);
  44. //      putc('\n',stdout);
  45.     }
  46.     end_time = clock();         // end measuring time
  47.     elapsed_time2 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  48.     printf("\n 2. putc , elapsed_time     =  %lf s \n", elapsed_time2);
  49.  
  50.  
  51. // 3
  52.     start_time = clock();       // start measuring time
  53.     for(i=1; i<=n; i++)
  54.     {
  55.         fputc(218,stdout);
  56.         fputc(196,stdout);
  57.         fputc(191,stdout);
  58. //      fputc('\n',stdout);
  59.     }
  60.     end_time = clock();         // end measuring time
  61.     elapsed_time3 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  62.     printf("\n 3. fputc , elapsed_time    =  %lf s \n", elapsed_time3);
  63.  
  64.  
  65. // 4
  66.     start_time = clock();       // start measuring time
  67.     for(i=1; i<=n; i++)
  68.     {
  69.         fputchar(218);
  70.         fputchar(196);
  71.         fputchar(191);
  72. //      fputchar('\n');
  73.     }
  74.     end_time = clock();         // end measuring time
  75.     elapsed_time4 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  76.     printf("\n 4. fputchar , elapsed_time =  %lf s \n\n", elapsed_time4);
  77.  
  78.  
  79.     printf("\n    Results : \n");
  80.     printf("\n 1. putchar , elapsed_time  =  %lf s \n", elapsed_time1);
  81.     printf("\n 2. putc , elapsed_time     =  %lf s \n", elapsed_time2);
  82.     printf("\n 3. fputc , elapsed_time    =  %lf s \n", elapsed_time3);
  83.     printf("\n 4. fputchar , elapsed_time =  %lf s \n", elapsed_time4);
  84.  
  85.  
  86.     return 0;
  87.  
  88. } // main()
  89.  
Advertisement
RAW Paste Data Copied
Advertisement