Skip to content

optimize numerator() #14

@DeDeRon

Description

@DeDeRon

hello deluan,

first thank you for this very useful library.

i rewrote the numerator() function. usually one would send a pull request, but i know norhing about git (mercurial user).

so i just paste my version here:

func numerator(img *imageBinaryChannel, template *imageBinaryChannel, offsetX int, offsetY int) float64 {
	imgArray := img.zeroMeanImage
	templateArray := template.zeroMeanImage

	iidx := offsetY*img.width + offsetX
	idelta := img.width - template.width
	tidx := 0
	var sum float64 = 0
	for y := 0; y < template.height; y++ {
		for x := 0; x < template.width; x++ {
			sum += imgArray[iidx] * templateArray[tidx]
			iidx++
			tidx++
		}
		iidx += idelta
	}
	return sum
}

two big changes:
1st: i pulled constanst computations out of the loop, as they don't change.
2nd:i swapped the loop order - y outer loop, x inner loop.
this way the loop is a lot more cache friendly

the new code is not only faster but a lot simpler and easier to understand.

on my machine i got a speed improvement of 20%. hope you find this useful!

regards,

dederon

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions