Debian Codegraph @

benchmark_dense_matop_test.go

raw
1 package tensor
2
3 import (
4 "math/rand"
5 "testing"
6 )
7
8 func BenchmarkDense_Transpose(b *testing.B) {
9 T := New(WithShape(100, 100, 2), WithBacking(Range(Byte, 0, 100*100*2)))
10 for i := 0; i < b.N; i++ {
11 T.T()
12 T.Transpose()
13 }
14 }
15
16 func BenchmarkNativeSet(b *testing.B) {
17 T := New(WithShape(10000), Of(Float64))
18 data := T.Data().([]float64)
19 for i := 0; i < b.N; i++ {
20 for next := 0; next < 10000; next++ {
21 data[next] = float64(next + 1)
22 }
23 }
24 }
25
26 func BenchmarkSetMethod(b *testing.B) {
27 T := New(WithShape(10000), Of(Float64))
28 for i := 0; i < b.N; i++ {
29 for next := 0; next < 10000; next++ {
30 T.Set(next, float64(next+1))
31 }
32 }
33 }
34
35 func BenchmarkNativeGet(b *testing.B) {
36 T := New(WithShape(10000), Of(Float64))
37 data := T.Data().([]float64)
38 var f float64
39 for i := 0; i < b.N; i++ {
40 for next := 0; next < 10000; next++ {
41 f = data[next]
42 }
43 }
44 _ = f
45 }
46
47 func BenchmarkGetMethod(b *testing.B) {
48 T := New(WithShape(10000), Of(Float64))
49 var f float64
50 for i := 0; i < b.N; i++ {
51 for next := 0; next < 10000; next++ {
52 f = T.Get(next).(float64)
53 }
54 }
55 _ = f
56 }
57
58 func BenchmarkGetWithIterator(b *testing.B) {
59 T := New(WithShape(100, 100), Of(Float64))
60 var f float64
61 data := T.Data().([]float64)
62 for i := 0; i < b.N; i++ {
63 it := IteratorFromDense(T)
64 var next int
65 var err error
66 for next, err = it.Start(); err == nil; next, err = it.Next() {
67 f = data[next]
68 }
69 if _, ok := err.(NoOpError); !ok {
70 b.Errorf("Error: %v", err)
71 }
72 }
73 _ = f
74 }
75
76 func BenchmarkComplicatedGet(b *testing.B) {
77 T := New(WithShape(101, 1, 36, 5), Of(Float64))
78 T.T(0, 2, 1, 3)
79 data := T.Data().([]float64)
80 var f float64
81 b.ResetTimer()
82 for i := 0; i < b.N; i++ {
83 it := IteratorFromDense(T)
84 var next int
85
86 var err error
87 for next, err = it.Start(); err == nil; next, err = it.Next() {
88 f = data[next]
89 }
90 if _, ok := err.(NoOpError); !ok {
91 b.Errorf("Error: %v", err)
92 }
93 }
94 _ = f
95 }
96
97 var atCoords [10000][2]int
98
99 func init() {
100 for i := range atCoords {
101 atCoords[i][0] = rand.Intn(100)
102 atCoords[i][1] = rand.Intn(100)
103 }
104 }
105
106 var at1, at2 float64
107
108 // func BenchmarkAtWithNativeIterator(b *testing.B) {
109 // T := New(WithShape(100, 100), Of(Float64))
110 // it, err := NativeMatrixF64(T)
111 // if err != nil {
112 // b.Fatalf("Error: %v", err)
113 // }
114
115 // var j int
116 // for i := 0; i < b.N; i++ {
117
118 // if j >= len(atCoords) {
119 // j = 0
120 // }
121
122 // at := atCoords[j]
123 // at1 = it[at[0]][at[1]]
124 // j++
125 // }
126 // }
127
128 func BenchmarkAt(b *testing.B) {
129 T := New(WithShape(100, 100), Of(Float64))
130 var j int
131 for i := 0; i < b.N; i++ {
132 if j >= len(atCoords) {
133 j = 0
134 }
135
136 at := atCoords[j]
137 _, err := T.At(at[0], at[1])
138 if err != nil {
139 b.Errorf("Error: %v", err)
140 }
141
142 j++
143 }
144 }