Advertisement
thornik

Bench 'Matrix mul' for D

Dec 5th, 2017
2,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.92 KB | None | 0 0
  1. // ldc2.exe -release -m64 -O matrix.d
  2.  
  3. import std.stdio;
  4. import std.conv;
  5.  
  6. const int SIZE = 30;
  7.  
  8. void init_matrix(ref int[SIZE][SIZE] m) {
  9.     int count = 1;
  10.     for (int i=0; i < SIZE; i++)
  11.         for (int j=0; j < SIZE; j++) {
  12.             m[i][j] = count++;
  13.         }
  14. }
  15.  
  16. void mmult(ref int[SIZE][SIZE] m1, ref int[SIZE][SIZE] m2, ref int[SIZE][SIZE] m3)
  17. {
  18.     for (int i=0; i < SIZE; i++) {
  19.         for (int j=0; j < SIZE; j++) {
  20.             int val = 0;
  21.             for (int k=0; k < SIZE; k++) {
  22.                 val += m1[i][k] * m2[k][j];
  23.             }
  24.             m3[i][j] = val;
  25.         }
  26.     }
  27. }
  28.  
  29. void main(string[] args) {        
  30.     int n = args.length < 2 ? 1 : to!int(args[1]);
  31.    
  32.     int[SIZE][SIZE] m1, m2, mm;
  33.     init_matrix(m1);
  34.     init_matrix(m2);
  35.  
  36.     for (int i=0; i < n; i++)
  37.         mmult(m1, m2, mm);
  38.    
  39.     writefln(`%s %s %s %s`, mm[0][0], mm[2][3], mm[3][2], mm[4][4]);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement