Last active
September 17, 2021 09:58
-
-
Save mocyuto/58350d64c499fee6aa81f0b28b045feb to your computer and use it in GitHub Desktop.
buffer test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"io" | |
"testing" | |
) | |
const N int = 2000000 | |
func BenchmarkReadFrom(b *testing.B) { | |
data := make([]byte, N) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
reader := bytes.NewBuffer(data) | |
buf := new(bytes.Buffer) | |
buf.ReadFrom(reader) | |
buf.Bytes() | |
} | |
} | |
func BenchmarkReadCopyVar(b *testing.B) { | |
data := make([]byte, N) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
reader := bytes.NewBuffer(data) | |
buf := new(bytes.Buffer) | |
io.Copy(buf, reader) | |
buf.Bytes() | |
buf.Reset() | |
} | |
} | |
func BenchmarkReadCopy(b *testing.B) { | |
data := make([]byte, N) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
reader := bytes.NewBuffer(data) | |
buf := new(bytes.Buffer) | |
io.Copy(buf, reader) | |
buf.Bytes() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"io" | |
"testing" | |
) | |
const N int = 1000 | |
func BenchmarkReadAll(b *testing.B) { | |
data := make([]byte, N) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
reader := bytes.NewBuffer(data) | |
io.ReadAll(reader) | |
} | |
} | |
func BenchmarkReadFrom(b *testing.B) { | |
data := make([]byte, N) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
reader := bytes.NewBuffer(data) | |
buf := new(bytes.Buffer) | |
buf.ReadFrom(reader) | |
buf.Bytes() | |
} | |
} | |
func BenchmarkReadCopy(b *testing.B) { | |
data := make([]byte, N) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
reader := bytes.NewBuffer(data) | |
buf := new(bytes.Buffer) | |
io.Copy(buf, reader) | |
buf.Bytes() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment