Skip to content

Instantly share code, notes, and snippets.

@bardex
Last active November 8, 2021 15:33
Show Gist options
  • Save bardex/b3668ea77c9ad5d47dd72e01101438d6 to your computer and use it in GitHub Desktop.
Save bardex/b3668ea77c9ad5d47dd72e01101438d6 to your computer and use it in GitHub Desktop.
Benchmarks of images resize

Тест производительности библиотек для ресайза изображений на Go

Исходное изображение: JPEG 1920x1080px 707kB Обработанное изображение: JPEG 800x600px

$ go test -bench=. -benchmem  bench_test.go 
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-6700T CPU @ 2.80GHz
BenchmarkBimg-8            88          12814039 ns/op          114736 B/op         12 allocs/op
BenchmarkBild-8             4         301796907 ns/op        15247708 B/op         40 allocs/op
BenchmarkImaging-8       1506            781762 ns/op         1937763 B/op         14 allocs/op
PASS
ok      command-line-arguments  7.917s
package test
import (
"image"
_ "image/jpeg"
_ "image/png"
"testing"
"github.com/disintegration/imaging"
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/transform"
"github.com/h2non/bimg"
)
func BenchmarkBimg(b *testing.B) {
buffer, err := bimg.Read("sample.jpg")
if err != nil {
b.Fatal(err)
}
newImage := bimg.NewImage(buffer)
var newImageb []byte
for i := 0; i < b.N; i++ {
newImageb, err = newImage.ForceResize(800, 600)
if err != nil {
b.Fatal(err)
}
}
err = bimg.Write("sample_libvips.jpg", newImageb)
if err != nil {
b.Fatal(err)
}
}
func BenchmarkBild(b *testing.B) {
img, err := imgio.Open("sample.jpg")
if err != nil {
b.Fatal(err)
}
var resized *image.RGBA
for i := 0; i < b.N; i++ {
resized = transform.Resize(img, 800, 600, transform.Lanczos)
}
if err := imgio.Save("sample_purego.jpg", resized, imgio.JPEGEncoder(85)); err != nil {
b.Fatal(err)
}
}
func BenchmarkImaging(b *testing.B) {
src, err := imaging.Open("sample.jpg")
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
src = imaging.Resize(src, 800, 600, imaging.Lanczos)
}
err = imaging.Save(src, "sample_imaging.jpg", imaging.JPEGQuality(85))
if err != nil {
b.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment