github.com/jfcg/sorty@v1.2.0/tune_test.go (about)

     1  // +build tuneparam
     2  
     3  /*	Copyright (c) 2021, Serhat Şevki Dinçer.
     4  	This Source Code Form is subject to the terms of the Mozilla Public
     5  	License, v. 2.0. If a copy of the MPL was not distributed with this
     6  	file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7  */
     8  
     9  package sorty
    10  
    11  import (
    12  	"fmt"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/jfcg/opt"
    17  )
    18  
    19  func printSec(_ string, d time.Duration) float64 {
    20  	return d.Seconds()
    21  }
    22  
    23  func printOpt(x, y int, v float64) {
    24  	fmt.Printf("%3d %3d %5.2fs\n", x, y, v)
    25  }
    26  
    27  var (
    28  	optName = [...]string{"SortU4/F4", "Lsw-U4/F4", "SortS", "Lsw-S"}
    29  
    30  	bufap = make([]uint32, N)
    31  
    32  	optFn = [...]func() float64{
    33  		// optimize for native arithmetic types
    34  		func() float64 { return sumtU4(bufau, bufap[:0]) + sumtF4(bufaf, bufbf[:0]) },
    35  
    36  		// optimize for function-based sort
    37  		// carry over bufap,bufbf for further comparison
    38  		func() float64 { return sumtLswU4(bufau, bufap) + sumtLswF4(bufaf, bufbf) },
    39  
    40  		// optimize for native string
    41  		func() float64 { return sumtS(bufau, bufap[:0]) },
    42  
    43  		// optimize for function-based sort (string key)
    44  		// carry over bufap for further comparison
    45  		func() float64 { return sumtLswS(bufau, bufap) }}
    46  )
    47  
    48  // Optimize max slice lengths for insertion sort/recursion (Mli,Hmli,Mlr)
    49  // Takes a long time, run with -tags tuneparam
    50  func TestOptimize(t *testing.T) {
    51  	tsPtr = t
    52  
    53  	s1, s2 := "Mli", 96
    54  
    55  	for i := 0; i < len(optFn); i++ {
    56  		fmt.Printf("\n%s\n%s Mlr:\n", optName[i], s1)
    57  
    58  		_, _, _, n := opt.FindMinTri(2, s2, 480, 16, 120,
    59  			func(x, y int) float64 {
    60  				if x < 10 || y <= 2*x {
    61  					return 9e9 // keep parameters feasible
    62  				}
    63  				Mli, Hmli, Mlr = x, x, y
    64  				return optFn[i]()
    65  			}, printOpt)
    66  		fmt.Println(n, "calls")
    67  
    68  		s1, s2 = "Hmli", 48
    69  	}
    70  }