Skip to content

Instantly share code, notes, and snippets.

@extemporalgenome
Created November 25, 2013 00:18
Show Gist options
  • Save extemporalgenome/7634394 to your computer and use it in GitHub Desktop.
Save extemporalgenome/7634394 to your computer and use it in GitHub Desktop.
go copy bench
package main
import (
"testing"
"unsafe"
)
const N = 32 << 20 // 32 MiB
type struct32 struct {
b bool
d [12]int8
i byte
j int16
f float32
k int32
g float64
}
func TestSize_struct32(t *testing.T) {
size := unsafe.Sizeof(struct32{})
if size != 32 {
t.Errorf("expected struct32 size of 32 bytes (%d actual)", size)
}
}
func BenchmarkCopy_byte(b *testing.B) {
var dst, src [N]byte
b.SetBytes(N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(dst[:], src[:])
}
}
func BenchmarkCopy_int64(b *testing.B) {
var dst, src [N / 8]int64
b.SetBytes(N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(dst[:], src[:])
}
}
func BenchmarkCopy_4byte(b *testing.B) {
var dst, src [N / 4][4]byte
b.SetBytes(N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(dst[:], src[:])
}
}
func BenchmarkCopy_16byte(b *testing.B) {
var dst, src [N / 16][16]byte
b.SetBytes(N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(dst[:], src[:])
}
}
func BenchmarkCopy_struct32(b *testing.B) {
var dst, src [N / 32]struct32
b.SetBytes(N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(dst[:], src[:])
}
}
# Intel Atom N450 1.66GHz
GOARCH=amd64 go test -bench .
PASS
BenchmarkCopy_byte 50 29592577 ns/op 1133.88 MB/s
BenchmarkCopy_int64 50 29635021 ns/op 1132.26 MB/s
BenchmarkCopy_4byte 50 29905231 ns/op 1122.03 MB/s
BenchmarkCopy_16byte 50 29697470 ns/op 1129.88 MB/s
BenchmarkCopy_struct32 50 29572247 ns/op 1134.66 MB/s
GOARCH=386 go test -bench .
PASS
BenchmarkCopy_byte 50 29507551 ns/op 1137.15 MB/s
BenchmarkCopy_int64 50 29393642 ns/op 1141.55 MB/s
BenchmarkCopy_4byte 50 29930332 ns/op 1121.08 MB/s
BenchmarkCopy_16byte 50 30106251 ns/op 1114.53 MB/s
BenchmarkCopy_struct32 50 29465431 ns/op 1138.77 MB/s
GOARCH=386 GO386=387 go test -bench .
PASS
BenchmarkCopy_byte 50 29960040 ns/op 1119.97 MB/s
BenchmarkCopy_int64 100 29958192 ns/op 1120.04 MB/s
BenchmarkCopy_4byte 50 30005881 ns/op 1118.26 MB/s
BenchmarkCopy_16byte 50 29578779 ns/op 1134.41 MB/s
BenchmarkCopy_struct32 50 29460366 ns/op 1138.97 MB/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment