Skip to content

Instantly share code, notes, and snippets.

@bemasher
Created April 8, 2012 09:24
Show Gist options
  • Save bemasher/2336242 to your computer and use it in GitHub Desktop.
Save bemasher/2336242 to your computer and use it in GitHub Desktop.
Benchmark for comparing safe and unsafe methods for setting a pixel in the golang image package.
package ImageSet
import (
"image"
"image/color"
"testing"
)
const (
DIM = 128
)
func BenchmarkSafeSet(b *testing.B) {
img := image.NewGray(image.Rect(0,0,DIM,DIM))
for i := 0; i < b.N; i++ {
for x := 0; x < DIM; x++ {
for y := 0; y < DIM; y++ {
img.Set(x, y, color.Gray{128})
}
}
}
}
func BenchmarkUnSafeSet(b *testing.B) {
img := image.NewGray(image.Rect(0,0,DIM,DIM))
for i := 0; i < b.N; i++ {
for x := 0; x < DIM; x++ {
for y := 0; y < DIM; y++ {
img.Pix[img.PixOffset(x, y)] = uint8(128)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment