Skip to content

Instantly share code, notes, and snippets.

@dubik
Created June 16, 2018 08:12
Show Gist options
  • Save dubik/e3c003b4059b3ef18d69e39cad52229f to your computer and use it in GitHub Desktop.
Save dubik/e3c003b4059b3ef18d69e39cad52229f to your computer and use it in GitHub Desktop.
// CacheSmth.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <chrono>
#include <iostream>
struct Timer
{
Timer()
{
reset();
}
void reportAndReset(const std::string& msg)
{
const auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsedSeconds = end - m_time;
std::cout << msg.c_str() << " done in: " << elapsedSeconds.count() << "s" << std::endl;
reset();
}
void reset()
{
m_time = std::chrono::system_clock::now();
}
private:
std::chrono::system_clock::time_point m_time;
};
using namespace std;
const int kDim = 10000;
float calcRow(float* matrix)
{
float result = 0;
for (int r = 0; r < kDim; r++) {
for (int c = 0; c < kDim; c++) {
const int offset = r * kDim + c;
result += matrix[offset];
}
}
return result;
}
float calcColumn(float* matrix)
{
float result = 0;
for (int c = 0; c < kDim; c++) {
for (int r = 0; r < kDim; r++) {
const int offset = r * kDim + c;
result += matrix[offset];
}
}
return result;
}
int main()
{
float* matrix = new float[kDim * kDim];
for (int i = 0; i < kDim * kDim; i++) {
matrix[i] = 1;
}
Timer t;
const float row = calcRow(matrix);
t.reportAndReset("Row");
const float column = calcRow(matrix);
t.reportAndReset("Calc");
cout << "row: " << row << endl;
cout << "column: " << column << endl;
delete[] matrix;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment