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

Speed up/reduce allocations by using strings.Map #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

artyom
Copy link

@artyom artyom commented Jan 15, 2022

With the following benchmark:

var sink string

func BenchmarkCreate(b *testing.B) {
	b.Run("Simple", func(b *testing.B) {
		const text = `Strings, bytes, runes and characters in Go`
		b.ReportAllocs()
		b.SetBytes(int64(len(text)))
		for i := 0; i < b.N; i++ {
			sink = sanitized_anchor_name.Create(text)
		}
	})
	b.Run("Complex", func(b *testing.B) {
		const text = `日本語; «Ёжик в тумане»?!`
		b.ReportAllocs()
		b.SetBytes(int64(len(text)))
		for i := 0; i < b.N; i++ {
			sink = sanitized_anchor_name.Create(text)
		}
	})
}

The results are:

name              old time/op    new time/op     delta
Create/Simple-8      434ns ± 0%      187ns ± 1%   -56.99%  (p=0.000 n=10+10)
Create/Complex-8     667ns ± 0%      579ns ± 0%   -13.26%  (p=0.000 n=10+10)

name              old speed      new speed       delta
Create/Simple-8   96.8MB/s ± 0%  225.0MB/s ± 1%  +132.53%  (p=0.000 n=10+10)
Create/Complex-8  61.5MB/s ± 0%   70.9MB/s ± 0%   +15.30%  (p=0.000 n=10+10)

name              old alloc/op   new alloc/op    delta
Create/Simple-8       552B ± 0%        48B ± 0%   -91.30%  (p=0.000 n=10+10)
Create/Complex-8      296B ± 0%        48B ± 0%   -83.78%  (p=0.000 n=10+10)

name              old allocs/op  new allocs/op   delta
Create/Simple-8       7.00 ± 0%       1.00 ± 0%   -85.71%  (p=0.000 n=10+10)
Create/Complex-8      6.00 ± 0%       1.00 ± 0%   -83.33%  (p=0.000 n=10+10)

With the following benchmark:

    var sink string

    func BenchmarkCreate(b *testing.B) {
        b.Run("Simple", func(b *testing.B) {
            const text = `Strings, bytes, runes and characters in Go`
            b.ReportAllocs()
            b.SetBytes(int64(len(text)))
            for i := 0; i < b.N; i++ {
                sink = sanitized_anchor_name.Create(text)
            }
        })
        b.Run("Complex", func(b *testing.B) {
            const text = `日本語; «Ёжик в тумане»?!`
            b.ReportAllocs()
            b.SetBytes(int64(len(text)))
            for i := 0; i < b.N; i++ {
                sink = sanitized_anchor_name.Create(text)
            }
        })
    }

Results are:

name              old time/op    new time/op     delta
Create/Simple-8      434ns ± 0%      187ns ± 1%   -56.99%  (p=0.000 n=10+10)
Create/Complex-8     667ns ± 0%      579ns ± 0%   -13.26%  (p=0.000 n=10+10)

name              old speed      new speed       delta
Create/Simple-8   96.8MB/s ± 0%  225.0MB/s ± 1%  +132.53%  (p=0.000 n=10+10)
Create/Complex-8  61.5MB/s ± 0%   70.9MB/s ± 0%   +15.30%  (p=0.000 n=10+10)

name              old alloc/op   new alloc/op    delta
Create/Simple-8       552B ± 0%        48B ± 0%   -91.30%  (p=0.000 n=10+10)
Create/Complex-8      296B ± 0%        48B ± 0%   -83.78%  (p=0.000 n=10+10)

name              old allocs/op  new allocs/op   delta
Create/Simple-8       7.00 ± 0%       1.00 ± 0%   -85.71%  (p=0.000 n=10+10)
Create/Complex-8      6.00 ± 0%       1.00 ± 0%   -83.33%  (p=0.000 n=10+10)
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

Successfully merging this pull request may close these issues.

None yet

1 participant