github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/iface2/basic.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/loov/hrtime"
     8  )
     9  
    10  func main() {
    11  	runtime.LockOSThread()
    12  
    13  	const N = 100000000
    14  
    15  	var overhead uint64 = 100
    16  	for i := 0; i < N; i++ {
    17  		start := hrtime.RDTSC()
    18  		stop := hrtime.RDTSCP()
    19  		delta := stop - start
    20  		if delta < overhead {
    21  			overhead = delta
    22  		}
    23  	}
    24  
    25  	averageOverhead := float64(overhead)
    26  	fmt.Println("measurement overhead:", averageOverhead, "cy")
    27  
    28  	{
    29  		var measurement uint64 = 100
    30  		for i := 0; i < N; i++ {
    31  			start := hrtime.RDTSC()
    32  
    33  			runtime.KeepAlive(example.Get())
    34  			runtime.KeepAlive(example.Get())
    35  			runtime.KeepAlive(example.Get())
    36  			runtime.KeepAlive(example.Get())
    37  			runtime.KeepAlive(example.Get())
    38  
    39  			runtime.KeepAlive(example.Get())
    40  			runtime.KeepAlive(example.Get())
    41  			runtime.KeepAlive(example.Get())
    42  			runtime.KeepAlive(example.Get())
    43  			runtime.KeepAlive(example.Get())
    44  
    45  			stop := hrtime.RDTSCP()
    46  			delta := stop - start
    47  			if delta < measurement {
    48  				measurement = delta
    49  			}
    50  
    51  			if i & 1 == 1 {
    52  				example = &A{}
    53  			} else {
    54  				example = &B{}
    55  			}
    56  		}
    57  
    58  		averageCall := float64(measurement)
    59  		fmt.Println("iface call:", float64(averageCall-averageOverhead) / 10, "cy")
    60  	}
    61  
    62  
    63  	{
    64  		var measurement uint64 = 100
    65  		for i := 0; i < N; i++ {
    66  			start := hrtime.RDTSC()
    67  			
    68  			runtime.KeepAlive(nop())
    69  			runtime.KeepAlive(nop())
    70  			runtime.KeepAlive(nop())
    71  			runtime.KeepAlive(nop())
    72  			runtime.KeepAlive(nop())
    73  
    74  			runtime.KeepAlive(nop())
    75  			runtime.KeepAlive(nop())
    76  			runtime.KeepAlive(nop())
    77  			runtime.KeepAlive(nop())
    78  			runtime.KeepAlive(nop())
    79  
    80  			stop := hrtime.RDTSCP()
    81  			delta := stop - start
    82  			if delta < measurement {
    83  				measurement = delta
    84  			}
    85  
    86  			if i & 1 == 1 {
    87  				example = &A{}
    88  			} else {
    89  				example = &B{}
    90  			}
    91  		}
    92  
    93  		averageCall := float64(measurement)
    94  		fmt.Println("nop call:", float64(averageCall-averageOverhead) / 10, "cy")
    95  	}
    96  }
    97  
    98  var example Interface = &A{}
    99  
   100  type Interface interface {
   101  	Get() int
   102  }
   103  
   104  type A struct{ _ int; Value int }
   105  type B struct{ _ int; Value int }
   106  
   107  func (x *A) Get() int { return x.Value }
   108  func (x *B) Get() int { return x.Value }
   109  
   110  
   111  //go:noinline
   112  func nop() int { return 0 }