fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sys/time.h>
  4. #include <float.h>
  5.  
  6. using namespace std;
  7.  
  8. struct timeval start_time,end_time;
  9.  
  10. // We shall try results with different N values
  11. static const int N=100;
  12. // We shall try results with different OFFSET values
  13. static const int OFFSET=0;
  14.  
  15. //--------------------------------------------------------------------------------------------------------------------------------------------
  16. // template <typename Left, typename Right> // Left and Right might be expressions!
  17. // class sum { // Functor to evaluate left[i]+right[i]
  18.  
  19. // private:
  20. // Left myleft; // operator[](int) is another standard operator
  21. // Right myright;
  22. // public:
  23. // sum(Left l, Right r) : myleft(l), myright(r) {}
  24.  
  25. // int operator[](int i) {return (myleft[i]+myright[i]);} // Left and right must also define operator[]
  26.  
  27.  
  28. // };
  29.  
  30. //-----------------------------------------------------
  31.  
  32. // template <typename T>
  33.  
  34. // T arrayElementAdd(T a, T b) {
  35.  
  36. // return a+b;
  37. // }
  38.  
  39.  
  40. //-----------------------------------------------------
  41. template<typename T>
  42. class array {
  43.  
  44. private:
  45. T* data;
  46.  
  47. int N;
  48. public:
  49. array( T *_data, int _N) : data(_data), N(_N) { } // Constructor
  50.  
  51. // // template <typename Left,typename Right> // Assign the result of sum<left,right>[i] to â??this arrayâ?
  52.  
  53. // void operator=(T right) { // Argument is a functor embodying left+right
  54. // data[i] = right[i];
  55. // }
  56. void operator=(const T& right) { // Argument is a functor embodying left+right
  57. for(int i=1; i<=N; i++)
  58. data[i] = right[i];
  59. }
  60.  
  61. array operator+(const array& right) {
  62. array arr = *this;
  63.  
  64. // T *arrmem = new T(N);
  65. // array<T> arr(arrmem,N);
  66.  
  67. for(int i=1; i<=N; i++)
  68. arr[i]=arr[i]/*+right[i+OFFSET]*/;
  69.  
  70. return arr;
  71. }
  72.  
  73. T& operator[](const int i) {return data[i];}
  74. };
  75.  
  76. //-------------------------------------------------------------
  77.  
  78.  
  79. int main()
  80. {
  81. int *amem = new int(N);
  82. int *bmem = new int(N);
  83. int *cmem = new int(N);
  84.  
  85. array<int> a(amem, N);
  86. array<int> b(bmem, N);
  87. array<int> c(cmem, N);
  88.  
  89. //Initialization of vectors with values from 1 to 500
  90. for (int i=1;i<=N;i++)
  91. {
  92. c[i]=i;
  93. b[i]=c[i];
  94. a[i]=b[i];
  95. }
  96.  
  97. gettimeofday(&start_time,NULL);
  98. // Burada okuduÄ?umuz ilk deÄ?ere bakalım
  99. cout<<"\n\ngettimeofday start_time (in seconds)="<<
  100. start_time.tv_sec;
  101. cout<<"\n\ngettimeofday start_time (in micro secs)="<<
  102. start_time.tv_usec;
  103.  
  104. a=b+c;
  105.  
  106. for (int i=1;i<=N-OFFSET;i++)
  107. {
  108. // // a[i]=b[i]+c[i+OFFSET];
  109. // a[i] = arrayElementAdd(b[i], c[i]);
  110. cout << "\n" << (int)a[i];
  111. }
  112.  
  113. gettimeofday(&end_time,NULL);
  114. // Burada okuduÄ?umuz son deÄ?ere bakalım
  115. cout<<"\n\ngettimeofday end_time (in seconds)="<<
  116. end_time.tv_sec;
  117. cout<<"\n\ngettimeofday end_time (in micro secs)="<<
  118. end_time.tv_usec;
  119.  
  120. //Print the resultant array.
  121. cout<<"\n\ngettimeofday wall time="<<
  122. end_time.tv_sec - start_time.tv_sec+(end_time.tv_usec-start_time.tv_usec)/1e6;
  123.  
  124. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout

gettimeofday start_time (in seconds)=1432213086

gettimeofday start_time (in micro secs)=368611
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
97
98
99
100

gettimeofday end_time (in seconds)=1432213086

gettimeofday end_time (in micro secs)=368663

gettimeofday wall time=5.2e-05