1
0
mirror of https://github.com/golang/go synced 2024-11-19 23:44:43 -07:00

image: png & jpeg encoding benchmarks

No code changes in this CL.

R=r
CC=golang-dev
https://golang.org/cl/4445074
This commit is contained in:
Brad Fitzpatrick 2011-04-29 10:42:44 -07:00
parent 2e7d6729d4
commit f5fa215d8a
2 changed files with 64 additions and 4 deletions

View File

@ -8,6 +8,8 @@ import (
"bytes"
"image"
"image/png"
"io/ioutil"
"rand"
"os"
"testing"
)
@ -85,3 +87,29 @@ func TestWriter(t *testing.T) {
}
}
}
func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(640, 480)
// Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds()
rnd := rand.New(rand.NewSource(123))
for y := bo.Min.Y; y < bo.Max.Y; y++ {
for x := bo.Min.X; x < bo.Max.X; x++ {
img.Set(x, y, image.RGBAColor{
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
uint8(rnd.Intn(256)),
255})
}
}
if !img.Opaque() {
panic("expected image to be opaque")
}
b.SetBytes(640 * 480 * 4)
b.StartTimer()
options := &Options{Quality: 90}
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img, options)
}
}

View File

@ -5,10 +5,10 @@
package png
import (
"bytes"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"testing"
)
@ -81,10 +81,42 @@ func BenchmarkEncodePaletted(b *testing.B) {
image.RGBAColor{0, 0, 0, 255},
image.RGBAColor{255, 255, 255, 255},
})
b.SetBytes(640 * 480 * 1)
b.StartTimer()
buffer := new(bytes.Buffer)
for i := 0; i < b.N; i++ {
buffer.Reset()
Encode(buffer, img)
Encode(ioutil.Discard, img)
}
}
func BenchmarkEncodeRGBOpaque(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(640, 480)
// Set all pixels to 0xFF alpha to force opaque mode.
bo := img.Bounds()
for y := bo.Min.Y; y < bo.Max.Y; y++ {
for x := bo.Min.X; x < bo.Max.X; x++ {
img.Set(x, y, image.RGBAColor{0, 0, 0, 255})
}
}
if !img.Opaque() {
panic("expected image to be opaque")
}
b.SetBytes(640 * 480 * 4)
b.StartTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img)
}
}
func BenchmarkEncodeRGBA(b *testing.B) {
b.StopTimer()
img := image.NewRGBA(640, 480)
if img.Opaque() {
panic("expected image to not be opaque")
}
b.SetBytes(640 * 480 * 4)
b.StartTimer()
for i := 0; i < b.N; i++ {
Encode(ioutil.Discard, img)
}
}