Skip to content

Instantly share code, notes, and snippets.

@akshaynanavati
Created May 21, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshaynanavati/2e39e33e27c27fe780520c31773c32ec to your computer and use it in GitHub Desktop.
Save akshaynanavati/2e39e33e27c27fe780520c31773c32ec to your computer and use it in GitHub Desktop.
#include <chrono>
#include <cstdlib>
#include <iostream>
constexpr auto MAX = 1 << 24;
char accessMemory(char *buf, size_t n) { return buf[0] ^ buf[n - 1]; }
char *align(char *buf, size_t &size, size_t alignment) {
while ((uint64_t)buf & (alignment - 1)) {
if (!size) {
throw std::exception();
}
++buf;
--size;
}
return buf;
}
int main() {
uint64_t prevTime = 0;
for (size_t n = 1; n <= MAX; n *= 2) {
size_t size = 2 * n;
char *buful = (char *)std::calloc(sizeof(char), size);
char *buf = align(buful, size, n);
char x = buf[0]; // warm l1 cache
auto now = std::chrono::steady_clock::now();
accessMemory(buf, size);
int elapsed = (std::chrono::steady_clock::now() - now).count();
std::free(buful);
/*
if (prevTime > 0 && elapsed / prevTime > 7) {
std::cout << n << std::endl;
return 0;
}
*/
std::cout << "n: " << n << " elapsed: " << elapsed << std::endl;
prevTime = elapsed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment