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