Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 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; 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. int h=
  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. if (tablica[k] = x)
  44. return true;
  45. if (tablica[k] == -1)
  46. return false;
  47. }
  48. return false;
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54. std::default_random_engine gen(std::random_device{}());
  55. const int N = 1000000;
  56. int *tablica = new int[N];
  57. for (int i = 0; i < N; i++)
  58. {
  59. tablica[i] = -1;
  60. }
  61.  
  62. const int M = 100000;
  63. int* tab = new int[M]; // tablica losowych liczb
  64. for (int i = 0; i < M; i++)
  65. {
  66. tab[i] = losowa_liczba();
  67. }
  68.  
  69. /*for (int x = 0; x <= 9; x++)
  70. {
  71. auto start = std::chrono::high_resolution_clock::now();
  72. for (int i = (M/10)*x; i <(M/10)*x+n; i++)
  73. {
  74. has_al_wstaw(tab[i], tablica, N);
  75. }
  76. auto end = std::chrono::high_resolution_clock::now();
  77. std::chrono::duration<double, std::micro> duration = end - start;
  78. cerr << "Uplynelo: " << duration.count() / 10000 << "us" << " " << 10 * x << "%" <<" "<<wywolan/n<< endl;
  79.  
  80. for (int i = (N / 10)*x + n; i < (N / 10)*x; i++)
  81. {
  82. has_al_wstaw(losowa_liczba(), tablica, N);
  83. }
  84. }*/
  85.  
  86. for (int y = 0;y < 10;y++)
  87. {
  88. wywolan = 0;
  89. auto start = std::chrono::high_resolution_clock::now();
  90. for (int i = (M / 10)*y; i <(M / 10)*y + n; i++)
  91. {
  92. has_al_wstaw(tab[i], tablica, N);
  93. }
  94. auto end = std::chrono::high_resolution_clock::now();
  95. std::chrono::duration<double, std::micro> duration = end - start;
  96. cout << "wstawianie" << endl;
  97. cerr << "Uplynelo: " << duration.count() / 10000 << "us" << " " << 10 * y << "%" << " " << wywolan / n << endl;
  98.  
  99.  
  100.  
  101. if (y > 1)
  102. {
  103. shuffle(tab, tab + (y + 1) * 10000, gen);
  104. }
  105. wywolan1 = 0;
  106. start = std::chrono::high_resolution_clock::now();
  107. for (int i = 0;i < 10000;i++)
  108. {
  109. hash_al_szukaj(tab[i], tablica, N);
  110. }
  111. end = std::chrono::high_resolution_clock::now();
  112. duration = end - start;
  113. cout << "szukanie" << endl;
  114. cerr << "Uplynelo: " << duration.count() / 10000 << "us" << " " << 10 * y << "%" << " " << wywolan1 / n << endl;
  115. if (y < 9)
  116. {
  117. for (int i = (N / 10)*y; i < (N / 10)*(y + 1); i++)
  118. {
  119. has_al_wstaw(losowa_liczba(), tablica, N);
  120. }
  121. }
  122. }
  123.  
  124.  
  125. delete[]tablica;
  126. system("pause");
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement