Advertisement
Guest User

Царю челобитная

a guest
May 7th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define N 40000000
  5. #define M 10
  6.  
  7. #include <sys/time.h>
  8. #include <stdlib.h>
  9.  
  10. double gettime ()
  11. {
  12.     struct timeval tv;
  13.     gettimeofday (&tv, NULL);
  14.     return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
  15. }
  16.  
  17. double sum1 (double *a, int n)
  18. {
  19.     int i;
  20.     double sum = 0;
  21.     for (i=0; i<n; i+=4)
  22.     {
  23.         sum += a[i+0];
  24.         sum += a[i+1];
  25.         sum += a[i+2];
  26.         sum += a[i+3];
  27.     }
  28.     return sum;
  29. }
  30.  
  31. double sum2 (double *a, int n)
  32. {
  33.     int i;
  34.     double sum1=0, sum2=0, sum3=0, sum4=0;
  35.     for (i=0; i<n; i+=4)
  36.     {
  37.         sum1 += a[i+0];
  38.         sum2 += a[i+1];
  39.         sum3 += a[i+2];
  40.         sum4 += a[i+3];
  41.     }
  42.     return sum1+sum2+sum3+sum4;
  43. }
  44.  
  45. int main ()
  46. {
  47.     double *array = malloc (N * sizeof (double));
  48.     int i;
  49.     double time;
  50.     double s;
  51.     for (i=0; i<N; i++) array[i] = 2;
  52.  
  53.     time = gettime();
  54.     s = 0;
  55.     for (i=0; i<M; i++) s += sum2 (array, N);
  56.     time = gettime() - time;
  57.     printf ("%f %f\n", s, time);
  58.  
  59.     time = gettime();
  60.     s = 0;
  61.     for (i=0; i<M; i++) s += sum1 (array, N);
  62.     time = gettime() - time;
  63.     printf ("%f %f\n", s, time);
  64.  
  65.     free (array);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement