-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonEncoderTest.swift
150 lines (132 loc) · 3.7 KB
/
jsonEncoderTest.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
140
141
142
143
144
145
146
147
148
149
import Foundation
#if os(macOS) || os(iOS)
import Darwin
let CLOCK_MONOTONIC = _CLOCK_MONOTONIC
#else
import Glibc
#endif
let DEBUG = Bool(ProcessInfo.processInfo.environment["DEBUG"] ?? "false") ?? false
let encoder = JSONEncoder()
struct MyValue: Codable {
let int1:Int
let int2:Int
let int3:Int
let int4:Int
let int5:Int
let int6:Int
let int7:Int
let int8:Int
let int9:Int
let int10:Int
init() {
int1 = 1
int2 = 12
int3 = 123
int4 = 1234
int5 = 12345
int6 = 123456
int7 = 1234567
int8 = 12345678
int9 = 123456789
int10 = 1234567890
}
}
// Codable (compatible with JSONEncoder)
let myValue = MyValue()
// Codable dictionary (compatible with JSONEncoder and JSONSerialization)
let myDict: [String:Int] = ["int1": 1, "int2": 12, "int3": 123, "int4": 1234,
"int5": 12345, "int6": 123456, "int7": 1234567, "int8": 12345678,
"int9": 123456789, "int10": 1234567890]
// Array (compatible with JSONSerialization)
//let myArray: [Any] = ["int1", "int2", "int3", "int4", "int5", "int6", "int7", "int8", "int9", "int10", 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890]
// Codable Array (compatible with JSONEncoder and JSONSerialization)
let codableArray: [Int] = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890]
let iterations = 1000
func benchmark(_ work: () -> Void) -> Double {
var start = timespec()
var end = timespec()
clock_gettime(CLOCK_MONOTONIC, &start)
work()
clock_gettime(CLOCK_MONOTONIC, &end)
return (Double(end.tv_sec) * 1.0e9 + Double(end.tv_nsec)) - (Double(start.tv_sec) * 1.0e9 + Double(start.tv_nsec))
}
func jsonEncoder_Struct() {
do {
for i in 1...iterations {
let result = try encoder.encode(myValue)
if DEBUG && i == 1 {
print("Result (JSONEncoder Struct): \(String(data: result, encoding: .utf8) ?? "nil")")
}
}
} catch {
print("Fail")
}
}
func jsonEncoder_Dict() {
do {
for i in 1...iterations {
let result = try encoder.encode(myDict)
if DEBUG && i == 1 {
print("Result (JSONEncoder Dict): \(String(data: result, encoding: .utf8) ?? "nil")")
}
}
} catch {
print("Fail")
}
}
func jsonEncoder_Array() {
do {
for i in 1...iterations {
let result = try encoder.encode(codableArray)
if DEBUG && i == 1 {
print("Result (JSONEncoder Array): \(String(data: result, encoding: .utf8) ?? "nil")")
}
}
} catch {
print("Fail")
}
}
func jsonSerialization_Dict() {
do {
for i in 1...iterations {
let result = try JSONSerialization.data(withJSONObject: myDict)
if DEBUG && i == 1 {
print("Result (JSONSerialization): \(String(data: result, encoding: .utf8) ?? "nil")")
}
}
} catch {
print("Fail")
}
}
func jsonSerialization_Array() {
do {
for i in 1...iterations {
let result = try JSONSerialization.data(withJSONObject: codableArray)
if DEBUG && i == 1 {
print("Result (JSONSerialization): \(String(data: result, encoding: .utf8) ?? "nil")")
}
}
} catch {
print("Fail")
}
}
var timeNanos = benchmark {
jsonEncoder_Struct()
}
print("JSONEncoder (Struct) took \(timeNanos / Double(iterations)) ns")
timeNanos = benchmark {
jsonEncoder_Dict()
}
print("JSONEncoder (Dict) took \(timeNanos / Double(iterations)) ns")
timeNanos = benchmark {
jsonEncoder_Array()
}
print("JSONEncoder (Array) took \(timeNanos / Double(iterations)) ns")
timeNanos = benchmark {
jsonSerialization_Dict()
}
print("JSONSerialization (Dict) took \(timeNanos / Double(iterations)) ns")
timeNanos = benchmark {
jsonSerialization_Array()
}
print("JSONSerialization (Array) took \(timeNanos / Double(iterations)) ns")