File: benchmark_dense_matop_test.go

package info (click to toggle)
golang-github-gorgonia-tensor 0.9.24-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,696 kB
  • sloc: sh: 18; asm: 18; makefile: 8
file content (144 lines) | stat: -rw-r--r-- 2,708 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package tensor

import (
	"math/rand"
	"testing"
)

func BenchmarkDense_Transpose(b *testing.B) {
	T := New(WithShape(100, 100, 2), WithBacking(Range(Byte, 0, 100*100*2)))
	for i := 0; i < b.N; i++ {
		T.T()
		T.Transpose()
	}
}

func BenchmarkNativeSet(b *testing.B) {
	T := New(WithShape(10000), Of(Float64))
	data := T.Data().([]float64)
	for i := 0; i < b.N; i++ {
		for next := 0; next < 10000; next++ {
			data[next] = float64(next + 1)
		}
	}
}

func BenchmarkSetMethod(b *testing.B) {
	T := New(WithShape(10000), Of(Float64))
	for i := 0; i < b.N; i++ {
		for next := 0; next < 10000; next++ {
			T.Set(next, float64(next+1))
		}
	}
}

func BenchmarkNativeGet(b *testing.B) {
	T := New(WithShape(10000), Of(Float64))
	data := T.Data().([]float64)
	var f float64
	for i := 0; i < b.N; i++ {
		for next := 0; next < 10000; next++ {
			f = data[next]
		}
	}
	_ = f
}

func BenchmarkGetMethod(b *testing.B) {
	T := New(WithShape(10000), Of(Float64))
	var f float64
	for i := 0; i < b.N; i++ {
		for next := 0; next < 10000; next++ {
			f = T.Get(next).(float64)
		}
	}
	_ = f
}

func BenchmarkGetWithIterator(b *testing.B) {
	T := New(WithShape(100, 100), Of(Float64))
	var f float64
	data := T.Data().([]float64)
	for i := 0; i < b.N; i++ {
		it := IteratorFromDense(T)
		var next int
		var err error
		for next, err = it.Start(); err == nil; next, err = it.Next() {
			f = data[next]
		}
		if _, ok := err.(NoOpError); !ok {
			b.Errorf("Error: %v", err)
		}
	}
	_ = f
}

func BenchmarkComplicatedGet(b *testing.B) {
	T := New(WithShape(101, 1, 36, 5), Of(Float64))
	T.T(0, 2, 1, 3)
	data := T.Data().([]float64)
	var f float64
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		it := IteratorFromDense(T)
		var next int

		var err error
		for next, err = it.Start(); err == nil; next, err = it.Next() {
			f = data[next]
		}
		if _, ok := err.(NoOpError); !ok {
			b.Errorf("Error: %v", err)
		}
	}
	_ = f
}

var atCoords [10000][2]int

func init() {
	for i := range atCoords {
		atCoords[i][0] = rand.Intn(100)
		atCoords[i][1] = rand.Intn(100)
	}
}

var at1, at2 float64

// func BenchmarkAtWithNativeIterator(b *testing.B) {
// 	T := New(WithShape(100, 100), Of(Float64))
// 	it, err := NativeMatrixF64(T)
// 	if err != nil {
// 		b.Fatalf("Error: %v", err)
// 	}

// 	var j int
// 	for i := 0; i < b.N; i++ {

// 		if j >= len(atCoords) {
// 			j = 0
// 		}

// 		at := atCoords[j]
// 		at1 = it[at[0]][at[1]]
// 		j++
// 	}
// }

func BenchmarkAt(b *testing.B) {
	T := New(WithShape(100, 100), Of(Float64))
	var j int
	for i := 0; i < b.N; i++ {
		if j >= len(atCoords) {
			j = 0
		}

		at := atCoords[j]
		_, err := T.At(at[0], at[1])
		if err != nil {
			b.Errorf("Error: %v", err)
		}

		j++
	}
}