dmilicev

timer1_v1.c

Aug 16th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. /*
  2.  
  3.     timer1_v1.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 multiply(int n, int m)
  16. {
  17.     int ans = 0, count = 0;
  18.     while (m)
  19.     {
  20.         if (m % 2 == 1)         // check for set bit and left
  21.             ans += n << count;  // shift n, count times
  22.  
  23.         count++;                // increment of place value (count)
  24.         m /= 2;
  25.     }
  26.     return ans;
  27. }
  28.  
  29. int main(void)
  30. {
  31.     clock_t start_time, end_time;
  32.     double elapsed_time;
  33.  
  34.     int i;
  35.     unsigned sum=1, n=100000000;    // n is the number of terms
  36.  
  37.     start_time = clock();   // start measuring time
  38.     for(i=1; i<=n; i++)
  39.     {
  40.         sum = i*10 + 1;
  41.     }
  42.     end_time = clock();     // end measuring time
  43.     elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  44.     printf("\n 1. Classically, by multiplication ");
  45.     printf("\n 1. sum = %u ", sum);
  46.     printf("\n 1. elapsed_time =    %lf s \n", elapsed_time);
  47.  
  48.  
  49.     start_time = clock();   // start measuring time
  50.     for(i=1; i<=n; i++)
  51.     {
  52.         sum = (i << 3) + i + i + 1;
  53.     }
  54.     end_time = clock();     // end measuring time
  55.     elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  56.     printf("\n 2. Using shift operator and addition ");
  57.     printf("\n 2. sum = %u ", sum);
  58.     printf("\n 2. elapsed_time =    %lf s \n", elapsed_time);
  59.  
  60.  
  61.     start_time = clock();   // start measuring time
  62.     for(i=1; i<=n; i++)
  63.     {
  64.         sum = (i << 3) + (i << 1) + 1;
  65.     }
  66.     end_time = clock();     // end measuring time
  67.     elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  68.     printf("\n 3. Using shift operator ");
  69.     printf("\n 3. sum = %u ", sum);
  70.     printf("\n 3. elapsed_time =    %lf s \n", elapsed_time);
  71.  
  72.  
  73.     start_time = clock();   // start measuring time
  74.     for(i=1; i<=n; i++)
  75.     {
  76.         sum = multiply(i,10) + 1;
  77.     }
  78.     end_time = clock();     // end measuring time
  79.     elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  80.     printf("\n 4. By calling the multiplication function ");
  81.     printf("\n 4. sum = %u ", sum);
  82.     printf("\n 4. elapsed_time =    %lf s \n", elapsed_time);
  83.  
  84.  
  85.     start_time = clock();   // start measuring time
  86.     unsigned ans = 0;
  87.     int count = 0, m=10;
  88.     for(i=1; i<=n; i++)
  89.     {
  90.         while (m)
  91.         {
  92.             if (m % 2 == 1)         // check for set bit and left
  93.                 ans += n << count;  // shift n, count times
  94.  
  95.             count++;                // increment of place value (count)
  96.             m /= 2;
  97.         }
  98.         ans = ans + 1;
  99.         count=0;
  100.         m=10;
  101.     }
  102.     end_time = clock();     // end measuring time
  103.     elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  104.     printf("\n 5. With code of multiplication function ");
  105.     printf("\n 5. sum = %u ", sum);
  106.     printf("\n 5. elapsed_time =    %lf s \n", elapsed_time);
  107.  
  108.  
  109.     return(0);
  110. }
  111.  
Add Comment
Please, Sign In to add comment