Advertisement
Guest User

252

a guest
Mar 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3.  
  4. int a[1000];
  5. int b[1000];
  6.  
  7. void setAandB()
  8. {
  9.     int i = 0;
  10.     for(i = 0; i < 1000; i++) {
  11.         a[i] = 0;
  12.         b[i] = i;
  13.     }
  14. }
  15. void bar()
  16. {        
  17.     int i = 0;        
  18.     while (i < 1000){            
  19.       a[i] += b[i++];            
  20.       a[i] += b[i++];            
  21.       a[i] += b[i++];            
  22.       a[i] += b[i++];        
  23.     }
  24. }
  25. void foo() {        
  26.     int i;        
  27.     for (i = 0; i < 1000; i++) {            
  28.         a[i] = a[i] + b[i];        
  29.     }    
  30. }
  31.  
  32. int main()
  33. {
  34.     // setAandB();
  35.     clock_t tic = clock();
  36.     bar(); //slower
  37.     clock_t toc = clock();
  38.     printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
  39.     // setAandB();
  40.     clock_t tic1 = clock();
  41.     foo(); //faster
  42.     clock_t toc1 = clock();
  43.     printf("Elapsed: %f seconds\n", (double)(toc1 - tic1) / CLOCKS_PER_SEC);
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement