Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4.  
  5. #define N 1000
  6.  
  7. double array_sum(double a[N][N])
  8. {
  9. int i,j;
  10. double s;
  11. s=0;
  12. for (i=0;i<N;++i)
  13. {
  14. for (j=0;j<N;++j)
  15. {
  16. s += a[i][j];
  17. }
  18. }
  19. return s;
  20. }
  21.  
  22. double array_sum2(double a[N][N])
  23. {
  24. int i,j;
  25. double s;
  26. s=0;
  27. for (j=0;j<N;++j)
  28. {
  29. for (i=0;i<N;++i)
  30. {
  31. s += a[i][j];
  32. }
  33. }
  34. return s;
  35. }
  36.  
  37. int main(int argc, char** argv)
  38. {
  39. using namespace std;
  40. double a[N][N];
  41. int i,j;
  42.  
  43. for (j=0;j<N;++j)
  44. {
  45. for (i=0;i<N;++i)
  46. {
  47. a[i][j] = 0.01;
  48. }
  49. }
  50.  
  51. {
  52. double sum = 0.0;
  53. clock_t begin = clock();
  54. for (i=0;i<2000;++i)
  55. {
  56. sum+=array_sum(a);
  57. }
  58. clock_t end = clock();
  59. std::cout << "sum = " << sum << std::endl;
  60. double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  61. std::cout << "elapsed_secs " << elapsed_secs << std::endl;
  62. }
  63.  
  64. {
  65. double sum = 0.0;
  66. clock_t begin = clock();
  67. for (i=0;i<2000;++i)
  68. {
  69. sum+=array_sum2(a);
  70. }
  71. clock_t end = clock();
  72. std::cout << "sum = " << sum << std::endl;
  73. double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  74. std::cout << "elapsed_secs " << elapsed_secs << std::endl;
  75. }
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement