Last active
April 25, 2020 17:40
-
-
Save popmedic/4f003c2df3d668efa2dee6f1206f6aa2 to your computer and use it in GitHub Desktop.
Plist vs. JSON for converting Encodable implementation to Any (Dictionary)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct LotsOfStuff: Codable { | |
let x: Int | |
let y: Int | |
let z: Double | |
let a: [String] | |
let b: String | |
let c: String | |
let less: LessStuff | |
} | |
struct LessStuff: Codable { | |
let x: Int | |
let y: Int | |
let a: String | |
} | |
var object = LotsOfStuff(x: 1, y: 2, z: 3.0, | |
a: ["this", "is", "a", "test"], | |
b: "this is a test", c: "of the emergancy broadcast system", | |
less: LessStuff(x: 4, y: 5, a: "I ❤️ Denver")) | |
do { | |
var start: TimeInterval | |
start = Date().timeIntervalSince1970 | |
for _ in [0..<1000000] { | |
_ = try JSONSerialization.jsonObject(with: try JSONEncoder().encode(object)) | |
} | |
print("json took: \(Date().timeIntervalSince1970 - start)") | |
start = Date().timeIntervalSince1970 | |
for _ in [0..<1000000] { | |
_ = try PropertyListSerialization.propertyList(from: try PropertyListEncoder().encode(object), | |
options: [], format: nil) | |
} | |
print("plist took: \(Date().timeIntervalSince1970 - start)") | |
} catch { | |
print("error: \(error.localizedDescription)") | |
exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment