github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/rootio/encoding/read_test.go (about)

     1  package hep
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // Naming
     8  //   B / L = big endian / little endian
     9  //   A / S = (at, data) / (data[at:])
    10  //   F / R = forward vs reverse
    11  //   U = no bounds checks
    12  
    13  func BenchmarkReadU32(b *testing.B) {
    14  	data := make([]byte, 1003)
    15  	for i := range data {
    16  		data[i] = byte(i)
    17  	}
    18  	b.Run("binary.BigEndian", func(b *testing.B) {
    19  		t := uint32(0)
    20  		for i := 0; i < b.N; i++ {
    21  			for at := 0; at < len(data)-4; at++ {
    22  				t += BigU32(data[at:])
    23  			}
    24  		}
    25  	})
    26  	b.Run("BAR", func(b *testing.B) {
    27  		t := uint32(0)
    28  		for i := 0; i < b.N; i++ {
    29  			for at := 0; at < len(data)-4; at++ {
    30  				t += BigU32ar(at, data)
    31  			}
    32  		}
    33  	})
    34  	b.Run("BAF", func(b *testing.B) {
    35  		t := uint32(0)
    36  		for i := 0; i < b.N; i++ {
    37  			for at := 0; at < len(data)-4; at++ {
    38  				t += BigU32af(at, data)
    39  			}
    40  		}
    41  	})
    42  	b.Run("LAF", func(b *testing.B) {
    43  		t := uint32(0)
    44  		for i := 0; i < b.N; i++ {
    45  			for at := 0; at < len(data)-4; at++ {
    46  				t += LittleU32af(at, data)
    47  			}
    48  		}
    49  	})
    50  	b.Run("LAR", func(b *testing.B) {
    51  		t := uint32(0)
    52  		for i := 0; i < b.N; i++ {
    53  			for at := 0; at < len(data)-4; at++ {
    54  				t += LittleU32ar(at, data)
    55  			}
    56  		}
    57  	})
    58  	b.Run("NAD", func(b *testing.B) {
    59  		t := uint32(0)
    60  		for i := 0; i < b.N; i++ {
    61  			for at := 0; at < len(data)-4; at++ {
    62  				t += NativeU32ad(at, data)
    63  			}
    64  		}
    65  	})
    66  	b.Run("NADU", func(b *testing.B) {
    67  		t := uint32(0)
    68  		for i := 0; i < b.N; i++ {
    69  			for at := 0; at < len(data)-4; at++ {
    70  				t += NativeU32adu(at, data)
    71  			}
    72  		}
    73  	})
    74  	b.Run("BSR", func(b *testing.B) {
    75  		t := uint32(0)
    76  		for i := 0; i < b.N; i++ {
    77  			for at := 0; at < len(data)-4; at++ {
    78  				t += BigU32sr(data[at:])
    79  			}
    80  		}
    81  	})
    82  	b.Run("BSF", func(b *testing.B) {
    83  		t := uint32(0)
    84  		for i := 0; i < b.N; i++ {
    85  			for at := 0; at < len(data)-4; at++ {
    86  				t += BigU32sf(data[at:])
    87  			}
    88  		}
    89  	})
    90  	b.Run("binary.LittleEndian", func(b *testing.B) {
    91  		t := uint32(0)
    92  		for i := 0; i < b.N; i++ {
    93  			for at := 0; at < len(data)-4; at++ {
    94  				t += LittleU32(data[at:])
    95  			}
    96  		}
    97  	})
    98  	b.Run("LSF", func(b *testing.B) {
    99  		t := uint32(0)
   100  		for i := 0; i < b.N; i++ {
   101  			for at := 0; at < len(data)-4; at++ {
   102  				t += LittleU32sf(data[at:])
   103  			}
   104  		}
   105  	})
   106  	b.Run("LSR", func(b *testing.B) {
   107  		t := uint32(0)
   108  		for i := 0; i < b.N; i++ {
   109  			for at := 0; at < len(data)-4; at++ {
   110  				t += LittleU32sr(data[at:])
   111  			}
   112  		}
   113  	})
   114  	b.Run("NSD", func(b *testing.B) {
   115  		t := uint32(0)
   116  		for i := 0; i < b.N; i++ {
   117  			for at := 0; at < len(data)-4; at++ {
   118  				t += NativeU32sd(data[at:])
   119  			}
   120  		}
   121  	})
   122  	b.Run("NSDU", func(b *testing.B) {
   123  		t := uint32(0)
   124  		for i := 0; i < b.N; i++ {
   125  			for at := 0; at < len(data)-4; at++ {
   126  				t += NativeU32sdu(data[at:])
   127  			}
   128  		}
   129  	})
   130  }