Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <algorithm>
- #include <functional>
- #include <array>
- #include <xmmintrin.h> // _mm_prefetch((const char*)block, _MM_HINT_T0); // has no effect
- #define TRASH(INDEX, SIZE) int __trash##INDEX[SIZE];
- //#define TRASH(INDEX, SIZE)
- const int numTries = 1000;
- const int blocksNum = 9000;
- struct Block
- {
- TRASH(0, 5);
- int id;
- TRASH(1, 5);
- float position[3];
- TRASH(2, 5);
- int type;
- TRASH(3, 5);
- int material;
- TRASH(4, 5);
- };
- struct BlockNoTrash
- {
- int id;
- float position[3];
- int type;
- int material;
- };
- void fillWithRandomData(std::vector<std::shared_ptr<Block>>& blocks)
- {
- const int randSeed = 5168487;
- std::srand(randSeed);
- for (int i = 0; i < blocksNum; ++i)
- {
- auto block = std::make_shared<Block>();
- block->id = i;
- block->position[0] = std::rand() / 1000.0f;
- block->position[1] = std::rand() / 1000.0f;
- block->position[2] = std::rand() / 1000.0f;
- block->type = std::rand() % 50;
- block->material = std::rand() % 10;
- blocks.push_back(block);
- }
- }
- int main()
- {
- {
- std::clock_t total = 0;
- for (int t = 0; t < numTries; ++t)
- {
- std::vector<std::shared_ptr<Block>> blocks;
- fillWithRandomData(blocks);
- std::clock_t begin = std::clock();
- {
- struct {
- bool operator()(std::shared_ptr<Block>& a, std::shared_ptr<Block>& b) const
- {
- if (a->id != b->id) return a->id < b->id;
- if (a->position[0] != b->position[0]) return a->position[0] < b->position[0];
- if (a->position[1] != b->position[1]) return a->position[1] < b->position[1];
- if (a->position[2] != b->position[2]) return a->position[2] < b->position[2];
- if (a->type != b->type) return a->type < b->type;
- if (a->material != b->material) return a->material < b->material;
- return false;
- }
- } customLess;
- std::sort(blocks.begin(), blocks.end(), customLess);
- }
- std::clock_t end = std::clock();
- total += end - begin;
- }
- double elapsedSecs = double(total) / CLOCKS_PER_SEC;
- std::cout << "elapsedSecs: " << elapsedSecs << "\n";
- }
- {
- std::clock_t total = 0;
- for (int t = 0; t < numTries; ++t)
- {
- std::vector<std::shared_ptr<Block>> blocks;
- fillWithRandomData(blocks);
- std::clock_t begin = std::clock();
- {
- BlockNoTrash* blocksNoTrash = new BlockNoTrash[blocksNum];
- for (int i = 0; i < blocksNum; ++i)
- {
- std::shared_ptr<Block>& block = blocks[i];
- BlockNoTrash& blockNoTrash = blocksNoTrash[i];
- blockNoTrash.id = block->id;
- blockNoTrash.position[0] = block->position[0];
- blockNoTrash.position[1] = block->position[1];
- blockNoTrash.position[2] = block->position[2];
- blockNoTrash.type = block->type;
- blockNoTrash.material = block->material;
- }
- {
- struct {
- bool operator()(BlockNoTrash& a, BlockNoTrash& b) const
- {
- if (a.id != b.id) return a.id < b.id;
- if (a.position[0] != b.position[0]) return a.position[0] < b.position[0];
- if (a.position[1] != b.position[1]) return a.position[1] < b.position[1];
- if (a.position[2] != b.position[2]) return a.position[2] < b.position[2];
- if (a.type != b.type) return a.type < b.type;
- if (a.material != b.material) return a.material < b.material;
- return false;
- }
- } customLess;
- std::sort(blocksNoTrash, blocksNoTrash + blocksNum, customLess);
- }
- delete[] blocksNoTrash;
- }
- std::clock_t end = std::clock();
- total += end - begin;
- }
- double elapsedSecs = double(total) / CLOCKS_PER_SEC;
- std::cout << "elapsedSecs: " << elapsedSecs << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement