File: shuffle_go17_test.go

package info (click to toggle)
golang-github-shogo82148-go-shuffle 0.0~git20180218.27e6095-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 100 kB
  • sloc: makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,334 bytes parent folder | download | duplicates (2)
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
//+build go1.7

package shuffle_test

import (
	"fmt"
	"math/rand"
	"testing"

	"github.com/shogo82148/go-shuffle"
)

func ExampleInts() {
	x := []int{1, 2, 3, 4, 5}
	shuffle.Ints(x)
	for _, value := range x {
		fmt.Println(value)
		// Unordered output:
		// 1
		// 2
		// 3
		// 4
		// 5
	}
}

func BenchmarkInts(b *testing.B) {
	for _, n := range []int{1, 10, 100, 1000, 10000} {

		b.Run(fmt.Sprintf("shuffle %d", n), func(b *testing.B) {
			a := make([]int, n)
			for i := 0; i < b.N; i++ {
				shuffle.Ints(a)
			}
		})

		b.Run(fmt.Sprintf("perm %d", n), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				rand.Perm(n)
			}
		})

		b.Run(fmt.Sprintf("perm and move %d", n), func(b *testing.B) {
			s1 := make([]int, n)
			s2 := make([]int, n)
			for i := 0; i < b.N; i++ {
				for i, j := range rand.Perm(n) {
					s2[i] = s1[j]
				}
			}
		})

	}
}

func BenchmarkFloat64s(b *testing.B) {
	for _, n := range []int{1, 10, 100, 1000, 10000} {

		b.Run(fmt.Sprintf("shuffle %d", n), func(b *testing.B) {
			a := make([]float64, n)
			for i := 0; i < b.N; i++ {
				shuffle.Float64s(a)
			}
		})

		b.Run(fmt.Sprintf("perm and move %d", n), func(b *testing.B) {
			s1 := make([]float64, n)
			s2 := make([]float64, n)
			for i := 0; i < b.N; i++ {
				for i, j := range rand.Perm(n) {
					s2[i] = s1[j]
				}
			}
		})

	}
}