-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
141 lines (125 loc) · 3.71 KB
/
main.swift
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Created by lalawue on 2021/03/19
//
// test re-balance
import Foundation
protocol TestUnit: AnyObject {
func set(_ key: Int, _ value: Int?)
func prefix(count: Int) -> [Int]
func count() -> Int
}
class UnitDict: TestUnit {
var dict = [Int:Int]()
func set(_ key: Int, _ value: Int?) {
dict[key] = value
}
func prefix(count: Int) -> [Int] {
return dict.sorted() { $0.key < $1.key }.prefix(count).map { $0.value }
}
func count() -> Int {
return dict.count
}
}
class UnitSD: TestUnit {
var map = SortedDictionary<Int,Int?>()
func set(_ key: Int, _ value: Int?) {
map[key] = value
}
func prefix(count: Int) -> [Int] {
var arr = [Int]()
var index = Int(0)
map.forEach() { (_, _, value, stop) in
if let `value` = value {
if index >= count {
stop = true
}
arr.append(value)
index += 1
}
}
return arr
}
func count() -> Int {
return map.count
}
}
class Test {
/// amount : setting data count
/// addition: delete/replace data count
/// prefix : only take prefix after sort
/// loop : set/delete/sort round times
static func testUnit(container: TestUnit,
amount: Int,
addition: Int,
prefix: Int,
loop: Int) -> Bool {
var avg = UInt64(0)
var round = UInt64(0)
var last = DispatchTime.now()
while round < loop {
round += 1
var left = Int(1)
var right = amount
while left < right {
container.set(left, left)
container.set(right, right)
left += 1
right -= 1
}
var idx = Int(0)
var an = Int(0)
while idx < addition {
if an % 5 == 0 {
container.set(an, nil)
} else {
container.set(an, an)
}
an += 1
idx += 1
if an >= amount {
an = 0
}
let result = container.prefix(count: prefix)
var index = Int(0)
var rn = Int(0)
while index < prefix {
if rn < idx && rn % 5 == 0 {
rn += 1
}
if result[index] != rn {
return false
}
index += 1
rn += 1
}
}
let now = DispatchTime.now()
let elapsed = now.uptimeNanoseconds - last.uptimeNanoseconds
avg += elapsed
print("round \(round): \(elapsed/1000000)ms, avg: \(avg / round / 1000000)ms")
last = now
}
return true
}
}
let amount = Int(256) // impact factor
let addition = Int(512) // impact factor
let prefix = Int(16) /// impact factor
let loop = Int(10)
print("with amount:\(amount) addition:\(addition) prefix:\(prefix) loop:\(loop)")
print("\nTest Dicionary:")
if !Test.testUnit(container: UnitDict(),
amount: amount,
addition: addition,
prefix: prefix,
loop: loop) {
print("failed to test ud")
}
print("\nTest SortedDicionary:")
if !Test.testUnit(container: UnitSD(),
amount: amount,
addition: addition,
prefix: prefix,
loop: loop) {
print("failed to test us")
}