Skip to content

Instantly share code, notes, and snippets.

@sidepelican
Created October 9, 2020 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidepelican/3604669c43a665189ee151350c49399d to your computer and use it in GitHub Desktop.
Save sidepelican/3604669c43a665189ee151350c49399d to your computer and use it in GitHub Desktop.
import Foundation
public struct Airport: Codable {
let name: String
let iata: String
let icao: String
let coordinates: [Double]
public struct Runway: Codable {
enum Surface: String, Codable {
case rigid, flexible, gravel, sealed, unpaved, other
}
let direction: String
let distance: Int
let surface: Surface
}
let runways: [Runway]
}
extension Airport {
public init(json: [String: Any]) {
guard let name = json["name"] as? String,
let iata = json["iata"] as? String,
let icao = json["icao"] as? String,
let coordinates = json["coordinates"] as? [Double],
let runways = json["runways"] as? [[String: Any]]
else {
fatalError("Cannot initialize Airport from JSON")
}
self.name = name
self.iata = iata
self.icao = icao
self.coordinates = coordinates
self.runways = runways.map { Runway(json: $0) }
}
}
extension Airport.Runway {
public init(json: [String: Any]) {
guard let direction = json["direction"] as? String,
let distance = json["distance"] as? Int,
let surfaceRawValue = json["surface"] as? String,
let surface = Surface(rawValue: surfaceRawValue)
else {
fatalError("Cannot initialize Runway from JSON")
}
self.direction = direction
self.distance = distance
self.surface = surface
}
}
let data = try! Data(contentsOf: URL(fileURLWithPath: ProcessInfo.processInfo.arguments[1]))
func measure(_ block: () -> ()) {
let start = Date()
for _ in 0..<10 {
block()
}
let end = Date()
print("elapsed:", end.timeIntervalSince(start))
}
measure {
let decoder = JSONDecoder()
let airports = try! decoder.decode([Airport].self, from: data)
assert(airports.count == 10000)
}
measure {
let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]
let airports = json.map{ Airport(json: $0) }
assert(airports.count == 10000)
}
FROM swift:5.3-focal as build
COPY ./ ./
RUN swift build -c release
CMD ["./.build/release/CodablePerformance", "./airports10000.json"]
@sidepelican
Copy link
Author

airports10000.jsonここ から拾ってきた

@sidepelican
Copy link
Author

雑な計測結果
Codable on mac: 2.3s
JSONS.. on mac: 1.0s
Codable on docker4mac: 9.2s
JSONS.. on docker4mac: 2.3s

@sidepelican
Copy link
Author

全体的な実装はこちらのリポジトリから拝借しました https://github.com/Flight-School/CodablePerformance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment