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

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package syllab
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  /*
    10  go tool compile -S encode_test.go > encode_test_C.S
    11  go tool objdump encode_test.o > encode_test_O.S
    12  */
    13  
    14  var sliceLen = 4
    15  
    16  func BenchmarkFunctionInlineByCompiler(b *testing.B) {
    17  	var t = test{
    18  		Num: 1587464654,
    19  	}
    20  	var buf = make([]byte, sliceLen)
    21  	for n := 0; n < b.N; n++ {
    22  		t.syllabEncoderCompiler(buf)
    23  	}
    24  }
    25  
    26  func BenchmarkFunctionInlineByDev(b *testing.B) {
    27  	var t = test{
    28  		Num: 1587464654,
    29  	}
    30  	var buf = make([]byte, sliceLen)
    31  	for n := 0; n < b.N; n++ {
    32  		t.syllabEncoderDev(buf)
    33  	}
    34  }
    35  
    36  func BenchmarkFunctionInlineByCompilerReturn(b *testing.B) {
    37  	var t = test{
    38  		Num: 1587464654,
    39  	}
    40  	for n := 0; n < b.N; n++ {
    41  		t.syllabEncoderCompilerReturn()
    42  	}
    43  }
    44  
    45  func BenchmarkFunctionInlineByDevReturn(b *testing.B) {
    46  	var t = test{
    47  		Num: 1587464654,
    48  	}
    49  	for n := 0; n < b.N; n++ {
    50  		t.syllabEncoderReturn()
    51  	}
    52  }
    53  
    54  type test struct {
    55  	Num uint32
    56  }
    57  
    58  func (t *test) syllabEncoderCompiler(buf []byte) {
    59  	setUInt32(buf, t.Num)
    60  }
    61  
    62  func setUInt32(p []byte, n uint32) {
    63  	p[0] = byte(n)
    64  	p[1] = byte(n >> 8)
    65  	p[2] = byte(n >> 16)
    66  	p[3] = byte(n >> 24)
    67  }
    68  
    69  func (t *test) syllabEncoderDev(buf []byte) {
    70  	buf[0] = byte(t.Num)
    71  	buf[1] = byte(t.Num >> 8)
    72  	buf[2] = byte(t.Num >> 16)
    73  	buf[3] = byte(t.Num >> 24)
    74  }
    75  
    76  func (t *test) syllabEncoderCompilerReturn() (buf []byte) {
    77  	buf = make([]byte, sliceLen)
    78  	setUInt32(buf, t.Num)
    79  	return
    80  }
    81  
    82  func (t *test) syllabEncoderReturn() (buf []byte) {
    83  	buf = make([]byte, sliceLen)
    84  
    85  	buf[0] = byte(t.Num)
    86  	buf[1] = byte(t.Num >> 8)
    87  	buf[2] = byte(t.Num >> 16)
    88  	buf[3] = byte(t.Num >> 24)
    89  	return
    90  }