#include <iostream>
#include <chrono>
#include <cstdlib>
#include <cstring>
int main() {
constexpr size_t MAX_DATA_SIZE = 1024 * 1024 * 1; // 1 MB
constexpr size_t BLOCK_SIZES[] = {1, 2, 8, 16, 32, 64, 128, 256, 512, MAX_DATA_SIZE};
// Allocate memory space
char* data = new char[MAX_DATA_SIZE];
for (size_t blockSize : BLOCK_SIZES) {
std::chrono::duration<double> latencySum = std::chrono::duration<double>::zero();
size_t iterations = MAX_DATA_SIZE / blockSize;
for (size_t iter = 0; iter < iterations; ++iter) {
auto start = std::chrono::steady_clock::now();
// Perform read operation in blocks of specific size
for (size_t i = 0; i < MAX_DATA_SIZE; i += blockSize) {
volatile char temp = data[i]; // Read the data block
// You might want to perform some operation with 'temp' to prevent optimization
// For accurate measurement, considering using 'temp' in some meaningful way
std::memset(&temp, 0, sizeof(temp)); // An example operation to prevent optimization
}
auto end = std::chrono::steady_clock::now();
latencySum += end - start;
}
// Calculate average latency
double averageLatency = latencySum.count() / iterations;
std::cout << "Block Size: " << blockSize << " bytes - ";
std::cout << "Average Read Latency: " << averageLatency << " seconds\n";
}
// Free memory space
delete[] data;
return 0;
}