Skip to content

Commit 4039be0

Browse files
iandbradfitz
authored andcommitted
image: add benchmarks for At and Set methods
Added in preparation for looking at some optimizations around bounds checks. BenchmarkAt/rgba-8 100000000 18.5 ns/op 4 B/op 1 allocs/op BenchmarkAt/rgba64-8 100000000 22.9 ns/op 8 B/op 1 allocs/op BenchmarkAt/nrgba-8 100000000 18.8 ns/op 4 B/op 1 allocs/op BenchmarkAt/nrgba64-8 100000000 22.1 ns/op 8 B/op 1 allocs/op BenchmarkAt/alpha-8 100000000 14.6 ns/op 1 B/op 1 allocs/op BenchmarkAt/alpha16-8 200000000 6.46 ns/op 0 B/op 0 allocs/op BenchmarkAt/gray-8 100000000 14.3 ns/op 1 B/op 1 allocs/op BenchmarkAt/gray16-8 200000000 6.45 ns/op 0 B/op 0 allocs/op BenchmarkAt/paletted-8 300000000 4.28 ns/op 0 B/op 0 allocs/op BenchmarkSet/rgba-8 50000000 39.2 ns/op 8 B/op 2 allocs/op BenchmarkSet/rgba64-8 30000000 45.8 ns/op 16 B/op 2 allocs/op BenchmarkSet/nrgba-8 50000000 39.3 ns/op 8 B/op 2 allocs/op BenchmarkSet/nrgba64-8 30000000 45.6 ns/op 16 B/op 2 allocs/op BenchmarkSet/alpha-8 50000000 34.5 ns/op 2 B/op 2 allocs/op BenchmarkSet/alpha16-8 50000000 34.9 ns/op 4 B/op 2 allocs/op BenchmarkSet/gray-8 100000000 20.3 ns/op 1 B/op 1 allocs/op BenchmarkSet/gray16-8 50000000 36.2 ns/op 4 B/op 2 allocs/op BenchmarkSet/paletted-8 50000000 39.5 ns/op 1 B/op 1 allocs/op BenchmarkRGBAAt-8 500000000 3.74 ns/op BenchmarkRGBASetRGBA-8 300000000 4.33 ns/op BenchmarkRGBA64At-8 300000000 5.06 ns/op BenchmarkRGBA64SetRGBA64-8 200000000 6.61 ns/op BenchmarkNRGBAAt-8 500000000 3.69 ns/op BenchmarkNRGBASetNRGBA-8 300000000 4.06 ns/op BenchmarkNRGBA64At-8 300000000 4.98 ns/op BenchmarkNRGBA64SetNRGBA64-8 200000000 6.62 ns/op BenchmarkAlphaAt-8 2000000000 1.43 ns/op BenchmarkAlphaSetAlpha-8 2000000000 1.55 ns/op BenchmarkAlpha16At-8 1000000000 2.87 ns/op BenchmarkAlphaSetAlpha16-8 500000000 3.27 ns/op BenchmarkGrayAt-8 2000000000 1.43 ns/op BenchmarkGraySetGray-8 2000000000 1.55 ns/op BenchmarkGray16At-8 1000000000 2.87 ns/op BenchmarkGraySetGray16-8 500000000 3.14 ns/op Updates #14884 Change-Id: I349fb214ee75f13ecbc62ac22a40e3b337648f60 Reviewed-on: https://go-review.googlesource.com/136796 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 9774fa6 commit 4039be0

File tree

1 file changed

+195
-15
lines changed

1 file changed

+195
-15
lines changed

src/image/image_test.go

Lines changed: 195 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@ func cmp(cm color.Model, c0, c1 color.Color) bool {
2222
return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
2323
}
2424

25+
var testImages = []struct {
26+
name string
27+
image image
28+
}{
29+
{"rgba", NewRGBA(Rect(0, 0, 10, 10))},
30+
{"rgba64", NewRGBA64(Rect(0, 0, 10, 10))},
31+
{"nrgba", NewNRGBA(Rect(0, 0, 10, 10))},
32+
{"nrgba64", NewNRGBA64(Rect(0, 0, 10, 10))},
33+
{"alpha", NewAlpha(Rect(0, 0, 10, 10))},
34+
{"alpha16", NewAlpha16(Rect(0, 0, 10, 10))},
35+
{"gray", NewGray(Rect(0, 0, 10, 10))},
36+
{"gray16", NewGray16(Rect(0, 0, 10, 10))},
37+
{"paletted", NewPaletted(Rect(0, 0, 10, 10), color.Palette{
38+
Transparent,
39+
Opaque,
40+
})},
41+
}
42+
2543
func TestImage(t *testing.T) {
26-
testImage := []image{
27-
NewRGBA(Rect(0, 0, 10, 10)),
28-
NewRGBA64(Rect(0, 0, 10, 10)),
29-
NewNRGBA(Rect(0, 0, 10, 10)),
30-
NewNRGBA64(Rect(0, 0, 10, 10)),
31-
NewAlpha(Rect(0, 0, 10, 10)),
32-
NewAlpha16(Rect(0, 0, 10, 10)),
33-
NewGray(Rect(0, 0, 10, 10)),
34-
NewGray16(Rect(0, 0, 10, 10)),
35-
NewPaletted(Rect(0, 0, 10, 10), color.Palette{
36-
Transparent,
37-
Opaque,
38-
}),
39-
}
40-
for _, m := range testImage {
44+
for _, tc := range testImages {
45+
m := tc.image
4146
if !Rect(0, 0, 10, 10).Eq(m.Bounds()) {
4247
t.Errorf("%T: want bounds %v, got %v", m, Rect(0, 0, 10, 10), m.Bounds())
4348
continue
@@ -111,3 +116,178 @@ func Test16BitsPerColorChannel(t *testing.T) {
111116
}
112117
}
113118
}
119+
120+
func BenchmarkAt(b *testing.B) {
121+
for _, tc := range testImages {
122+
b.Run(tc.name, func(b *testing.B) {
123+
b.ReportAllocs()
124+
for i := 0; i < b.N; i++ {
125+
tc.image.At(4, 5)
126+
}
127+
})
128+
}
129+
}
130+
131+
func BenchmarkSet(b *testing.B) {
132+
c := color.Gray{0xff}
133+
for _, tc := range testImages {
134+
b.Run(tc.name, func(b *testing.B) {
135+
b.ReportAllocs()
136+
for i := 0; i < b.N; i++ {
137+
tc.image.Set(4, 5, c)
138+
}
139+
})
140+
}
141+
}
142+
143+
func BenchmarkRGBAAt(b *testing.B) {
144+
m := NewRGBA(Rect(0, 0, 10, 10))
145+
b.ResetTimer()
146+
147+
for i := 0; i < b.N; i++ {
148+
m.RGBAAt(4, 5)
149+
}
150+
}
151+
152+
func BenchmarkRGBASetRGBA(b *testing.B) {
153+
m := NewRGBA(Rect(0, 0, 10, 10))
154+
c := color.RGBA{0xff, 0xff, 0xff, 0x13}
155+
b.ResetTimer()
156+
157+
for i := 0; i < b.N; i++ {
158+
m.SetRGBA(4, 5, c)
159+
}
160+
}
161+
162+
func BenchmarkRGBA64At(b *testing.B) {
163+
m := NewRGBA64(Rect(0, 0, 10, 10))
164+
b.ResetTimer()
165+
166+
for i := 0; i < b.N; i++ {
167+
m.RGBA64At(4, 5)
168+
}
169+
}
170+
171+
func BenchmarkRGBA64SetRGBA64(b *testing.B) {
172+
m := NewRGBA64(Rect(0, 0, 10, 10))
173+
c := color.RGBA64{0xffff, 0xffff, 0xffff, 0x1357}
174+
b.ResetTimer()
175+
176+
for i := 0; i < b.N; i++ {
177+
m.SetRGBA64(4, 5, c)
178+
}
179+
}
180+
181+
func BenchmarkNRGBAAt(b *testing.B) {
182+
m := NewNRGBA(Rect(0, 0, 10, 10))
183+
b.ResetTimer()
184+
185+
for i := 0; i < b.N; i++ {
186+
m.NRGBAAt(4, 5)
187+
}
188+
}
189+
190+
func BenchmarkNRGBASetNRGBA(b *testing.B) {
191+
m := NewNRGBA(Rect(0, 0, 10, 10))
192+
c := color.NRGBA{0xff, 0xff, 0xff, 0x13}
193+
b.ResetTimer()
194+
195+
for i := 0; i < b.N; i++ {
196+
m.SetNRGBA(4, 5, c)
197+
}
198+
}
199+
200+
func BenchmarkNRGBA64At(b *testing.B) {
201+
m := NewNRGBA64(Rect(0, 0, 10, 10))
202+
b.ResetTimer()
203+
204+
for i := 0; i < b.N; i++ {
205+
m.NRGBA64At(4, 5)
206+
}
207+
}
208+
209+
func BenchmarkNRGBA64SetNRGBA64(b *testing.B) {
210+
m := NewNRGBA64(Rect(0, 0, 10, 10))
211+
c := color.NRGBA64{0xffff, 0xffff, 0xffff, 0x1357}
212+
b.ResetTimer()
213+
214+
for i := 0; i < b.N; i++ {
215+
m.SetNRGBA64(4, 5, c)
216+
}
217+
}
218+
219+
func BenchmarkAlphaAt(b *testing.B) {
220+
m := NewAlpha(Rect(0, 0, 10, 10))
221+
b.ResetTimer()
222+
223+
for i := 0; i < b.N; i++ {
224+
m.AlphaAt(4, 5)
225+
}
226+
}
227+
228+
func BenchmarkAlphaSetAlpha(b *testing.B) {
229+
m := NewAlpha(Rect(0, 0, 10, 10))
230+
c := color.Alpha{0x13}
231+
b.ResetTimer()
232+
233+
for i := 0; i < b.N; i++ {
234+
m.SetAlpha(4, 5, c)
235+
}
236+
}
237+
238+
func BenchmarkAlpha16At(b *testing.B) {
239+
m := NewAlpha16(Rect(0, 0, 10, 10))
240+
b.ResetTimer()
241+
242+
for i := 0; i < b.N; i++ {
243+
m.Alpha16At(4, 5)
244+
}
245+
}
246+
247+
func BenchmarkAlphaSetAlpha16(b *testing.B) {
248+
m := NewAlpha16(Rect(0, 0, 10, 10))
249+
c := color.Alpha16{0x13}
250+
b.ResetTimer()
251+
252+
for i := 0; i < b.N; i++ {
253+
m.SetAlpha16(4, 5, c)
254+
}
255+
}
256+
257+
func BenchmarkGrayAt(b *testing.B) {
258+
m := NewGray(Rect(0, 0, 10, 10))
259+
b.ResetTimer()
260+
261+
for i := 0; i < b.N; i++ {
262+
m.GrayAt(4, 5)
263+
}
264+
}
265+
266+
func BenchmarkGraySetGray(b *testing.B) {
267+
m := NewGray(Rect(0, 0, 10, 10))
268+
c := color.Gray{0x13}
269+
b.ResetTimer()
270+
271+
for i := 0; i < b.N; i++ {
272+
m.SetGray(4, 5, c)
273+
}
274+
}
275+
276+
func BenchmarkGray16At(b *testing.B) {
277+
m := NewGray16(Rect(0, 0, 10, 10))
278+
b.ResetTimer()
279+
280+
for i := 0; i < b.N; i++ {
281+
m.Gray16At(4, 5)
282+
}
283+
}
284+
285+
func BenchmarkGraySetGray16(b *testing.B) {
286+
m := NewGray16(Rect(0, 0, 10, 10))
287+
c := color.Gray16{0x13}
288+
b.ResetTimer()
289+
290+
for i := 0; i < b.N; i++ {
291+
m.SetGray16(4, 5, c)
292+
}
293+
}

0 commit comments

Comments
 (0)