Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <chrono>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <windows.h>
  7. #include <string>
  8. #include <sstream>
  9. #include <new>
  10.  
  11.  
  12. constexpr size_t N = 48;
  13. constexpr size_t CYCLES = 5000;
  14. constexpr size_t K = 2;
  15. constexpr size_t L3 = 3 * 1024 * 1024;
  16. constexpr size_t L2 = 256 * 1024;
  17. constexpr size_t L1 = 32 * 1024;
  18.  
  19. __declspec(align(64)) struct u64_8
  20. {
  21. uint64_t u0;
  22. uint64_t u1;
  23. uint64_t u2;
  24. uint64_t u3;
  25. uint64_t u4;
  26. uint64_t u5;
  27. uint64_t u6;
  28. uint64_t u7;
  29. };
  30.  
  31. std::vector<std::chrono::high_resolution_clock::duration> test_cache(const size_t size)
  32. {
  33. std::vector<std::chrono::high_resolution_clock::duration> results;
  34. results.reserve(N - 2);
  35.  
  36. const size_t offset = (size * K) / sizeof(u64_8);
  37. u64_8* const mas = (u64_8*)_aligned_malloc(sizeof(u64_8) * offset * N, 64);
  38.  
  39. for (size_t n = 2; n <= N; ++n)
  40. {
  41. const size_t block_size = (size / sizeof(u64_8)) / n / 2;
  42. for (size_t i = 0; i < n; ++i)
  43. {
  44. for (size_t j = 0; j < block_size; ++j)
  45. {
  46. if (i == n - 1)
  47. {
  48. if (j == block_size - 1)
  49. mas[i * offset + j].u0 = 0;
  50. else
  51. mas[i * offset + j].u0 = j + 1;
  52. }
  53. else
  54. {
  55. mas[i * offset + j].u0 = (i + 1) * offset + j;
  56. }
  57. }
  58. }
  59.  
  60. uint64_t t = 0;
  61. std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
  62. start = std::chrono::high_resolution_clock::now();
  63. for (int i = 0; i < size / sizeof(u64_8) / n / 2 * n * CYCLES; ++i)
  64. {
  65. t = mas[t].u0;
  66. }
  67. end = std::chrono::high_resolution_clock::now();
  68. printf("%d", t);
  69. results.push_back(end - start);
  70. }
  71. _aligned_free(mas);
  72. return results;
  73. }
  74.  
  75. int main()
  76. {
  77. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  78.  
  79. std::vector<std::chrono::high_resolution_clock::duration> results[3];
  80.  
  81. results[0] = test_cache(L3);
  82.  
  83. std::cout << "L3" << std::endl;
  84. for (int i = 0; i < results[0].size(); ++i)
  85. {
  86. std::cout << "N: " << std::setw(2) << i + 2 << " Time: " << std::setw(16) << results[0][i].count() << " ns" << std::endl;
  87. }
  88.  
  89. results[1] = test_cache(L2);
  90.  
  91. std::cout << "L2" << std::endl;
  92. for (int i = 0; i < results[1].size(); ++i)
  93. {
  94. std::cout << "N: " << std::setw(2) << i + 2 << " Time: " << std::setw(16) << results[1][i].count() << " ns" << std::endl;
  95. }
  96.  
  97. results[2] = test_cache(L1);
  98.  
  99. std::cout << "L1" << std::endl;
  100. for (int i = 0; i < results[2].size(); ++i)
  101. {
  102. std::cout << "N: " << std::setw(2) << i + 2 << " Time: " << std::setw(16) << results[2][i].count() << " ns" << std::endl;
  103. }
  104.  
  105.  
  106.  
  107. system("pause");
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement