#include <iostream>
#include <chrono>
#include <cstdlib>
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <data_size_in_MB>\n";
return 1;
}
// Get DATA_SIZE from command line argument
size_t DATA_SIZE = std::atol(argv[1]) * 1024 * 1024;
// Allocate memory space
char* data = new char[DATA_SIZE];
// Test memory read latency
auto start = std::chrono::steady_clock::now();
volatile char temp;
for (size_t i = 0; i < DATA_SIZE; ++i) {
temp = data[i];
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> read_latency = end - start;
// Test memory write latency
start = std::chrono::steady_clock::now();
for (size_t i = 0; i < DATA_SIZE; ++i) {
data[i] = 0;
}
end = std::chrono::steady_clock::now();
std::chrono::duration<double> write_latency = end - start;
// Test memory read bandwidth
start = std::chrono::steady_clock::now();
for (size_t i = 0; i < DATA_SIZE; ++i) {
temp = data[i];
}
end = std::chrono::steady_clock::now();
std::chrono::duration<double> read_bandwidth_time = end - start;
double read_bandwidth = (DATA_SIZE / read_bandwidth_time.count()) / (1024 * 1024); // in MB/s
// Test memory write bandwidth
start = std::chrono::steady_clock::now();
for (size_t i = 0; i < DATA_SIZE; ++i) {
data[i] = 0;
}
end = std::chrono::steady_clock::now();
std::chrono::duration<double> write_bandwidth_time = end - start;
double write_bandwidth = (DATA_SIZE / write_bandwidth_time.count()) / (1024 * 1024); // in MB/s
// Output results
std::cout << "Memory read latency: " << read_latency.count() << " seconds\n";
std::cout << "Memory write latency: " << write_latency.count() << " seconds\n";
std::cout << "Memory read bandwidth: " << read_bandwidth << " MB/s\n";
std::cout << "Memory write bandwidth: " << write_bandwidth << " MB/s\n";
// Free memory space
delete[] data;
return 0;
}