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

Alloc enough space is better than multi append #518

Closed
wants to merge 1 commit into from
Closed

Alloc enough space is better than multi append #518

wants to merge 1 commit into from

Conversation

cloudaice
Copy link
Contributor

@cloudaice cloudaice commented Jun 26, 2018

I do some benchmark for this:

Code:

var (
        v1 = []byte("hello")
        v2 = []byte("world")
)

func Append() {
        v := append([]byte{}, v1...)
        v = append(v, v2...)
}

func Copy() {
        v := make([]byte, len(v1)+len(v2))
        n := copy(v, v1)
        copy(v[n:], v2)
}

Bench Code:

func BenchmarkAppend(b *testing.B) {
        for i := 0; i < b.N; i++ {
                Append()
        }
}

func BenchmarkCopy(b *testing.B) {
        for i := 0; i < b.N; i++ {
                Copy()
        }
}

Bench Reusult:

BenchmarkAppend-32      20000000                77.0 ns/op            24 B/op          2 allocs/op
BenchmarkCopy-32        50000000                37.5 ns/op            16 B/op          1 allocs/op
PASS
ok      demo/append     3.537s

This change is Reviewable

@CLAassistant
Copy link

CLAassistant commented Jun 26, 2018

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


xiangchao.01 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cloudaice
Copy link
Contributor Author

Something wrong happend

@cloudaice cloudaice closed this Jun 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants