Skip to content

Instantly share code, notes, and snippets.

@johnwalley
Last active August 29, 2015 14:05
Show Gist options
  • Save johnwalley/eaa38c1852e94c0683aa to your computer and use it in GitHub Desktop.
Save johnwalley/eaa38c1852e94c0683aa to your computer and use it in GitHub Desktop.
Simple matrix-vector multiplication
using System;
using System.Diagnostics;
namespace MatrixVectorMultiply
{
class MatrixVectorMultiply2
{
static void Main(string[] args)
{
const int dim = 1024 * 8;
var matrix = new float[dim, dim];
var vecIn = new float[dim];
var vecOut = new float[dim];
Init(dim, matrix, vecIn);
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
vecOut[i] += matrix[i, j] * vecIn[i];
}
}
watch.Stop();
Console.WriteLine("Elapsed time for example #2: " + watch.ElapsedMilliseconds + " ms");
}
private static void Init(int dim, float[,] matrix, float[] vecIn)
{
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
matrix[i, j] = i*j;
}
}
for (int i = 0; i < dim; i++)
{
vecIn[i] = i;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment