Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. void straight (int mas[], int n)
  11. {
  12. for (int i = 0; i < n; i++)
  13. {
  14. mas[i] = mas[i] + 17;
  15. }
  16. }
  17. void back (int mas[], int n)
  18. {
  19. for (int i = n; i > 0; i--)
  20. {
  21. mas[i] = mas[i] + 17;
  22. }
  23. }
  24. void random (int mas[], int n)
  25. {
  26. random_shuffle(mas, mas+n);
  27.  
  28. for (int i = 0; i < n; i++)
  29. {
  30. mas[i] = mas[i] + 17;
  31. }
  32. }
  33.  
  34. int viling (int mas[], int k)
  35. {
  36. for (int i = 0; i < k; i++)
  37. {
  38. mas[i] = rand() % 20;
  39. return mas[i];
  40. }
  41. }
  42.  
  43. int output1( int mas[], int n)
  44. {
  45. straight(mas, n); // прогревание кэша
  46. unsigned int start_time = clock();
  47. for (int j = 0; j < 1000; j++)
  48. {
  49. straight(mas, n);
  50. }
  51. unsigned int end_time = clock();
  52. unsigned int search_time = end_time-start_time;
  53. return search_time;
  54. }
  55. int output2(int mas[], int n)
  56. {
  57. back(mas, n); // прогревание кэша
  58. unsigned int start_time = clock();
  59. for (int j = 0; j < 1000; j++)
  60. {
  61. straight(mas, n);
  62. }
  63. unsigned int end_time = clock();
  64. unsigned int search_time = end_time - start_time;
  65. return search_time;
  66. }
  67. int output3(int mas[], int n)
  68. {
  69. random(mas, n); // прогревание кэша
  70.  
  71. unsigned int start_time = clock();
  72. for (int j = 0; j < 1000; j++)
  73. {
  74. straight(mas, n);
  75. }
  76. unsigned int end_time = clock();
  77. unsigned int search_time = end_time - start_time;
  78. return search_time;
  79. }
  80.  
  81. int main()
  82. { // 1 int = 4 байт, 1kb = 1024 b
  83. int mas[7]; //массив размерности в байтах
  84. int n1 = 32768; int r1 = 128; // 128 kb = 131072 b (1/2 от первого уровня)
  85. int n = 2359296; int r5 = 9216; // 9216 kb = 9437184 b (3/2 третьего уровня)
  86.  
  87. vector <int> random_walk;
  88. vector <int> back_walk;
  89. vector <int> straight_walk;
  90.  
  91. for (int i = 1; i < 8; i++)
  92. {
  93. mas[i - 1] = (r1 * i * 1024) / 4;
  94. }
  95.  
  96. srand(time(0));
  97.  
  98. for (int j = 0; j < 7; j++)
  99. {
  100. int *array = new int[mas[j]]; //создание массива
  101.  
  102. viling(array, mas[j]); //заполнение массива рандомными числами
  103.  
  104. random_walk.push_back(output3(array, mas[j])); //случайный обход
  105. back_walk.push_back(output3(array, mas[j])); //обратный обход
  106. straight_walk.push_back(output3(array, mas[j])); //прямой обход
  107.  
  108. delete[] array;
  109. }
  110.  
  111. cout << "investigation:" << endl;
  112. cout << " travel_variant: straight " << endl;
  113. cout << " experiments:" << endl;
  114. for (int i = 0; i < 7; i++)
  115. {
  116. cout << " - experiment:" << endl;
  117. cout << " number: " << i+1 << endl;
  118. cout << " input_data:" << endl;
  119. cout << " buffer_size: '" << r1 << "' kb" << endl;
  120. cout << " results:" << endl;
  121. cout << " duration: " << straight_walk[i] << " ms" << endl;
  122. r1 = r1 * 2;
  123. }
  124. cout << endl;
  125. cout << "investigation:" << endl;
  126. cout << " travel_variant: back " << endl;
  127. cout << " experiments:" << endl;
  128. r1 = 128;
  129. for (int i = 0; i < 7; i++)
  130. {
  131. cout << " - experiment:" << endl;
  132. cout << " number: " << i + 1 << endl;
  133. cout << " input_data:" << endl;
  134. cout << " buffer_size: '" << r1 << "' kb" << endl;
  135. cout << " results:" << endl;
  136. cout << " duration: " << back_walk[i] << " ms" << endl;
  137. r1 = r1 * 2;
  138. }
  139. cout << endl;
  140. cout << "investigation:" << endl;
  141. cout << " travel_variant: random " << endl;
  142. cout << " experiments:" << endl;
  143. r1 = 128;
  144. for (int i = 0; i < 7; i++)
  145. {
  146. cout << " - experiment:" << endl;
  147. cout << " number: " << i + 1 << endl;
  148. cout << " input_data:" << endl;
  149. cout << " buffer_size: '" << r1 << "' kb" << endl;
  150. cout << " results:" << endl;
  151. cout << " duration: " << random_walk[i] << " ms" << endl;
  152. r1 = r1 * 2;
  153. }
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement