gonum.org/v1/gonum@v0.14.0/internal/asm/f64/bench_other_test.go (about)

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package f64_test
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  
    11  	. "gonum.org/v1/gonum/internal/asm/f64"
    12  )
    13  
    14  func benchL1Norm(f func(x []float64) float64, sz int, t *testing.B) {
    15  	dst := y[:sz]
    16  	for i := 0; i < t.N; i++ {
    17  		f(dst)
    18  	}
    19  }
    20  
    21  var naiveL1Norm = func(x []float64) (sum float64) {
    22  	for _, v := range x {
    23  		sum += math.Abs(v)
    24  	}
    25  	return sum
    26  }
    27  
    28  func BenchmarkL1Norm1(t *testing.B)      { benchL1Norm(L1Norm, 1, t) }
    29  func BenchmarkL1Norm2(t *testing.B)      { benchL1Norm(L1Norm, 2, t) }
    30  func BenchmarkL1Norm3(t *testing.B)      { benchL1Norm(L1Norm, 3, t) }
    31  func BenchmarkL1Norm4(t *testing.B)      { benchL1Norm(L1Norm, 4, t) }
    32  func BenchmarkL1Norm5(t *testing.B)      { benchL1Norm(L1Norm, 5, t) }
    33  func BenchmarkL1Norm10(t *testing.B)     { benchL1Norm(L1Norm, 10, t) }
    34  func BenchmarkL1Norm100(t *testing.B)    { benchL1Norm(L1Norm, 100, t) }
    35  func BenchmarkL1Norm1000(t *testing.B)   { benchL1Norm(L1Norm, 1000, t) }
    36  func BenchmarkL1Norm10000(t *testing.B)  { benchL1Norm(L1Norm, 10000, t) }
    37  func BenchmarkL1Norm100000(t *testing.B) { benchL1Norm(L1Norm, 100000, t) }
    38  func BenchmarkL1Norm500000(t *testing.B) { benchL1Norm(L1Norm, 500000, t) }
    39  
    40  func BenchmarkLL1Norm1(t *testing.B)      { benchL1Norm(naiveL1Norm, 1, t) }
    41  func BenchmarkLL1Norm2(t *testing.B)      { benchL1Norm(naiveL1Norm, 2, t) }
    42  func BenchmarkLL1Norm3(t *testing.B)      { benchL1Norm(naiveL1Norm, 3, t) }
    43  func BenchmarkLL1Norm4(t *testing.B)      { benchL1Norm(naiveL1Norm, 4, t) }
    44  func BenchmarkLL1Norm5(t *testing.B)      { benchL1Norm(naiveL1Norm, 5, t) }
    45  func BenchmarkLL1Norm10(t *testing.B)     { benchL1Norm(naiveL1Norm, 10, t) }
    46  func BenchmarkLL1Norm100(t *testing.B)    { benchL1Norm(naiveL1Norm, 100, t) }
    47  func BenchmarkLL1Norm1000(t *testing.B)   { benchL1Norm(naiveL1Norm, 1000, t) }
    48  func BenchmarkLL1Norm10000(t *testing.B)  { benchL1Norm(naiveL1Norm, 10000, t) }
    49  func BenchmarkLL1Norm100000(t *testing.B) { benchL1Norm(naiveL1Norm, 100000, t) }
    50  func BenchmarkLL1Norm500000(t *testing.B) { benchL1Norm(naiveL1Norm, 500000, t) }
    51  
    52  func benchL1NormInc(t *testing.B, ln, inc int, f func(x []float64, n, incX int) float64) {
    53  	for i := 0; i < t.N; i++ {
    54  		f(x, ln, inc)
    55  	}
    56  }
    57  
    58  var naiveL1NormInc = func(x []float64, n, incX int) (sum float64) {
    59  	for i := 0; i < n*incX; i += incX {
    60  		sum += math.Abs(x[i])
    61  	}
    62  	return sum
    63  }
    64  
    65  func BenchmarkF64L1NormIncN1Inc1(b *testing.B) { benchL1NormInc(b, 1, 1, L1NormInc) }
    66  
    67  func BenchmarkF64L1NormIncN2Inc1(b *testing.B)  { benchL1NormInc(b, 2, 1, L1NormInc) }
    68  func BenchmarkF64L1NormIncN2Inc2(b *testing.B)  { benchL1NormInc(b, 2, 2, L1NormInc) }
    69  func BenchmarkF64L1NormIncN2Inc4(b *testing.B)  { benchL1NormInc(b, 2, 4, L1NormInc) }
    70  func BenchmarkF64L1NormIncN2Inc10(b *testing.B) { benchL1NormInc(b, 2, 10, L1NormInc) }
    71  
    72  func BenchmarkF64L1NormIncN3Inc1(b *testing.B)  { benchL1NormInc(b, 3, 1, L1NormInc) }
    73  func BenchmarkF64L1NormIncN3Inc2(b *testing.B)  { benchL1NormInc(b, 3, 2, L1NormInc) }
    74  func BenchmarkF64L1NormIncN3Inc4(b *testing.B)  { benchL1NormInc(b, 3, 4, L1NormInc) }
    75  func BenchmarkF64L1NormIncN3Inc10(b *testing.B) { benchL1NormInc(b, 3, 10, L1NormInc) }
    76  
    77  func BenchmarkF64L1NormIncN4Inc1(b *testing.B)  { benchL1NormInc(b, 4, 1, L1NormInc) }
    78  func BenchmarkF64L1NormIncN4Inc2(b *testing.B)  { benchL1NormInc(b, 4, 2, L1NormInc) }
    79  func BenchmarkF64L1NormIncN4Inc4(b *testing.B)  { benchL1NormInc(b, 4, 4, L1NormInc) }
    80  func BenchmarkF64L1NormIncN4Inc10(b *testing.B) { benchL1NormInc(b, 4, 10, L1NormInc) }
    81  
    82  func BenchmarkF64L1NormIncN10Inc1(b *testing.B)  { benchL1NormInc(b, 10, 1, L1NormInc) }
    83  func BenchmarkF64L1NormIncN10Inc2(b *testing.B)  { benchL1NormInc(b, 10, 2, L1NormInc) }
    84  func BenchmarkF64L1NormIncN10Inc4(b *testing.B)  { benchL1NormInc(b, 10, 4, L1NormInc) }
    85  func BenchmarkF64L1NormIncN10Inc10(b *testing.B) { benchL1NormInc(b, 10, 10, L1NormInc) }
    86  
    87  func BenchmarkF64L1NormIncN1000Inc1(b *testing.B)  { benchL1NormInc(b, 1000, 1, L1NormInc) }
    88  func BenchmarkF64L1NormIncN1000Inc2(b *testing.B)  { benchL1NormInc(b, 1000, 2, L1NormInc) }
    89  func BenchmarkF64L1NormIncN1000Inc4(b *testing.B)  { benchL1NormInc(b, 1000, 4, L1NormInc) }
    90  func BenchmarkF64L1NormIncN1000Inc10(b *testing.B) { benchL1NormInc(b, 1000, 10, L1NormInc) }
    91  
    92  func BenchmarkF64L1NormIncN100000Inc1(b *testing.B)  { benchL1NormInc(b, 100000, 1, L1NormInc) }
    93  func BenchmarkF64L1NormIncN100000Inc2(b *testing.B)  { benchL1NormInc(b, 100000, 2, L1NormInc) }
    94  func BenchmarkF64L1NormIncN100000Inc4(b *testing.B)  { benchL1NormInc(b, 100000, 4, L1NormInc) }
    95  func BenchmarkF64L1NormIncN100000Inc10(b *testing.B) { benchL1NormInc(b, 100000, 10, L1NormInc) }
    96  
    97  func BenchmarkLF64L1NormIncN1Inc1(b *testing.B) { benchL1NormInc(b, 1, 1, naiveL1NormInc) }
    98  
    99  func BenchmarkLF64L1NormIncN2Inc1(b *testing.B)  { benchL1NormInc(b, 2, 1, naiveL1NormInc) }
   100  func BenchmarkLF64L1NormIncN2Inc2(b *testing.B)  { benchL1NormInc(b, 2, 2, naiveL1NormInc) }
   101  func BenchmarkLF64L1NormIncN2Inc4(b *testing.B)  { benchL1NormInc(b, 2, 4, naiveL1NormInc) }
   102  func BenchmarkLF64L1NormIncN2Inc10(b *testing.B) { benchL1NormInc(b, 2, 10, naiveL1NormInc) }
   103  
   104  func BenchmarkLF64L1NormIncN3Inc1(b *testing.B)  { benchL1NormInc(b, 3, 1, naiveL1NormInc) }
   105  func BenchmarkLF64L1NormIncN3Inc2(b *testing.B)  { benchL1NormInc(b, 3, 2, naiveL1NormInc) }
   106  func BenchmarkLF64L1NormIncN3Inc4(b *testing.B)  { benchL1NormInc(b, 3, 4, naiveL1NormInc) }
   107  func BenchmarkLF64L1NormIncN3Inc10(b *testing.B) { benchL1NormInc(b, 3, 10, naiveL1NormInc) }
   108  
   109  func BenchmarkLF64L1NormIncN4Inc1(b *testing.B)  { benchL1NormInc(b, 4, 1, naiveL1NormInc) }
   110  func BenchmarkLF64L1NormIncN4Inc2(b *testing.B)  { benchL1NormInc(b, 4, 2, naiveL1NormInc) }
   111  func BenchmarkLF64L1NormIncN4Inc4(b *testing.B)  { benchL1NormInc(b, 4, 4, naiveL1NormInc) }
   112  func BenchmarkLF64L1NormIncN4Inc10(b *testing.B) { benchL1NormInc(b, 4, 10, naiveL1NormInc) }
   113  
   114  func BenchmarkLF64L1NormIncN10Inc1(b *testing.B)  { benchL1NormInc(b, 10, 1, naiveL1NormInc) }
   115  func BenchmarkLF64L1NormIncN10Inc2(b *testing.B)  { benchL1NormInc(b, 10, 2, naiveL1NormInc) }
   116  func BenchmarkLF64L1NormIncN10Inc4(b *testing.B)  { benchL1NormInc(b, 10, 4, naiveL1NormInc) }
   117  func BenchmarkLF64L1NormIncN10Inc10(b *testing.B) { benchL1NormInc(b, 10, 10, naiveL1NormInc) }
   118  
   119  func BenchmarkLF64L1NormIncN1000Inc1(b *testing.B)  { benchL1NormInc(b, 1000, 1, naiveL1NormInc) }
   120  func BenchmarkLF64L1NormIncN1000Inc2(b *testing.B)  { benchL1NormInc(b, 1000, 2, naiveL1NormInc) }
   121  func BenchmarkLF64L1NormIncN1000Inc4(b *testing.B)  { benchL1NormInc(b, 1000, 4, naiveL1NormInc) }
   122  func BenchmarkLF64L1NormIncN1000Inc10(b *testing.B) { benchL1NormInc(b, 1000, 10, naiveL1NormInc) }
   123  
   124  func BenchmarkLF64L1NormIncN100000Inc1(b *testing.B)  { benchL1NormInc(b, 100000, 1, naiveL1NormInc) }
   125  func BenchmarkLF64L1NormIncN100000Inc2(b *testing.B)  { benchL1NormInc(b, 100000, 2, naiveL1NormInc) }
   126  func BenchmarkLF64L1NormIncN100000Inc4(b *testing.B)  { benchL1NormInc(b, 100000, 4, naiveL1NormInc) }
   127  func BenchmarkLF64L1NormIncN100000Inc10(b *testing.B) { benchL1NormInc(b, 100000, 10, naiveL1NormInc) }
   128  
   129  func benchAdd(f func(dst, s []float64), sz int, t *testing.B) {
   130  	dst, s := y[:sz], x[:sz]
   131  	for i := 0; i < t.N; i++ {
   132  		f(dst, s)
   133  	}
   134  }
   135  
   136  var naiveAdd = func(dst, s []float64) {
   137  	for i, v := range s {
   138  		dst[i] += v
   139  	}
   140  }
   141  
   142  func BenchmarkAdd1(t *testing.B)      { benchAdd(Add, 1, t) }
   143  func BenchmarkAdd2(t *testing.B)      { benchAdd(Add, 2, t) }
   144  func BenchmarkAdd3(t *testing.B)      { benchAdd(Add, 3, t) }
   145  func BenchmarkAdd4(t *testing.B)      { benchAdd(Add, 4, t) }
   146  func BenchmarkAdd5(t *testing.B)      { benchAdd(Add, 5, t) }
   147  func BenchmarkAdd10(t *testing.B)     { benchAdd(Add, 10, t) }
   148  func BenchmarkAdd100(t *testing.B)    { benchAdd(Add, 100, t) }
   149  func BenchmarkAdd1000(t *testing.B)   { benchAdd(Add, 1000, t) }
   150  func BenchmarkAdd10000(t *testing.B)  { benchAdd(Add, 10000, t) }
   151  func BenchmarkAdd100000(t *testing.B) { benchAdd(Add, 100000, t) }
   152  func BenchmarkAdd500000(t *testing.B) { benchAdd(Add, 500000, t) }
   153  
   154  func BenchmarkLAdd1(t *testing.B)      { benchAdd(naiveAdd, 1, t) }
   155  func BenchmarkLAdd2(t *testing.B)      { benchAdd(naiveAdd, 2, t) }
   156  func BenchmarkLAdd3(t *testing.B)      { benchAdd(naiveAdd, 3, t) }
   157  func BenchmarkLAdd4(t *testing.B)      { benchAdd(naiveAdd, 4, t) }
   158  func BenchmarkLAdd5(t *testing.B)      { benchAdd(naiveAdd, 5, t) }
   159  func BenchmarkLAdd10(t *testing.B)     { benchAdd(naiveAdd, 10, t) }
   160  func BenchmarkLAdd100(t *testing.B)    { benchAdd(naiveAdd, 100, t) }
   161  func BenchmarkLAdd1000(t *testing.B)   { benchAdd(naiveAdd, 1000, t) }
   162  func BenchmarkLAdd10000(t *testing.B)  { benchAdd(naiveAdd, 10000, t) }
   163  func BenchmarkLAdd100000(t *testing.B) { benchAdd(naiveAdd, 100000, t) }
   164  func BenchmarkLAdd500000(t *testing.B) { benchAdd(naiveAdd, 500000, t) }
   165  
   166  func benchAddConst(f func(a float64, x []float64), sz int, t *testing.B) {
   167  	a, x := 1., x[:sz]
   168  	for i := 0; i < t.N; i++ {
   169  		f(a, x)
   170  	}
   171  }
   172  
   173  var naiveAddConst = func(a float64, x []float64) {
   174  	for i := range x {
   175  		x[i] += a
   176  	}
   177  }
   178  
   179  func BenchmarkAddConst1(t *testing.B)      { benchAddConst(AddConst, 1, t) }
   180  func BenchmarkAddConst2(t *testing.B)      { benchAddConst(AddConst, 2, t) }
   181  func BenchmarkAddConst3(t *testing.B)      { benchAddConst(AddConst, 3, t) }
   182  func BenchmarkAddConst4(t *testing.B)      { benchAddConst(AddConst, 4, t) }
   183  func BenchmarkAddConst5(t *testing.B)      { benchAddConst(AddConst, 5, t) }
   184  func BenchmarkAddConst10(t *testing.B)     { benchAddConst(AddConst, 10, t) }
   185  func BenchmarkAddConst100(t *testing.B)    { benchAddConst(AddConst, 100, t) }
   186  func BenchmarkAddConst1000(t *testing.B)   { benchAddConst(AddConst, 1000, t) }
   187  func BenchmarkAddConst10000(t *testing.B)  { benchAddConst(AddConst, 10000, t) }
   188  func BenchmarkAddConst100000(t *testing.B) { benchAddConst(AddConst, 100000, t) }
   189  func BenchmarkAddConst500000(t *testing.B) { benchAddConst(AddConst, 500000, t) }
   190  
   191  func BenchmarkLAddConst1(t *testing.B)      { benchAddConst(naiveAddConst, 1, t) }
   192  func BenchmarkLAddConst2(t *testing.B)      { benchAddConst(naiveAddConst, 2, t) }
   193  func BenchmarkLAddConst3(t *testing.B)      { benchAddConst(naiveAddConst, 3, t) }
   194  func BenchmarkLAddConst4(t *testing.B)      { benchAddConst(naiveAddConst, 4, t) }
   195  func BenchmarkLAddConst5(t *testing.B)      { benchAddConst(naiveAddConst, 5, t) }
   196  func BenchmarkLAddConst10(t *testing.B)     { benchAddConst(naiveAddConst, 10, t) }
   197  func BenchmarkLAddConst100(t *testing.B)    { benchAddConst(naiveAddConst, 100, t) }
   198  func BenchmarkLAddConst1000(t *testing.B)   { benchAddConst(naiveAddConst, 1000, t) }
   199  func BenchmarkLAddConst10000(t *testing.B)  { benchAddConst(naiveAddConst, 10000, t) }
   200  func BenchmarkLAddConst100000(t *testing.B) { benchAddConst(naiveAddConst, 100000, t) }
   201  func BenchmarkLAddConst500000(t *testing.B) { benchAddConst(naiveAddConst, 500000, t) }
   202  
   203  func benchCumSum(f func(a, b []float64) []float64, sz int, t *testing.B) {
   204  	a, b := x[:sz], y[:sz]
   205  	for i := 0; i < t.N; i++ {
   206  		f(a, b)
   207  	}
   208  }
   209  
   210  var naiveCumSum = func(dst, s []float64) []float64 {
   211  	if len(s) == 0 {
   212  		return dst
   213  	}
   214  	dst[0] = s[0]
   215  	for i, v := range s[1:] {
   216  		dst[i+1] = dst[i] + v
   217  	}
   218  	return dst
   219  }
   220  
   221  func BenchmarkCumSum1(t *testing.B)      { benchCumSum(CumSum, 1, t) }
   222  func BenchmarkCumSum2(t *testing.B)      { benchCumSum(CumSum, 2, t) }
   223  func BenchmarkCumSum3(t *testing.B)      { benchCumSum(CumSum, 3, t) }
   224  func BenchmarkCumSum4(t *testing.B)      { benchCumSum(CumSum, 4, t) }
   225  func BenchmarkCumSum5(t *testing.B)      { benchCumSum(CumSum, 5, t) }
   226  func BenchmarkCumSum10(t *testing.B)     { benchCumSum(CumSum, 10, t) }
   227  func BenchmarkCumSum100(t *testing.B)    { benchCumSum(CumSum, 100, t) }
   228  func BenchmarkCumSum1000(t *testing.B)   { benchCumSum(CumSum, 1000, t) }
   229  func BenchmarkCumSum10000(t *testing.B)  { benchCumSum(CumSum, 10000, t) }
   230  func BenchmarkCumSum100000(t *testing.B) { benchCumSum(CumSum, 100000, t) }
   231  func BenchmarkCumSum500000(t *testing.B) { benchCumSum(CumSum, 500000, t) }
   232  
   233  func BenchmarkLCumSum1(t *testing.B)      { benchCumSum(naiveCumSum, 1, t) }
   234  func BenchmarkLCumSum2(t *testing.B)      { benchCumSum(naiveCumSum, 2, t) }
   235  func BenchmarkLCumSum3(t *testing.B)      { benchCumSum(naiveCumSum, 3, t) }
   236  func BenchmarkLCumSum4(t *testing.B)      { benchCumSum(naiveCumSum, 4, t) }
   237  func BenchmarkLCumSum5(t *testing.B)      { benchCumSum(naiveCumSum, 5, t) }
   238  func BenchmarkLCumSum10(t *testing.B)     { benchCumSum(naiveCumSum, 10, t) }
   239  func BenchmarkLCumSum100(t *testing.B)    { benchCumSum(naiveCumSum, 100, t) }
   240  func BenchmarkLCumSum1000(t *testing.B)   { benchCumSum(naiveCumSum, 1000, t) }
   241  func BenchmarkLCumSum10000(t *testing.B)  { benchCumSum(naiveCumSum, 10000, t) }
   242  func BenchmarkLCumSum100000(t *testing.B) { benchCumSum(naiveCumSum, 100000, t) }
   243  func BenchmarkLCumSum500000(t *testing.B) { benchCumSum(naiveCumSum, 500000, t) }
   244  
   245  func benchCumProd(f func(a, b []float64) []float64, sz int, t *testing.B) {
   246  	a, b := x[:sz], y[:sz]
   247  	for i := 0; i < t.N; i++ {
   248  		f(a, b)
   249  	}
   250  }
   251  
   252  var naiveCumProd = func(dst, s []float64) []float64 {
   253  	if len(s) == 0 {
   254  		return dst
   255  	}
   256  	dst[0] = s[0]
   257  	for i, v := range s[1:] {
   258  		dst[i+1] = dst[i] + v
   259  	}
   260  	return dst
   261  }
   262  
   263  func BenchmarkCumProd1(t *testing.B)      { benchCumProd(CumProd, 1, t) }
   264  func BenchmarkCumProd2(t *testing.B)      { benchCumProd(CumProd, 2, t) }
   265  func BenchmarkCumProd3(t *testing.B)      { benchCumProd(CumProd, 3, t) }
   266  func BenchmarkCumProd4(t *testing.B)      { benchCumProd(CumProd, 4, t) }
   267  func BenchmarkCumProd5(t *testing.B)      { benchCumProd(CumProd, 5, t) }
   268  func BenchmarkCumProd10(t *testing.B)     { benchCumProd(CumProd, 10, t) }
   269  func BenchmarkCumProd100(t *testing.B)    { benchCumProd(CumProd, 100, t) }
   270  func BenchmarkCumProd1000(t *testing.B)   { benchCumProd(CumProd, 1000, t) }
   271  func BenchmarkCumProd10000(t *testing.B)  { benchCumProd(CumProd, 10000, t) }
   272  func BenchmarkCumProd100000(t *testing.B) { benchCumProd(CumProd, 100000, t) }
   273  func BenchmarkCumProd500000(t *testing.B) { benchCumProd(CumProd, 500000, t) }
   274  
   275  func BenchmarkLCumProd1(t *testing.B)      { benchCumProd(naiveCumProd, 1, t) }
   276  func BenchmarkLCumProd2(t *testing.B)      { benchCumProd(naiveCumProd, 2, t) }
   277  func BenchmarkLCumProd3(t *testing.B)      { benchCumProd(naiveCumProd, 3, t) }
   278  func BenchmarkLCumProd4(t *testing.B)      { benchCumProd(naiveCumProd, 4, t) }
   279  func BenchmarkLCumProd5(t *testing.B)      { benchCumProd(naiveCumProd, 5, t) }
   280  func BenchmarkLCumProd10(t *testing.B)     { benchCumProd(naiveCumProd, 10, t) }
   281  func BenchmarkLCumProd100(t *testing.B)    { benchCumProd(naiveCumProd, 100, t) }
   282  func BenchmarkLCumProd1000(t *testing.B)   { benchCumProd(naiveCumProd, 1000, t) }
   283  func BenchmarkLCumProd10000(t *testing.B)  { benchCumProd(naiveCumProd, 10000, t) }
   284  func BenchmarkLCumProd100000(t *testing.B) { benchCumProd(naiveCumProd, 100000, t) }
   285  func BenchmarkLCumProd500000(t *testing.B) { benchCumProd(naiveCumProd, 500000, t) }
   286  
   287  func benchDiv(f func(a, b []float64), sz int, t *testing.B) {
   288  	a, b := x[:sz], y[:sz]
   289  	for i := 0; i < t.N; i++ {
   290  		f(a, b)
   291  	}
   292  }
   293  
   294  var naiveDiv = func(a, b []float64) {
   295  	for i, v := range b {
   296  		a[i] /= v
   297  	}
   298  }
   299  
   300  func BenchmarkDiv1(t *testing.B)      { benchDiv(Div, 1, t) }
   301  func BenchmarkDiv2(t *testing.B)      { benchDiv(Div, 2, t) }
   302  func BenchmarkDiv3(t *testing.B)      { benchDiv(Div, 3, t) }
   303  func BenchmarkDiv4(t *testing.B)      { benchDiv(Div, 4, t) }
   304  func BenchmarkDiv5(t *testing.B)      { benchDiv(Div, 5, t) }
   305  func BenchmarkDiv10(t *testing.B)     { benchDiv(Div, 10, t) }
   306  func BenchmarkDiv100(t *testing.B)    { benchDiv(Div, 100, t) }
   307  func BenchmarkDiv1000(t *testing.B)   { benchDiv(Div, 1000, t) }
   308  func BenchmarkDiv10000(t *testing.B)  { benchDiv(Div, 10000, t) }
   309  func BenchmarkDiv100000(t *testing.B) { benchDiv(Div, 100000, t) }
   310  func BenchmarkDiv500000(t *testing.B) { benchDiv(Div, 500000, t) }
   311  
   312  func BenchmarkLDiv1(t *testing.B)      { benchDiv(naiveDiv, 1, t) }
   313  func BenchmarkLDiv2(t *testing.B)      { benchDiv(naiveDiv, 2, t) }
   314  func BenchmarkLDiv3(t *testing.B)      { benchDiv(naiveDiv, 3, t) }
   315  func BenchmarkLDiv4(t *testing.B)      { benchDiv(naiveDiv, 4, t) }
   316  func BenchmarkLDiv5(t *testing.B)      { benchDiv(naiveDiv, 5, t) }
   317  func BenchmarkLDiv10(t *testing.B)     { benchDiv(naiveDiv, 10, t) }
   318  func BenchmarkLDiv100(t *testing.B)    { benchDiv(naiveDiv, 100, t) }
   319  func BenchmarkLDiv1000(t *testing.B)   { benchDiv(naiveDiv, 1000, t) }
   320  func BenchmarkLDiv10000(t *testing.B)  { benchDiv(naiveDiv, 10000, t) }
   321  func BenchmarkLDiv100000(t *testing.B) { benchDiv(naiveDiv, 100000, t) }
   322  func BenchmarkLDiv500000(t *testing.B) { benchDiv(naiveDiv, 500000, t) }
   323  
   324  func benchDivTo(f func(dst, a, b []float64) []float64, sz int, t *testing.B) {
   325  	dst, a, b := z[:sz], x[:sz], y[:sz]
   326  	for i := 0; i < t.N; i++ {
   327  		f(dst, a, b)
   328  	}
   329  }
   330  
   331  var naiveDivTo = func(dst, s, t []float64) []float64 {
   332  	for i, v := range s {
   333  		dst[i] = v / t[i]
   334  	}
   335  	return dst
   336  }
   337  
   338  func BenchmarkDivTo1(t *testing.B)      { benchDivTo(DivTo, 1, t) }
   339  func BenchmarkDivTo2(t *testing.B)      { benchDivTo(DivTo, 2, t) }
   340  func BenchmarkDivTo3(t *testing.B)      { benchDivTo(DivTo, 3, t) }
   341  func BenchmarkDivTo4(t *testing.B)      { benchDivTo(DivTo, 4, t) }
   342  func BenchmarkDivTo5(t *testing.B)      { benchDivTo(DivTo, 5, t) }
   343  func BenchmarkDivTo10(t *testing.B)     { benchDivTo(DivTo, 10, t) }
   344  func BenchmarkDivTo100(t *testing.B)    { benchDivTo(DivTo, 100, t) }
   345  func BenchmarkDivTo1000(t *testing.B)   { benchDivTo(DivTo, 1000, t) }
   346  func BenchmarkDivTo10000(t *testing.B)  { benchDivTo(DivTo, 10000, t) }
   347  func BenchmarkDivTo100000(t *testing.B) { benchDivTo(DivTo, 100000, t) }
   348  func BenchmarkDivTo500000(t *testing.B) { benchDivTo(DivTo, 500000, t) }
   349  
   350  func BenchmarkLDivTo1(t *testing.B)      { benchDivTo(naiveDivTo, 1, t) }
   351  func BenchmarkLDivTo2(t *testing.B)      { benchDivTo(naiveDivTo, 2, t) }
   352  func BenchmarkLDivTo3(t *testing.B)      { benchDivTo(naiveDivTo, 3, t) }
   353  func BenchmarkLDivTo4(t *testing.B)      { benchDivTo(naiveDivTo, 4, t) }
   354  func BenchmarkLDivTo5(t *testing.B)      { benchDivTo(naiveDivTo, 5, t) }
   355  func BenchmarkLDivTo10(t *testing.B)     { benchDivTo(naiveDivTo, 10, t) }
   356  func BenchmarkLDivTo100(t *testing.B)    { benchDivTo(naiveDivTo, 100, t) }
   357  func BenchmarkLDivTo1000(t *testing.B)   { benchDivTo(naiveDivTo, 1000, t) }
   358  func BenchmarkLDivTo10000(t *testing.B)  { benchDivTo(naiveDivTo, 10000, t) }
   359  func BenchmarkLDivTo100000(t *testing.B) { benchDivTo(naiveDivTo, 100000, t) }
   360  func BenchmarkLDivTo500000(t *testing.B) { benchDivTo(naiveDivTo, 500000, t) }
   361  
   362  func benchL1Dist(f func(a, b []float64) float64, sz int, t *testing.B) {
   363  	a, b := x[:sz], y[:sz]
   364  	for i := 0; i < t.N; i++ {
   365  		f(a, b)
   366  	}
   367  }
   368  
   369  var naiveL1Dist = func(s, t []float64) float64 {
   370  	var norm float64
   371  	for i, v := range s {
   372  		norm += math.Abs(t[i] - v)
   373  	}
   374  	return norm
   375  }
   376  
   377  func BenchmarkL1Dist1(t *testing.B)      { benchL1Dist(L1Dist, 1, t) }
   378  func BenchmarkL1Dist2(t *testing.B)      { benchL1Dist(L1Dist, 2, t) }
   379  func BenchmarkL1Dist3(t *testing.B)      { benchL1Dist(L1Dist, 3, t) }
   380  func BenchmarkL1Dist4(t *testing.B)      { benchL1Dist(L1Dist, 4, t) }
   381  func BenchmarkL1Dist5(t *testing.B)      { benchL1Dist(L1Dist, 5, t) }
   382  func BenchmarkL1Dist10(t *testing.B)     { benchL1Dist(L1Dist, 10, t) }
   383  func BenchmarkL1Dist100(t *testing.B)    { benchL1Dist(L1Dist, 100, t) }
   384  func BenchmarkL1Dist1000(t *testing.B)   { benchL1Dist(L1Dist, 1000, t) }
   385  func BenchmarkL1Dist10000(t *testing.B)  { benchL1Dist(L1Dist, 10000, t) }
   386  func BenchmarkL1Dist100000(t *testing.B) { benchL1Dist(L1Dist, 100000, t) }
   387  func BenchmarkL1Dist500000(t *testing.B) { benchL1Dist(L1Dist, 500000, t) }
   388  
   389  func BenchmarkLL1Dist1(t *testing.B)      { benchL1Dist(naiveL1Dist, 1, t) }
   390  func BenchmarkLL1Dist2(t *testing.B)      { benchL1Dist(naiveL1Dist, 2, t) }
   391  func BenchmarkLL1Dist3(t *testing.B)      { benchL1Dist(naiveL1Dist, 3, t) }
   392  func BenchmarkLL1Dist4(t *testing.B)      { benchL1Dist(naiveL1Dist, 4, t) }
   393  func BenchmarkLL1Dist5(t *testing.B)      { benchL1Dist(naiveL1Dist, 5, t) }
   394  func BenchmarkLL1Dist10(t *testing.B)     { benchL1Dist(naiveL1Dist, 10, t) }
   395  func BenchmarkLL1Dist100(t *testing.B)    { benchL1Dist(naiveL1Dist, 100, t) }
   396  func BenchmarkLL1Dist1000(t *testing.B)   { benchL1Dist(naiveL1Dist, 1000, t) }
   397  func BenchmarkLL1Dist10000(t *testing.B)  { benchL1Dist(naiveL1Dist, 10000, t) }
   398  func BenchmarkLL1Dist100000(t *testing.B) { benchL1Dist(naiveL1Dist, 100000, t) }
   399  func BenchmarkLL1Dist500000(t *testing.B) { benchL1Dist(naiveL1Dist, 500000, t) }
   400  
   401  func benchLinfDist(f func(a, b []float64) float64, sz int, t *testing.B) {
   402  	a, b := x[:sz], y[:sz]
   403  	for i := 0; i < t.N; i++ {
   404  		f(a, b)
   405  	}
   406  }
   407  
   408  var naiveLinfDist = func(s, t []float64) float64 {
   409  	var norm float64
   410  	if len(s) == 0 {
   411  		return 0
   412  	}
   413  	norm = math.Abs(t[0] - s[0])
   414  	for i, v := range s[1:] {
   415  		absDiff := math.Abs(t[i+1] - v)
   416  		if absDiff > norm || math.IsNaN(norm) {
   417  			norm = absDiff
   418  		}
   419  	}
   420  	return norm
   421  }
   422  
   423  func BenchmarkLinfDist1(t *testing.B)      { benchLinfDist(LinfDist, 1, t) }
   424  func BenchmarkLinfDist2(t *testing.B)      { benchLinfDist(LinfDist, 2, t) }
   425  func BenchmarkLinfDist3(t *testing.B)      { benchLinfDist(LinfDist, 3, t) }
   426  func BenchmarkLinfDist4(t *testing.B)      { benchLinfDist(LinfDist, 4, t) }
   427  func BenchmarkLinfDist5(t *testing.B)      { benchLinfDist(LinfDist, 5, t) }
   428  func BenchmarkLinfDist10(t *testing.B)     { benchLinfDist(LinfDist, 10, t) }
   429  func BenchmarkLinfDist100(t *testing.B)    { benchLinfDist(LinfDist, 100, t) }
   430  func BenchmarkLinfDist1000(t *testing.B)   { benchLinfDist(LinfDist, 1000, t) }
   431  func BenchmarkLinfDist10000(t *testing.B)  { benchLinfDist(LinfDist, 10000, t) }
   432  func BenchmarkLinfDist100000(t *testing.B) { benchLinfDist(LinfDist, 100000, t) }
   433  func BenchmarkLinfDist500000(t *testing.B) { benchLinfDist(LinfDist, 500000, t) }
   434  
   435  func BenchmarkLLinfDist1(t *testing.B)      { benchLinfDist(naiveLinfDist, 1, t) }
   436  func BenchmarkLLinfDist2(t *testing.B)      { benchLinfDist(naiveLinfDist, 2, t) }
   437  func BenchmarkLLinfDist3(t *testing.B)      { benchLinfDist(naiveLinfDist, 3, t) }
   438  func BenchmarkLLinfDist4(t *testing.B)      { benchLinfDist(naiveLinfDist, 4, t) }
   439  func BenchmarkLLinfDist5(t *testing.B)      { benchLinfDist(naiveLinfDist, 5, t) }
   440  func BenchmarkLLinfDist10(t *testing.B)     { benchLinfDist(naiveLinfDist, 10, t) }
   441  func BenchmarkLLinfDist100(t *testing.B)    { benchLinfDist(naiveLinfDist, 100, t) }
   442  func BenchmarkLLinfDist1000(t *testing.B)   { benchLinfDist(naiveLinfDist, 1000, t) }
   443  func BenchmarkLLinfDist10000(t *testing.B)  { benchLinfDist(naiveLinfDist, 10000, t) }
   444  func BenchmarkLLinfDist100000(t *testing.B) { benchLinfDist(naiveLinfDist, 100000, t) }
   445  func BenchmarkLLinfDist500000(t *testing.B) { benchLinfDist(naiveLinfDist, 500000, t) }