github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/binary/binary_bench_test.go (about)

     1  package binary
     2  
     3  import "testing"
     4  
     5  var putbuf = []byte{8: 0}
     6  
     7  var (
     8  	boolean bool
     9  	u8      uint8
    10  	u16     uint16
    11  	u32     uint32
    12  	u64     uint64
    13  )
    14  
    15  var big = []byte{
    16  	0x01,
    17  	0x01,
    18  	0x01, 0x02,
    19  	0x01, 0x02, 0x03, 0x04,
    20  	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    21  }
    22  
    23  var little = []byte{
    24  	0x01,
    25  	0x01,
    26  	0x02, 0x01,
    27  	0x04, 0x03, 0x02, 0x01,
    28  	0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
    29  }
    30  
    31  func BenchmarkPutUint8(b *testing.B) {
    32  	for i := 0; i < b.N; i++ {
    33  		PutUint8(putbuf[:1], uint8(i))
    34  	}
    35  }
    36  
    37  func BenchmarkPutBool(b *testing.B) {
    38  	for i := 0; i < b.N; i++ {
    39  		PutBool(putbuf[:1], true)
    40  	}
    41  }
    42  
    43  func BenchmarkUint8(b *testing.B) {
    44  	for i := 0; i < b.N; i++ {
    45  		u8 = Uint8(little[:1])
    46  	}
    47  }
    48  
    49  func BenchmarkBool(b *testing.B) {
    50  	for i := 0; i < b.N; i++ {
    51  		boolean = Bool(little[:1])
    52  	}
    53  }