Skip to content

Instantly share code, notes, and snippets.

@tristanz
Created October 9, 2011 20:38
Show Gist options
  • Save tristanz/1274134 to your computer and use it in GitHub Desktop.
Save tristanz/1274134 to your computer and use it in GitHub Desktop.
Boo vs C# performance
import System
// Written by Attractive Chaos; distributed under the MIT license
// To compile: mcs -optimize+ -out:matmul_v1.run matmul_v1.cs
// Translated from Java with reference to: http://code.wikia.com/wiki/Matrix_multiplication
internal class matmul_v1:
public def matgen(n as int) as (double, 2):
a as (double, 2) = matrix(double, n, n)
tmp as double = ((1.0 / n) / n)
for i in range(0, n):
for j in range(0, n):
a[i, j] = ((tmp * (i - j)) * (i + j))
return a
public def matmul(a as (double, 2), b as (double, 2)) as (double, 2):
m as int = a.GetLength(0)
n as int = a.GetLength(1)
p as int = b.GetLength(0)
x as (double, 2) = matrix(double, m, p)
c as (double, 2) = matrix(double, p, n)
for i in range(0, n):
for j in range(0, p):
// transpose
c[j, i] = b[i, j]
for i in range(0, m):
for j in range(0, p):
s = 0.0
for k in range(0, n):
s += (a[i, k] * c[j, k])
x[i, j] = s
return x
public static def Main(args as (String)):
n = 100
times = 1000
m = matmul_v1()
for i in range(0, times):
a as (double, 2)
b as (double, 2)
x as (double, 2)
a = m.matgen(n)
b = m.matgen(n)
x = m.matmul(a, b)
Console.WriteLine(x[(n / 2), (n / 2)])
matmul_v1.Main(argv)
// Written by Attractive Chaos; distributed under the MIT license
// To compile: mcs -optimize+ -out:matmul_v1.run matmul_v1.cs
// Translated from Java with reference to: http://code.wikia.com/wiki/Matrix_multiplication
using System;
class matmul_v1 {
public double[,] matgen(int n) {
double[,] a = new double[n,n];
double tmp = 1.0 / n / n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
a[i,j] = tmp * (i - j) * (i + j);
return a;
}
public double[,] matmul(double[,] a, double[,] b) {
int m = a.GetLength(0), n = a.GetLength(1), p = b.GetLength(0);
double[,] x = new double[m,p];
double[,] c = new double[p,n];
for (int i = 0; i < n; ++i) // transpose
for (int j = 0; j < p; ++j)
c[j,i] = b[i,j];
for (int i = 0; i < m; ++i)
for (int j = 0; j < p; ++j) {
double s = 0.0;
for (int k = 0; k < n; ++k)
s += a[i,k] * c[j,k];
x[i,j] = s;
}
return x;
}
public static void Main(String[] args) {
int n = 100;
int times = 1000;
matmul_v1 m = new matmul_v1();
for(int i = 0; i < times; ++i) {
double[,] a, b, x;
a = m.matgen(n);
b = m.matgen(n);
x = m.matmul(a, b);
Console.WriteLine(x[n/2,n/2]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment