JSON 使用多核解析
JSON Parsing using multiple cores
iOS 设备越来越好,拥有更多内核,但我们如何在解析时从中获益 JSON?
目前,
我正在使用 JSONDecoder() 进行 JSON 解析。有什么办法可以更快吗?可能使用多线程分部分解析等
任何 hints/pointers 将不胜感激。
import Foundation
let filePath = Bundle.main.path(forResource: "json", ofType: "json")
struct Vehicle: Codable {
private let color: String
private let tires: [Tire]
private let number: String
}
struct Tire: Codable {
private let company: String
private let isNew: Bool
}
func parseData(_ data: Data) {
let decoder = JSONDecoder()
try? decoder.decode([Vehicle].self, from: data)
}
func modifiedParsing(_ data: Data) {
}
let data = try String(contentsOfFile: filePath!).data(using: .utf8)
let date = Date()
let start = date.timeIntervalSince1970
//parseData(data!)
let end = Date().timeIntervalSince1970
print("Time Taken \(end-start)")
/*
Initial Times: [3.728722095489502, 3.5913820266723633, 3.5568389892578125, 3.534559965133667, 3.506725311279297]
After Changes
*/
我想让 JSON 解析得更快。
对于任何正在寻找好的解决方案的人,请参考:https://github.com/bwhiteley/JSONShootout
Marshal 比 codable 快。
iOS 设备越来越好,拥有更多内核,但我们如何在解析时从中获益 JSON?
目前, 我正在使用 JSONDecoder() 进行 JSON 解析。有什么办法可以更快吗?可能使用多线程分部分解析等
任何 hints/pointers 将不胜感激。
import Foundation
let filePath = Bundle.main.path(forResource: "json", ofType: "json")
struct Vehicle: Codable {
private let color: String
private let tires: [Tire]
private let number: String
}
struct Tire: Codable {
private let company: String
private let isNew: Bool
}
func parseData(_ data: Data) {
let decoder = JSONDecoder()
try? decoder.decode([Vehicle].self, from: data)
}
func modifiedParsing(_ data: Data) {
}
let data = try String(contentsOfFile: filePath!).data(using: .utf8)
let date = Date()
let start = date.timeIntervalSince1970
//parseData(data!)
let end = Date().timeIntervalSince1970
print("Time Taken \(end-start)")
/*
Initial Times: [3.728722095489502, 3.5913820266723633, 3.5568389892578125, 3.534559965133667, 3.506725311279297]
After Changes
*/
我想让 JSON 解析得更快。 对于任何正在寻找好的解决方案的人,请参考:https://github.com/bwhiteley/JSONShootout
Marshal 比 codable 快。