Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <algorithm>
  5. #include <functional>
  6. #include <array>
  7. #include <xmmintrin.h> // _mm_prefetch((const char*)block, _MM_HINT_T0); // has no effect
  8.  
  9.  
  10. #define TRASH(INDEX, SIZE) int __trash##INDEX[SIZE];
  11. //#define TRASH(INDEX, SIZE)
  12.  
  13. const int numTries = 1000;
  14. const int blocksNum = 9000;
  15.  
  16. struct Block
  17. {
  18. TRASH(0, 5);
  19. int id;
  20. TRASH(1, 5);
  21. float position[3];
  22. TRASH(2, 5);
  23. int type;
  24. TRASH(3, 5);
  25. int material;
  26. TRASH(4, 5);
  27. };
  28.  
  29. struct BlockNoTrash
  30. {
  31. int id;
  32. float position[3];
  33. int type;
  34. int material;
  35. };
  36.  
  37. void fillWithRandomData(std::vector<std::shared_ptr<Block>>& blocks)
  38. {
  39. const int randSeed = 5168487;
  40. std::srand(randSeed);
  41. for (int i = 0; i < blocksNum; ++i)
  42. {
  43. auto block = std::make_shared<Block>();
  44. block->id = i;
  45. block->position[0] = std::rand() / 1000.0f;
  46. block->position[1] = std::rand() / 1000.0f;
  47. block->position[2] = std::rand() / 1000.0f;
  48. block->type = std::rand() % 50;
  49. block->material = std::rand() % 10;
  50. blocks.push_back(block);
  51. }
  52. }
  53.  
  54. int main()
  55. {
  56.  
  57. {
  58. std::clock_t total = 0;
  59. for (int t = 0; t < numTries; ++t)
  60. {
  61. std::vector<std::shared_ptr<Block>> blocks;
  62. fillWithRandomData(blocks);
  63.  
  64. std::clock_t begin = std::clock();
  65. {
  66. struct {
  67. bool operator()(std::shared_ptr<Block>& a, std::shared_ptr<Block>& b) const
  68. {
  69. if (a->id != b->id) return a->id < b->id;
  70. if (a->position[0] != b->position[0]) return a->position[0] < b->position[0];
  71. if (a->position[1] != b->position[1]) return a->position[1] < b->position[1];
  72. if (a->position[2] != b->position[2]) return a->position[2] < b->position[2];
  73. if (a->type != b->type) return a->type < b->type;
  74. if (a->material != b->material) return a->material < b->material;
  75. return false;
  76. }
  77. } customLess;
  78. std::sort(blocks.begin(), blocks.end(), customLess);
  79. }
  80. std::clock_t end = std::clock();
  81. total += end - begin;
  82. }
  83. double elapsedSecs = double(total) / CLOCKS_PER_SEC;
  84. std::cout << "elapsedSecs: " << elapsedSecs << "\n";
  85. }
  86.  
  87. {
  88. std::clock_t total = 0;
  89. for (int t = 0; t < numTries; ++t)
  90. {
  91. std::vector<std::shared_ptr<Block>> blocks;
  92. fillWithRandomData(blocks);
  93.  
  94. std::clock_t begin = std::clock();
  95. {
  96. BlockNoTrash* blocksNoTrash = new BlockNoTrash[blocksNum];
  97.  
  98. for (int i = 0; i < blocksNum; ++i)
  99. {
  100. std::shared_ptr<Block>& block = blocks[i];
  101. BlockNoTrash& blockNoTrash = blocksNoTrash[i];
  102. blockNoTrash.id = block->id;
  103. blockNoTrash.position[0] = block->position[0];
  104. blockNoTrash.position[1] = block->position[1];
  105. blockNoTrash.position[2] = block->position[2];
  106. blockNoTrash.type = block->type;
  107. blockNoTrash.material = block->material;
  108. }
  109.  
  110. {
  111. struct {
  112. bool operator()(BlockNoTrash& a, BlockNoTrash& b) const
  113. {
  114. if (a.id != b.id) return a.id < b.id;
  115. if (a.position[0] != b.position[0]) return a.position[0] < b.position[0];
  116. if (a.position[1] != b.position[1]) return a.position[1] < b.position[1];
  117. if (a.position[2] != b.position[2]) return a.position[2] < b.position[2];
  118. if (a.type != b.type) return a.type < b.type;
  119. if (a.material != b.material) return a.material < b.material;
  120. return false;
  121. }
  122. } customLess;
  123. std::sort(blocksNoTrash, blocksNoTrash + blocksNum, customLess);
  124. }
  125.  
  126. delete[] blocksNoTrash;
  127. }
  128. std::clock_t end = std::clock();
  129. total += end - begin;
  130. }
  131. double elapsedSecs = double(total) / CLOCKS_PER_SEC;
  132. std::cout << "elapsedSecs: " << elapsedSecs << "\n";
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement