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

another faster version #1

Open
ericlagergren opened this issue Jun 3, 2021 · 1 comment
Open

another faster version #1

ericlagergren opened this issue Jun 3, 2021 · 1 comment

Comments

@ericlagergren
Copy link

ericlagergren commented Jun 3, 2021

This is very so slightly faster:

var pow10tab = [16]uint32{
	1,
	10,
	100,
	1000,
	10000,
	100000,
	1000000,
	10000000,
	100000000,
	1000000000,
}

func length(x uint32) int {
        x |= 1
	r := ((bits.Len32(x) + 1) * 1233) >> 12
	if x < pow10tab[r&15] {
		return r
	}
	return r + 1
}

I'm seeing ~0.9752 ns/op instead of ~1 ns/op.

@josharian
Copy link
Owner

Thanks! I need to do a bit of benchmarking and incorporate the latest from https://commaok.xyz/post/lookup_tables/, at which point I'll include this as a contender.

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

2 participants