File: bench_test.go

package info (click to toggle)
golang-github-xtgo-set 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (85 lines) | stat: -rw-r--r-- 3,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2015 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package set_test

import (
	"sort"
	"testing"

	"github.com/xtgo/set"
	"github.com/xtgo/set/internal/sliceset"
	td "github.com/xtgo/set/internal/testdata"
)

func BenchmarkUnion64K_revcat(b *testing.B) { benchMut(b, "Union", td.RevCat(2, td.Large)) }
func BenchmarkUnion32(b *testing.B)         { benchMut(b, "Union", td.Overlap(2, td.Small)) }
func BenchmarkUnion64K(b *testing.B)        { benchMut(b, "Union", td.Overlap(2, td.Large)) }
func BenchmarkUnion_alt32(b *testing.B)     { benchMut(b, "Union", td.Alternate(2, td.Small)) }
func BenchmarkUnion_alt64K(b *testing.B)    { benchMut(b, "Union", td.Alternate(2, td.Large)) }
func BenchmarkInter32(b *testing.B)         { benchMut(b, "Inter", td.Overlap(2, td.Small)) }
func BenchmarkInter64K(b *testing.B)        { benchMut(b, "Inter", td.Overlap(2, td.Large)) }
func BenchmarkInter_alt32(b *testing.B)     { benchMut(b, "Inter", td.Alternate(2, td.Small)) }
func BenchmarkInter_alt64K(b *testing.B)    { benchMut(b, "Inter", td.Alternate(2, td.Large)) }
func BenchmarkDiff32(b *testing.B)          { benchMut(b, "Diff", td.Overlap(2, td.Small)) }
func BenchmarkDiff64K(b *testing.B)         { benchMut(b, "Diff", td.Overlap(2, td.Large)) }
func BenchmarkDiff_alt32(b *testing.B)      { benchMut(b, "Diff", td.Alternate(2, td.Small)) }
func BenchmarkDiff_alt64K(b *testing.B)     { benchMut(b, "Diff", td.Alternate(2, td.Large)) }
func BenchmarkSymDiff32(b *testing.B)       { benchMut(b, "SymDiff", td.Overlap(2, td.Small)) }
func BenchmarkSymDiff64K(b *testing.B)      { benchMut(b, "SymDiff", td.Overlap(2, td.Large)) }
func BenchmarkSymDiff_alt32(b *testing.B)   { benchMut(b, "SymDiff", td.Alternate(2, td.Small)) }
func BenchmarkSymDiff_alt64K(b *testing.B)  { benchMut(b, "SymDiff", td.Alternate(2, td.Large)) }

func BenchmarkIsInter32(b *testing.B)      { benchBool(b, "IsInter", td.Overlap(2, td.Small)) }
func BenchmarkIsInter64K(b *testing.B)     { benchBool(b, "IsInter", td.Overlap(2, td.Large)) }
func BenchmarkIsInter_alt32(b *testing.B)  { benchBool(b, "IsInter", td.Alternate(2, td.Small)) }
func BenchmarkIsInter_alt64K(b *testing.B) { benchBool(b, "IsInter", td.Alternate(2, td.Large)) }

func BenchmarkApply256_64K(b *testing.B) { benchApply(b, td.Rand(256, td.Large)) }

func benchMut(b *testing.B, name string, sets [][]int) {
	var op mutOp
	td.ConvMethod(&op, sliceset.Set(nil), name)
	bench(b, func(a, b sliceset.Set) { op(a, b) }, sets)
}

func benchBool(b *testing.B, name string, sets [][]int) {
	var op boolOp
	td.ConvMethod(&op, sliceset.Set(nil), name)
	bench(b, func(a, b sliceset.Set) { op(a, b) }, sets)
}

func bench(b *testing.B, op func(a, b sliceset.Set), sets [][]int) {
	s, t := sets[0], sets[1]
	data := make([]int, 0, len(s)+len(t))

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		data = append(data[:0], s...)
		op(data, t)
	}
}

func pivots(sets [][]int) []int {
	lengths := make([]int, len(sets))
	for i, set := range sets {
		lengths[i] = len(set)
	}
	return set.Pivots(lengths...)
}

func benchApply(b *testing.B, sets [][]int) {
	pivots := pivots(sets)
	n := len(sets) - 1
	data := make(sort.IntSlice, 0, pivots[n])

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		data = data[:0]
		for _, set := range sets {
			data = append(data, set...)
		}
		set.Apply(set.Inter, data, pivots)
	}
}