Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <chrono>
  2. #include <thread>
  3. #include <random>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. double wywolan = 0;
  9. double wywolan1 = 0;
  10. int n = 10000;
  11. int losowa_liczba(int min = 0, int max = std::numeric_limits<int>::max())
  12. {
  13. static std::default_random_engine gen(std::random_device{}());
  14. static std::uniform_int_distribution<int> dist;
  15. return dist(gen, std::uniform_int_distribution<int>::param_type{ min, max });
  16. }
  17.  
  18. bool has_al_wstaw(int x, int* tablica, int N)
  19. {
  20. int k = 0;
  21. for (int i = 0; N-1; i++)
  22. {
  23. wywolan++;
  24. //k = ((x%N) + i) % N;
  25. int h1 = x%N;
  26. int h2 =( ((x / N) % (N / 2)) * 2) + 1;
  27. k = (h1 + i*h2) % N;
  28. if (tablica[k] == -1)
  29. {
  30. tablica[k] = x;
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. bool hash_al_szukaj(int x,int *tablica, int m)
  37. {
  38. //int k = 0;
  39. for (int i = 0; i <= m - 1;i++)
  40. {
  41. wywolan1++;
  42. //k= ((x%m) + i) % m;
  43. int h1 = x%m;
  44. int h2 = (((x / m) % (m / 2)) * 2) + 1;
  45. int k = (h1 + i*h2) % m;
  46. if (tablica[k] == x)
  47. return true;
  48. if (tablica[k] == -1)
  49. return false;
  50. }
  51. return false;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57. std::default_random_engine gen(std::random_device{}());
  58. const int N = 1000000;
  59. int *tablica = new int[N];
  60. for (int i = 0; i < N; i++)
  61. {
  62. tablica[i] = -1;
  63. }
  64.  
  65. const int M = 100000;
  66. int* tab = new int[M]; // tablica losowych liczb
  67. for (int i = 0; i < M; i++)
  68. {
  69. tab[i] = losowa_liczba();
  70. }
  71.  
  72. /*for (int x = 0; x <= 9; x++)
  73. {
  74. auto start = std::chrono::high_resolution_clock::now();
  75. for (int i = (M/10)*x; i <(M/10)*x+n; i++)
  76. {
  77. has_al_wstaw(tab[i], tablica, N);
  78. }
  79. auto end = std::chrono::high_resolution_clock::now();
  80. std::chrono::duration<double, std::micro> duration = end - start;
  81. cerr << "Uplynelo: " << duration.count() / 10000 << "us" << " " << 10 * x << "%" <<" "<<wywolan/n<< endl;
  82.  
  83. for (int i = (N / 10)*x + n; i < (N / 10)*x; i++)
  84. {
  85. has_al_wstaw(losowa_liczba(), tablica, N);
  86. }
  87. }*/
  88.  
  89. for (int y = 0;y < 10;y++)
  90. {
  91. wywolan = 0;
  92. auto start = std::chrono::high_resolution_clock::now();
  93. for (int i = (M / 10)*y; i <(M / 10)*y + n; i++)
  94. {
  95. has_al_wstaw(tab[i], tablica, N);
  96. }
  97. auto end = std::chrono::high_resolution_clock::now();
  98. std::chrono::duration<double, std::micro> duration = end - start;
  99. cout << "Wstawianie" << endl;
  100. cerr << "Uplynelo: " << duration.count() / 10000 << "us" << " " << 10 * y << "%" << " " << "Srednia liczba wywolan: " << wywolan / n << endl;
  101.  
  102.  
  103.  
  104. if (y > 1)
  105. {
  106. shuffle(tab, tab + (y + 1) * 10000, gen);
  107. }
  108. wywolan1 = 0;
  109. start = std::chrono::high_resolution_clock::now();
  110. for (int i = 0;i < 10000;i++)
  111. {
  112. hash_al_szukaj(tab[i], tablica, N);
  113. }
  114. end = std::chrono::high_resolution_clock::now();
  115. duration = end - start;
  116. cout << "Szukanie" << endl;
  117. cerr << "Uplynelo: " << duration.count() / 10000 << "us" << " " << 10 * y << "%" << " " <<"Srednia liczba wywolan: "<< wywolan1 / n << endl;
  118. cout << endl;
  119. if (y < 9)
  120. {
  121. for (int i = (N / 10)*y + n; i < (N / 10)*(y + 1); i++)
  122. {
  123. has_al_wstaw(losowa_liczba(), tablica, N);
  124. }
  125. }
  126. }
  127.  
  128.  
  129. delete[]tablica;
  130. system("pause");
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement