Files
2026-05-27 23:03:00 +08:00

55 lines
838 B
Go

package main
import (
"sync"
"testing"
)
type SimpleStruct struct {
n int
}
func BenchmarkStructureFalseSharing(b *testing.B) {
structA := SimpleStruct{}
structB := SimpleStruct{}
wg := sync.WaitGroup{}
M := 1000000
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(2)
go func() {
for j := 0; j < M; j++ {
structA.n += j
}
wg.Done()
}()
go func() {
for j := 0; j < M; j++ {
structB.n += j
}
wg.Done()
}()
wg.Wait()
}
}
func BenchmarkCopyAVX2(b *testing.B) {
a := make([]byte, 128*1024*1024)
c := make([]byte, 128*1024*1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
CopySlice_AVX2(a, c, len(c))
}
}
func BenchmarkCopy(b *testing.B) {
a := make([]byte, 128*1024*1024)
c := make([]byte, 128*1024*1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
CopySlice(a, c, len(c))
}
}