mit-ocw

Source code for MIT-OCW coursework
git clone git://git.laack.co/mit-ocw.git
Log | Files | Refs

mat_mul.go (635B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"math/rand"
      6 	"time"
      7 )
      8 
      9 const n = 400
     10 
     11 func randInit(arr [n][n]float32) [n][n]float32 {
     12 
     13 	for i := range len(arr) {
     14 		for j := range len(arr[i]) {
     15 			arr[i][j] = rand.Float32()
     16 		}
     17 	}
     18 
     19 	return arr
     20 
     21 }
     22 
     23 func main(){
     24 
     25 
     26 	A := [n][n]float32{}
     27 	B := [n][n]float32{}
     28 	C := [n][n]float32{}
     29 
     30 
     31 	A = randInit(A)
     32 	B = randInit(B)
     33 
     34 	start := time.Now()
     35 
     36 	for i := range A {
     37 		for k := range B {
     38 			for j := range B {
     39 				C[i][j] += A[i][k] * B[k][j]
     40 			}
     41 		}
     42 	}
     43 
     44 	end := time.Now()
     45 
     46 	fmt.Println(end.Sub(start).Seconds())
     47 }
     48 
     49 // This runs in:
     50 
     51 // 1: 0.048146089
     52 // 2: 0.047572412
     53 // 3: 0.047875806
     54 
     55 // AVG: 0.047864769