github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/spec.tengo (about) 1 A := func (i, j) { 2 return ((i+j)*(i+j+1)/2 + i + 1) 3 } 4 5 TimesVec := func(v, s, n, u) { 6 ul := len(u) 7 for i := s; i < n; i++ { 8 vi := 0 9 for j := 0; j < ul; j++ { 10 vi = vi + u[j] / A(i, j) 11 } 12 v[i] = vi 13 } 14 } 15 16 TimesTranspVec := func(v, s, n, u) { 17 ul := len(u) 18 for i := s; i < n; i++ { 19 vi := 0 20 for j := 0; j < ul; j++ { 21 vi = vi + u[j] / A(j, i) 22 } 23 v[i] = vi 24 } 25 } 26 27 n := 2000 28 xx := [] 29 for idx := 0; idx < n; idx++ { 30 xx = append(xx, 0.0) 31 } 32 33 ATimesTransp := func(v, u) { 34 x := copy(xx) 35 TimesVec(x, 0, len(v), u) 36 TimesTranspVec(v, 0, len(v), x) 37 } 38 39 u := [] 40 v := [] 41 42 for idx := 0; idx < n; idx++ { 43 u = append(u, 1.0) 44 v = append(v, 0.0) 45 } 46 47 for i := 0; i < 10; i++ { 48 ATimesTransp(v, u) 49 ATimesTransp(u, v) 50 } 51 52 vBv := 0 53 vv := 0 54 for i := 0 ; i < len(v); i++ { 55 vBv = vBv + u[i] * v[i] 56 vv = vv + v[i] * v[i] 57 } 58 59 import("fmt").println(import("math").sqrt(vBv/vv)) 60