Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform.Crop performance goes bad when rect size is small #89

Open
Sora233 opened this issue Aug 22, 2021 · 0 comments
Open

transform.Crop performance goes bad when rect size is small #89

Sora233 opened this issue Aug 22, 2021 · 0 comments

Comments

@Sora233
Copy link

Sora233 commented Aug 22, 2021

my result is below.

goos: linux
goarch: amd64
pkg: test
cpu: AMD Ryzen 7 3700X 8-Core Processor
BenchmarkMyCrop-16                 14307             79645 ns/op
BenchmarkResizeCrop-16               134           8771603 ns/op

my test code is below.

const size = 50

func BenchmarkMyCrop(b *testing.B) {
	f, err := ioutil.ReadFile("img.png")
	if err != nil {
		panic(err)
	}
	dImage, _, err := image.Decode(bytes.NewReader(f))
	if err != nil {
		panic(err)
	}
	rect := image.Rect(0, 0, size, size)

	SubImage := func (img image.Image, r image.Rectangle) image.Image {
		var bgSize = image.Rect(0, 0, r.Dx(), r.Dy())
		var bg = image.NewNRGBA(bgSize)
		draw.Draw(bg, bg.Bounds(), img, r.Min, draw.Src)
		return bg
	}
	for i := 0; i < b.N; i++ {
		SubImage(dImage, rect)
	}
}

func BenchmarkResizeCrop(b *testing.B) {
	f, err := ioutil.ReadFile("img.png")
	if err != nil {
		panic(err)
	}
	dImage, _, err := image.Decode(bytes.NewReader(f))
	if err != nil {
		panic(err)
	}
	rect := image.Rect(0, 0, size, size)
	for i := 0; i < b.N; i++ {
		transform.Crop(dImage, rect)
	}
}

my test image is below.

img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant