-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_slimarray_stat_test.go
71 lines (58 loc) · 1.28 KB
/
example_slimarray_stat_test.go
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
package slimarray_test
import (
"fmt"
"math/rand"
"sort"
"github.com/openacid/slimarray"
)
func ExampleSlimArray_Stat() {
fmt.Println("== Memory cost stats of sorted random uint array ==")
cases := []struct {
n int
rng uint32
}{
{1000, 1000},
{1000 * 1000, 1000 * 1000},
{1000 * 1000, 1000 * 1000 * 1000},
}
for _, c := range cases {
n := c.n
rng := c.rng
nums := []uint32{}
rnd := rand.New(rand.NewSource(int64(n) * int64(rng)))
for i := 0; i < n; i++ {
s := uint32(rnd.Float64() * float64(rng))
nums = append(nums, s)
}
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] })
a := slimarray.NewU32(nums)
st := a.Stat()
fmt.Printf("\nn=%d rng=[0, %d]:\n\n", n, rng)
for _, k := range []string{
// "mem_elts", "span_cnt", "spans/seg", "elt_width",
"n", "mem_total", "bits/elt",
} {
fmt.Printf(" %10s: %d\n", k, st[k])
}
}
// Output:
// == Memory cost stats of sorted random uint array ==
//
// n=1000 rng=[0, 1000]:
//
// n: 1000
// mem_total: 856
// bits/elt: 6
//
// n=1000000 rng=[0, 1000000]:
//
// n: 1000000
// mem_total: 705720
// bits/elt: 5
//
// n=1000000 rng=[0, 1000000000]:
//
// n: 1000000
// mem_total: 2078336
// bits/elt: 16
}