-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
77 lines (72 loc) · 1.26 KB
/
utils.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
72
73
74
75
76
77
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var wgI sync.WaitGroup
var wgD sync.WaitGroup
var wgS sync.WaitGroup
func benchmark(t *BTree) {
st := time.Now()
numOps := 1_000_000
for i := 0; i < numOps; i++ {
t.Insert(i + 1)
if i%1000 == 0 {
fmt.Println("Epoch : #", i/1000)
}
}
x := time.Since(st)
fmt.Printf("Elapsed time: %v\n", x)
fmt.Printf("Number of operations per milliisecond : %v\n", int64(numOps)/x.Milliseconds())
}
func printTree(n *node) {
if n.isLeaf {
for j := 0; j < len(n.keys); j++ {
fmt.Printf("%v ", n.keys[j])
}
} else {
for i := 0; i < len(n.keys); i++ {
printTree(n.children[i])
fmt.Printf("%v ", n.keys[i])
}
printTree(n.children[len(n.keys)])
}
}
func concurrency(t *BTree) {
nums := make([]int, 0)
for i := 0; i < 10; i++ {
wgI.Add(1)
go func() {
x := rand.Intn(100)
nums = append(nums, x)
t.Insert(x)
wgI.Done()
}()
}
wgI.Wait()
for i := 0; i < 10; i++ {
wgS.Add(1)
go func(idx int) {
t.Search(nums[idx])
wgS.Done()
}(i)
}
wgS.Wait()
for i := 0; i < 10; i++ {
wgI.Add(1)
wgS.Add(1)
wgD.Add(1)
go func(idx int) {
t.Update(nums[idx], nums[idx]+100)
wgI.Done()
wgS.Done()
wgD.Done()
}(i)
}
wgI.Wait()
wgS.Wait()
wgD.Wait()
t.PrintTree()
}