fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <ctime>
  5. #define DIM 256
  6. #define LIMIT 1000000
  7.  
  8. using namespace std;
  9.  
  10. class Vector
  11. {
  12. private:
  13. const int size;
  14. int *array;
  15.  
  16. public:
  17. Vector(const int new_size) : size(new_size), array(new int [new_size]) {}
  18.  
  19. // copy constructor
  20. Vector(const Vector &rhs) : size(rhs.size)
  21. {
  22. // deep copy is required
  23. array = new int [rhs.size];
  24. memcpy(array, rhs.array, rhs.size * sizeof(int));
  25. }
  26.  
  27. int& operator[] (const int index)
  28. {
  29. return array[index];
  30. }
  31.  
  32. int operator[] (const int index) const
  33. {
  34. return array[index];
  35. }
  36.  
  37. Vector& operator+= (const Vector &rhs)
  38. {
  39. for(int index=0; index<size; ++index)
  40. array[index] += rhs[index];
  41. return *this;
  42. }
  43.  
  44. Vector operator+ (const Vector &rhs) const
  45. {
  46. Vector temp(*this);
  47. temp += rhs;
  48. return temp;
  49. }
  50.  
  51. ~Vector() { delete [] array; }
  52. };
  53.  
  54. double runtime_without_move_semantic (const int iterative_limit)
  55. {
  56. clock_t begin, end;
  57. Vector a(DIM), b(DIM), c(DIM);
  58.  
  59. for(int i=0; i<DIM; ++i) {
  60. a[i] = 0;
  61. b[i] = 1;
  62. c[i] = 2;
  63. }
  64.  
  65. begin = clock();
  66.  
  67. for (int iter; iter<iterative_limit; ++iter) {
  68. Vector d = a+b+c;
  69. d[iter&DIM] = 2;
  70. }
  71.  
  72. end = clock();
  73.  
  74. return (static_cast<double>(end-begin) / static_cast<double>(CLOCKS_PER_SEC) );
  75. }
  76.  
  77. int main()
  78. {
  79. cout << runtime_without_move_semantic(LIMIT) << '\n';
  80. return 0;
  81. }
Success #stdin #stdout 1.17s 3028KB
stdin
Standard input is empty
stdout
1.17