github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekamath/stat_test.go (about)

     1  // Copyright © 2020-2022. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekamath_test
     7  
     8  import (
     9  	"strconv"
    10  	"testing"
    11  
    12  	"github.com/qioalice/ekago/v3/ekamath"
    13  )
    14  
    15  func BenchmarkStat(b *testing.B) {
    16  
    17  	var statWeight = []int{100, 1_000, 10_000, 100_000}
    18  
    19  	var genBench = func(n int, s ekamath.Stat[int]) func(*testing.B) {
    20  		return func(b *testing.B) {
    21  			b.ReportAllocs()
    22  			for i := 0; i < b.N; i++ {
    23  				s.Clear()
    24  				for j := 0; j < n; j++ {
    25  					s.Count(j)
    26  				}
    27  				_ = s.Avg()
    28  			}
    29  		}
    30  	}
    31  
    32  	var sc = ekamath.NewStatCumulative[int]()
    33  	var si = ekamath.NewStatIterative[int]()
    34  
    35  	for i, n := 0, len(statWeight); i < n; i++ {
    36  		var weightStr = strconv.Itoa(statWeight[i])
    37  
    38  		b.Run("Cumulative"+"-"+weightStr, genBench(statWeight[i], sc))
    39  		b.Run("Iterative"+"-"+weightStr, genBench(statWeight[i], si))
    40  	}
    41  }