Skip to content

Instantly share code, notes, and snippets.

@fekberg
Last active December 19, 2015 04:39
Show Gist options
  • Select an option

  • Save fekberg/5899017 to your computer and use it in GitHub Desktop.

Select an option

Save fekberg/5899017 to your computer and use it in GitHub Desktop.
// http://channel9.msdn.com/Events/Build/2013/4-329
int N = 1000;
var A = new int[N,N];
var B = new int[N, N];
var C = new int[N, N];
var random = new Random();
for(int x = 0; x < N; x++)
{
for(int y = 0; y < N; y++)
{
A[x, y] = random.Next();
B[x, y] = random.Next();
C[x, y] = random.Next();
}
}
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
C[i,j] += A[i,k] * B[k, j];
watch.Start();
Console.WriteLine("Time: {0}ms", watch.ElapsedMilliseconds);
watch.Reset();
watch.Start();
// 60% Faster than the loop above!
for (int i = 0; i < N; i++)
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
C[i, j] += A[i, k] * B[k, j];
watch.Start();
Console.WriteLine("Time: {0}ms", watch.ElapsedMilliseconds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment