Created
October 20, 2012 08:37
-
-
Save mraleph/3922691 to your computer and use it in GitHub Desktop.
matmul dart benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:scalarlist'; // [!] added import | |
mat_transpose(a) | |
{ | |
int m = a.length, n = a[0].length; // m rows and n cols | |
var b = new List(n); | |
for (int j = 0; j < n; ++j) b[j] = new Float64List(m); // [!] converted List<double> to Float64List | |
for (int i = 0; i < m; ++i) | |
for (int j = 0; j < n; ++j) | |
b[j][i] = a[i][j]; | |
return b; | |
} | |
mat_mul(a, b) | |
{ | |
int m = a.length, n = a[0].length, s = b.length, t = b[0].length; | |
if (n != s) return null; | |
var x = new List(m), c = mat_transpose(b); | |
for (int i = 0; i < m; ++i) { | |
x[i] = new Float64List(t); // [!] converted List<double> to Float64List | |
for (int j = 0; j < t; ++j) { | |
double sum = 0.0; | |
var ai = a[i], cj = c[j]; | |
for (int k = 0; k < n; ++k) sum += ai[k] * cj[k]; | |
x[i][j] = sum; | |
} | |
} | |
return x; | |
} | |
mat_gen(int n) | |
{ | |
var a = new List(n); | |
double t = 1.0 / n / n; | |
for (int i = 0; i < n; ++i) { | |
a[i] = new Float64List(n); // [!] converted List<double> to Float64List | |
for (int j = 0; j < n; ++j) | |
a[i][j] = t * (i - j) * (i + j); | |
} | |
return a; | |
} | |
domul(n) { | |
var a = mat_gen(n); | |
var b = mat_gen(n); | |
var c = mat_mul(a, b); | |
return c[n~/2][n~/2]; | |
} | |
// [!] added warm up | |
warmup() { | |
domul(50); | |
} | |
main() { | |
warmup(); | |
var stopwatch = new Stopwatch()..start(); | |
var result = domul(500); | |
var time = stopwatch.elapsedInMs(); | |
print("$result computed in $time ms."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
∳ dart_analyzer matmul0.dart | |
file:/Users/vegorov/matmul0.dart:20: 'int' is not assignable to 'double' | |
19: for (int j = 0; j < t; ++j) { | |
20: double sum = 0; | |
∮ dart --enable-type-checks matmul0.dart | |
Unhandled exception: | |
type 'int' is not a subtype of type 'double' of 'sum'. | |
#0 mat_mul (file:///Users/vegorov/matmul0.dart:20:17) | |
#1 main (file:///Users/vegorov/matmul0.dart:46:17) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment