// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

template <typename T, int N>
struct Test
{

static inline int Ix(int i, int j)
{
    return i * N + j;
}

static void Run()
{
    clock_t t1 = clock();
    T *a = new T[N * N];
    T *b = new T[N * N];
    T *c = new T[N * N];
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            a[Ix(i, j)] = rand();
            a[Ix(i, j)] /= RAND_MAX;
            b[Ix(i, j)] = rand();
            b[Ix(i, j)] /= RAND_MAX;
        }
    }
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            c[i * N + j] = 0;
            for(int k = 0; k < N; k++)
            {
                c[Ix(i, j)] += a[Ix(i, k)] * b[Ix(k, j)];
            }
        }
    }
    delete[] a;
    delete[] b;
    delete[] c;
    clock_t t2 = clock();
    cout << sizeof(T) << " byte FP : " << (t2 - t1) << " clock ticks for dimension " << N << endl;
}

};

int main()
{
    srand(1234567);
    Test<float, 500>::Run();
    Test<double, 500>::Run();
    Test<float, 1000>::Run();
    Test<double, 1000>::Run();
    Test<float, 2000>::Run();
    Test<double, 2000>::Run();
    return 0;
}
