github.com/jxskiss/gopkg@v0.17.3/internal/benchmark/memclr_test.go (about)

     1  package benchmark
     2  
     3  import (
     4  	"github.com/jxskiss/gopkg/internal/linkname"
     5  	"reflect"
     6  	"testing"
     7  	"unsafe"
     8  )
     9  
    10  func BenchmarkMemory_Alloc_4K(b *testing.B) {
    11  	b.ReportAllocs()
    12  	b.ResetTimer()
    13  	for i := 0; i < b.N; i++ {
    14  		tmp := make([]byte, 4*1024)
    15  		_ = reflect.TypeOf(tmp) // make it escape
    16  	}
    17  }
    18  
    19  func BenchmarkMemory_LoopZero_4K(b *testing.B) {
    20  	tmp := make([]byte, 4*1024)
    21  
    22  	b.ResetTimer()
    23  	for i := 0; i < b.N; i++ {
    24  		for j := 0; j < len(tmp); j++ {
    25  			tmp[j] = byte(0)
    26  		}
    27  	}
    28  }
    29  
    30  func BenchmarkMemory_memclrNoHeapPointers_4K(b *testing.B) {
    31  	tmp := make([]byte, 4*1024)
    32  	ptr := unsafe.Pointer(&tmp[0])
    33  
    34  	b.ResetTimer()
    35  	for i := 0; i < b.N; i++ {
    36  		linkname.Runtime_memclrNoHeapPointers(ptr, uintptr(4*1024))
    37  	}
    38  }