Skip to content

Instantly share code, notes, and snippets.

@keichi
Last active December 17, 2020 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keichi/09349b5af08c3b5a978419d8eae597af to your computer and use it in GitHub Desktop.
Save keichi/09349b5af08c3b5a978419d8eae597af to your computer and use it in GitHub Desktop.
#include <chrono>
#include <iostream>
#include <random>
const int N = 1000;
const int TRIALS = 10;
// NxN matrices A, B and C
double A[N][N], B[N][N], C[N][N];
// Calculate C = AB
void run()
{
// Implement here
}
// Check if result is correct
void check_result()
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
double sum = 0.0;
for (int k = 0; k < N; k++) {
sum += A[i][k] * B[k][j];
}
if (std::abs(C[i][j] - sum) > 1e-9) {
std::cout << "Result is incorrect!" << std::endl;
return;
}
}
}
std::cout << "Result is correct." << std::endl;
}
int main()
{
// Initialize A and B with random values
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
std::uniform_real_distribution<> dist(-1.0, 1.0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = dist(engine);
B[i][j] = dist(engine);
}
}
// Run benchmark for TRIALS
for (int i = 0; i < TRIALS; i++) {
// Start timer
const std::chrono::steady_clock::time_point start =
std::chrono::steady_clock::now();
run();
// Stop timer
const std::chrono::steady_clock::time_point end =
std::chrono::steady_clock::now();
// Calculate and print runtime
std::chrono::duration<double, std::milli> runtime = end - start;
std::cout << "runtime: " << runtime.count() << " [ms]" << std::endl;
check_result();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment