aboutsummaryrefslogtreecommitdiff
path: root/src/image
diff options
context:
space:
mode:
authorIan Davis <nospam@iandavis.com>2018-09-22 14:23:38 +0100
committerBrad Fitzpatrick <bradfitz@golang.org>2018-09-24 17:44:11 +0000
commit4039be00a9e77bd4080ac657a940472341fa088f (patch)
tree352f56498a504faa566265bf69c44b05c3ba2ab4 /src/image
parent9774fa6f4020dba924d63991f572aa89325e1c9c (diff)
downloadgo-4039be00a9e77bd4080ac657a940472341fa088f.tar.gz
go-4039be00a9e77bd4080ac657a940472341fa088f.zip
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>
Diffstat (limited to 'src/image')
-rw-r--r--src/image/image_test.go210
1 files changed, 195 insertions, 15 deletions
diff --git a/src/image/image_test.go b/src/image/image_test.go
index 08ba61ea0c..6f49752a25 100644
--- a/src/image/image_test.go
+++ b/src/image/image_test.go
@@ -22,22 +22,27 @@ func cmp(cm color.Model, c0, c1 color.Color) bool {
return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
}
+var testImages = []struct {
+ name string
+ image image
+}{
+ {"rgba", NewRGBA(Rect(0, 0, 10, 10))},
+ {"rgba64", NewRGBA64(Rect(0, 0, 10, 10))},
+ {"nrgba", NewNRGBA(Rect(0, 0, 10, 10))},
+ {"nrgba64", NewNRGBA64(Rect(0, 0, 10, 10))},
+ {"alpha", NewAlpha(Rect(0, 0, 10, 10))},
+ {"alpha16", NewAlpha16(Rect(0, 0, 10, 10))},
+ {"gray", NewGray(Rect(0, 0, 10, 10))},
+ {"gray16", NewGray16(Rect(0, 0, 10, 10))},
+ {"paletted", NewPaletted(Rect(0, 0, 10, 10), color.Palette{
+ Transparent,
+ Opaque,
+ })},
+}
+
func TestImage(t *testing.T) {
- testImage := []image{
- NewRGBA(Rect(0, 0, 10, 10)),
- NewRGBA64(Rect(0, 0, 10, 10)),
- NewNRGBA(Rect(0, 0, 10, 10)),
- NewNRGBA64(Rect(0, 0, 10, 10)),
- NewAlpha(Rect(0, 0, 10, 10)),
- NewAlpha16(Rect(0, 0, 10, 10)),
- NewGray(Rect(0, 0, 10, 10)),
- NewGray16(Rect(0, 0, 10, 10)),
- NewPaletted(Rect(0, 0, 10, 10), color.Palette{
- Transparent,
- Opaque,
- }),
- }
- for _, m := range testImage {
+ for _, tc := range testImages {
+ m := tc.image
if !Rect(0, 0, 10, 10).Eq(m.Bounds()) {
t.Errorf("%T: want bounds %v, got %v", m, Rect(0, 0, 10, 10), m.Bounds())
continue
@@ -111,3 +116,178 @@ func Test16BitsPerColorChannel(t *testing.T) {
}
}
}
+
+func BenchmarkAt(b *testing.B) {
+ for _, tc := range testImages {
+ b.Run(tc.name, func(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ tc.image.At(4, 5)
+ }
+ })
+ }
+}
+
+func BenchmarkSet(b *testing.B) {
+ c := color.Gray{0xff}
+ for _, tc := range testImages {
+ b.Run(tc.name, func(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ tc.image.Set(4, 5, c)
+ }
+ })
+ }
+}
+
+func BenchmarkRGBAAt(b *testing.B) {
+ m := NewRGBA(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.RGBAAt(4, 5)
+ }
+}
+
+func BenchmarkRGBASetRGBA(b *testing.B) {
+ m := NewRGBA(Rect(0, 0, 10, 10))
+ c := color.RGBA{0xff, 0xff, 0xff, 0x13}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetRGBA(4, 5, c)
+ }
+}
+
+func BenchmarkRGBA64At(b *testing.B) {
+ m := NewRGBA64(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.RGBA64At(4, 5)
+ }
+}
+
+func BenchmarkRGBA64SetRGBA64(b *testing.B) {
+ m := NewRGBA64(Rect(0, 0, 10, 10))
+ c := color.RGBA64{0xffff, 0xffff, 0xffff, 0x1357}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetRGBA64(4, 5, c)
+ }
+}
+
+func BenchmarkNRGBAAt(b *testing.B) {
+ m := NewNRGBA(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.NRGBAAt(4, 5)
+ }
+}
+
+func BenchmarkNRGBASetNRGBA(b *testing.B) {
+ m := NewNRGBA(Rect(0, 0, 10, 10))
+ c := color.NRGBA{0xff, 0xff, 0xff, 0x13}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetNRGBA(4, 5, c)
+ }
+}
+
+func BenchmarkNRGBA64At(b *testing.B) {
+ m := NewNRGBA64(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.NRGBA64At(4, 5)
+ }
+}
+
+func BenchmarkNRGBA64SetNRGBA64(b *testing.B) {
+ m := NewNRGBA64(Rect(0, 0, 10, 10))
+ c := color.NRGBA64{0xffff, 0xffff, 0xffff, 0x1357}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetNRGBA64(4, 5, c)
+ }
+}
+
+func BenchmarkAlphaAt(b *testing.B) {
+ m := NewAlpha(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.AlphaAt(4, 5)
+ }
+}
+
+func BenchmarkAlphaSetAlpha(b *testing.B) {
+ m := NewAlpha(Rect(0, 0, 10, 10))
+ c := color.Alpha{0x13}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetAlpha(4, 5, c)
+ }
+}
+
+func BenchmarkAlpha16At(b *testing.B) {
+ m := NewAlpha16(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.Alpha16At(4, 5)
+ }
+}
+
+func BenchmarkAlphaSetAlpha16(b *testing.B) {
+ m := NewAlpha16(Rect(0, 0, 10, 10))
+ c := color.Alpha16{0x13}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetAlpha16(4, 5, c)
+ }
+}
+
+func BenchmarkGrayAt(b *testing.B) {
+ m := NewGray(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.GrayAt(4, 5)
+ }
+}
+
+func BenchmarkGraySetGray(b *testing.B) {
+ m := NewGray(Rect(0, 0, 10, 10))
+ c := color.Gray{0x13}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetGray(4, 5, c)
+ }
+}
+
+func BenchmarkGray16At(b *testing.B) {
+ m := NewGray16(Rect(0, 0, 10, 10))
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.Gray16At(4, 5)
+ }
+}
+
+func BenchmarkGraySetGray16(b *testing.B) {
+ m := NewGray16(Rect(0, 0, 10, 10))
+ c := color.Gray16{0x13}
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ m.SetGray16(4, 5, c)
+ }
+}