Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <vector>
- #include <iomanip>
- #include <windows.h>
- #include <string>
- #include <sstream>
- #include <new>
- constexpr size_t N = 48;
- constexpr size_t CYCLES = 5000;
- constexpr size_t K = 2;
- constexpr size_t L3 = 3 * 1024 * 1024;
- constexpr size_t L2 = 256 * 1024;
- constexpr size_t L1 = 32 * 1024;
- __declspec(align(64)) struct u64_8
- {
- uint64_t u0;
- uint64_t u1;
- uint64_t u2;
- uint64_t u3;
- uint64_t u4;
- uint64_t u5;
- uint64_t u6;
- uint64_t u7;
- };
- std::vector<std::chrono::high_resolution_clock::duration> test_cache(const size_t size)
- {
- std::vector<std::chrono::high_resolution_clock::duration> results;
- results.reserve(N - 2);
- const size_t offset = (size * K) / sizeof(u64_8);
- u64_8* const mas = (u64_8*)_aligned_malloc(sizeof(u64_8) * offset * N, 64);
- for (size_t n = 2; n <= N; ++n)
- {
- const size_t block_size = (size / sizeof(u64_8)) / n / 2;
- for (size_t i = 0; i < n; ++i)
- {
- for (size_t j = 0; j < block_size; ++j)
- {
- if (i == n - 1)
- {
- if (j == block_size - 1)
- mas[i * offset + j].u0 = 0;
- else
- mas[i * offset + j].u0 = j + 1;
- }
- else
- {
- mas[i * offset + j].u0 = (i + 1) * offset + j;
- }
- }
- }
- uint64_t t = 0;
- std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
- start = std::chrono::high_resolution_clock::now();
- for (int i = 0; i < size / sizeof(u64_8) / n / 2 * n * CYCLES; ++i)
- {
- t = mas[t].u0;
- }
- end = std::chrono::high_resolution_clock::now();
- printf("%d", t);
- results.push_back(end - start);
- }
- _aligned_free(mas);
- return results;
- }
- int main()
- {
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- std::vector<std::chrono::high_resolution_clock::duration> results[3];
- results[0] = test_cache(L3);
- std::cout << "L3" << std::endl;
- for (int i = 0; i < results[0].size(); ++i)
- {
- std::cout << "N: " << std::setw(2) << i + 2 << " Time: " << std::setw(16) << results[0][i].count() << " ns" << std::endl;
- }
- results[1] = test_cache(L2);
- std::cout << "L2" << std::endl;
- for (int i = 0; i < results[1].size(); ++i)
- {
- std::cout << "N: " << std::setw(2) << i + 2 << " Time: " << std::setw(16) << results[1][i].count() << " ns" << std::endl;
- }
- results[2] = test_cache(L1);
- std::cout << "L1" << std::endl;
- for (int i = 0; i < results[2].size(); ++i)
- {
- std::cout << "N: " << std::setw(2) << i + 2 << " Time: " << std::setw(16) << results[2][i].count() << " ns" << std::endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement