- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在从我们的 API 使用 SwiftyJson 解析 JSON 并填充核心数据时,我遇到了严重的性能问题。
数据是使用 Alamofire 下载的,它运行良好,但使用 SwiftyJson 解析 JSON 非常慢。为了查看库是否真的是问题所在,我在解析数据的众多地方之一重写了 json 解析。在下面的代码中,我正在解析大约 400 个旅游景点之一的开放时间。
查看这些屏幕截图中的差异,7.7 秒到 185 毫秒:
Swifty 方式:
let openDescription:String = json["OpeningHours"]["OpeningHoursGenericExceptions"].string!
let monOpen:[String] = json["OpeningHours"]["Monday"]["From"].string!.componentsSeparatedByString(":")
let monClose:[String] = json["OpeningHours"]["Monday"]["To"].string!.componentsSeparatedByString(":")
let tueOpen:[String] = json["OpeningHours"]["Tuesday"]["From"].string!.componentsSeparatedByString(":")
let tueClose:[String] = json["OpeningHours"]["Tuesday"]["To"].string!.componentsSeparatedByString(":")
let wedOpen:[String] = json["OpeningHours"]["Wednesday"]["From"].string!.componentsSeparatedByString(":")
let wedClose:[String] = json["OpeningHours"]["Wednesday"]["To"].string!.componentsSeparatedByString(":")
let thuOpen:[String] = json["OpeningHours"]["Thursday"]["From"].string!.componentsSeparatedByString(":")
let thuClose:[String] = json["OpeningHours"]["Thursday"]["To"].string!.componentsSeparatedByString(":")
let friOpen:[String] = json["OpeningHours"]["Friday"]["From"].string!.componentsSeparatedByString(":")
let friClose:[String] = json["OpeningHours"]["Friday"]["To"].string!.componentsSeparatedByString(":")
let satOpen:[String] = json["OpeningHours"]["Saturday"]["From"].string!.componentsSeparatedByString(":")
let satClose:[String] = json["OpeningHours"]["Saturday"]["To"].string!.componentsSeparatedByString(":")
let sunOpen:[String] = json["OpeningHours"]["Sunday"]["From"].string!.componentsSeparatedByString(":")
let sunClose:[String] = json["OpeningHours"]["Sunday"]["To"].string!.componentsSeparatedByString(":")
原生方式:
var monOpen:[String] = []
var monClose:[String] = []
var tueOpen:[String] = []
var tueClose:[String] = []
var wedOpen:[String] = []
var wedClose:[String] = []
var thuOpen:[String] = []
var thuClose:[String] = []
var friOpen:[String] = []
var friClose:[String] = []
var satOpen:[String] = []
var satClose:[String] = []
var sunOpen:[String] = []
var sunClose:[String] = []
var openDescription:String = ""
if let attractionsArray = orgJson as? NSArray{
if let attraction = attractionsArray[0] as? NSDictionary{
if let openHours = attraction["OpeningHours"] as? NSDictionary{
if let day = openHours["Monday"] as? NSDictionary{
if let open = day["From"] as? String{
monOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
monClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Tuesday"] as? NSDictionary{
if let open = day["From"] as? String{
tueOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
tueClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Wednesday"] as? NSDictionary{
if let open = day["From"] as? String{
wedOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
wedClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Thursday"] as? NSDictionary{
if let open = day["From"] as? String{
thuOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
thuClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Friday"] as? NSDictionary{
if let open = day["From"] as? String{
friOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
friClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Saturday"] as? NSDictionary{
if let open = day["From"] as? String{
satOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
satClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Sunday"] as? NSDictionary{
if let open = day["From"] as? String{
sunOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
sunClose = close.componentsSeparatedByString(":")
}
}
if let desc = openHours["OpeningHoursGenericExceptions"] as? String{
openDescription = desc
}
}
}
}
这只是被解析数据的一部分,因此应用中的性能非常明显。
我想问题是,我是否错误地使用了 SwiftyJSON 还是这是预料之中的?
最佳答案
首先,你的“native way”不等同于“Swifty way”。
SwiftyJSON版本有45个subscript
访问,而原生方式只有23个subscript
访问。
要使“本地方式”等效,它应该是这样的:
let openDescription = attraction["OpeningHours"]!["OpeningHoursGenericExceptions"] as String
let monOpen = (attraction["OpeningHours"]!["Monday"]!!["From"] as String).componentsSeparatedByString(":")
let monClose = (attraction["OpeningHours"]!["Monday"]!!["To"] as String).componentsSeparatedByString(":")
let tueOpen = (attraction["OpeningHours"]!["Tuesday"]!!["From"] as String).componentsSeparatedByString(":")
let tueClose = (attraction["OpeningHours"]!["Tuesday"]!!["To"]! as String).componentsSeparatedByString(":")
// ...
或“Swifty way”应该是这样的:
let openHours = json[0]["OpeningHours"]
var day:JSON
day = openHours["Monday"]
if let open = day["From"].string {
monOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
monClose = close.componentsSeparatedByString(":")
}
day = openHours["Tuesday"]
if let open = day["From"].string {
tueOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
tueClose = close.componentsSeparatedByString(":")
}
// ...
无论如何,是的,SwiftyJSON 很慢。让我们测量一下:
import Foundation
import XCTest
let orgJson:AnyObject = [
[
"OpeningHours": [
"OpeningHoursGenericExceptions": "test",
"Monday": ["From":"1:2:3","To":"1:2:3"],
"Tuesday": ["From":"1:2:3","To":"1:2:3"],
"Wednesday": ["From":"1:2:3","To":"1:2:3"],
"Thursday": ["From":"1:2:3","To":"1:2:3"],
"Friday": ["From":"1:2:3","To":"1:2:3"],
"Saturday": ["From":"1:2:3","To":"1:2:3"],
"Sunday": ["From":"1:2:3","To":"1:2:3"],
]
]
]
let json = JSON(orgJson)
class JSONTestTests: XCTestCase {
func testNativeSubscript() {
measureBlock { () -> Void in
for _ in 0 ..< 400 {
autoreleasepool {
if let attraction = orgJson[0] as? NSDictionary {
let openDescription = attraction["OpeningHours"]!["OpeningHoursGenericExceptions"] as String
let monOpen = (attraction["OpeningHours"]!["Monday"]!!["From"] as String).componentsSeparatedByString(":")
let monClose = (attraction["OpeningHours"]!["Monday"]!!["To"] as String).componentsSeparatedByString(":")
let tueOpen = (attraction["OpeningHours"]!["Tuesday"]!!["From"] as String).componentsSeparatedByString(":")
let tueClose = (attraction["OpeningHours"]!["Tuesday"]!!["To"] as String).componentsSeparatedByString(":")
let wedOpen = (attraction["OpeningHours"]!["Wednesday"]!!["From"] as String).componentsSeparatedByString(":")
let wedClose = (attraction["OpeningHours"]!["Wednesday"]!!["To"] as String).componentsSeparatedByString(":")
let thuOpen = (attraction["OpeningHours"]!["Thursday"]!!["From"] as String).componentsSeparatedByString(":")
let thuClose = (attraction["OpeningHours"]!["Thursday"]!!["To"] as String).componentsSeparatedByString(":")
let friOpen = (attraction["OpeningHours"]!["Friday"]!!["From"] as String).componentsSeparatedByString(":")
let friClose = (attraction["OpeningHours"]!["Friday"]!!["To"] as String).componentsSeparatedByString(":")
let satOpen = (attraction["OpeningHours"]!["Saturday"]!!["From"] as String).componentsSeparatedByString(":")
let satClose = (attraction["OpeningHours"]!["Saturday"]!!["To"] as String).componentsSeparatedByString(":")
let sunOpen = (attraction["OpeningHours"]!["Sunday"]!!["From"] as String).componentsSeparatedByString(":")
let sunClose = (attraction["OpeningHours"]!["Sunday"]!!["To"] as String).componentsSeparatedByString(":")
XCTAssertEqual(monOpen, ["1","2","3"], "")
XCTAssertEqual(openDescription, "test")
}
}
}
}
}
func testJSONSubscript() {
measureBlock { () -> Void in
for _ in 0 ..< 400 {
autoreleasepool {
let attraction = json[0]
let openDescription:String = attraction["OpeningHours"]["OpeningHoursGenericExceptions"].string!
let monOpen:[String] = attraction["OpeningHours"]["Monday"]["From"].string!.componentsSeparatedByString(":")
let monClose:[String] = attraction["OpeningHours"]["Monday"]["To"].string!.componentsSeparatedByString(":")
let tueOpen:[String] = attraction["OpeningHours"]["Tuesday"]["From"].string!.componentsSeparatedByString(":")
let tueClose:[String] = attraction["OpeningHours"]["Tuesday"]["To"].string!.componentsSeparatedByString(":")
let wedOpen:[String] = attraction["OpeningHours"]["Wednesday"]["From"].string!.componentsSeparatedByString(":")
let wedClose:[String] = attraction["OpeningHours"]["Wednesday"]["To"].string!.componentsSeparatedByString(":")
let thuOpen:[String] = attraction["OpeningHours"]["Thursday"]["From"].string!.componentsSeparatedByString(":")
let thuClose:[String] = attraction["OpeningHours"]["Thursday"]["To"].string!.componentsSeparatedByString(":")
let friOpen:[String] = attraction["OpeningHours"]["Friday"]["From"].string!.componentsSeparatedByString(":")
let friClose:[String] = attraction["OpeningHours"]["Friday"]["To"].string!.componentsSeparatedByString(":")
let satOpen:[String] = attraction["OpeningHours"]["Saturday"]["From"].string!.componentsSeparatedByString(":")
let satClose:[String] = attraction["OpeningHours"]["Saturday"]["To"].string!.componentsSeparatedByString(":")
let sunOpen:[String] = attraction["OpeningHours"]["Sunday"]["From"].string!.componentsSeparatedByString(":")
let sunClose:[String] = attraction["OpeningHours"]["Sunday"]["To"].string!.componentsSeparatedByString(":")
XCTAssertEqual(monOpen, ["1","2","3"], "")
}
}
}
}
func testNativeBinding() {
measureBlock { () -> Void in
for _ in 0 ..< 400 {
autoreleasepool {
var monOpen:[String] = []
var monClose:[String] = []
var tueOpen:[String] = []
var tueClose:[String] = []
var wedOpen:[String] = []
var wedClose:[String] = []
var thuOpen:[String] = []
var thuClose:[String] = []
var friOpen:[String] = []
var friClose:[String] = []
var satOpen:[String] = []
var satClose:[String] = []
var sunOpen:[String] = []
var sunClose:[String] = []
var openDescription:String = ""
if let attractionsArray = orgJson as? NSArray{
if let attraction = attractionsArray[0] as? NSDictionary{
if let openHours = attraction["OpeningHours"] as? NSDictionary{
if let day = openHours["Monday"] as? NSDictionary{
if let open = day["From"] as? String{
monOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
monClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Tuesday"] as? NSDictionary{
if let open = day["From"] as? String{
tueOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
tueClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Wednesday"] as? NSDictionary{
if let open = day["From"] as? String{
wedOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
wedClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Thursday"] as? NSDictionary{
if let open = day["From"] as? String{
thuOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
thuClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Friday"] as? NSDictionary{
if let open = day["From"] as? String{
friOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
friClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Saturday"] as? NSDictionary{
if let open = day["From"] as? String{
satOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
satClose = close.componentsSeparatedByString(":")
}
}
if let day = openHours["Sunday"] as? NSDictionary{
if let open = day["From"] as? String{
sunOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"] as? String{
sunClose = close.componentsSeparatedByString(":")
}
}
if let desc = openHours["OpeningHoursGenericExceptions"] as? String{
openDescription = desc
}
}
}
}
XCTAssertEqual(monOpen, ["1","2","3"], "")
}
}
}
}
func testJSONBinding() {
measureBlock { () -> Void in
for _ in 0 ..< 400 {
autoreleasepool {
var monOpen:[String] = []
var monClose:[String] = []
var tueOpen:[String] = []
var tueClose:[String] = []
var wedOpen:[String] = []
var wedClose:[String] = []
var thuOpen:[String] = []
var thuClose:[String] = []
var friOpen:[String] = []
var friClose:[String] = []
var satOpen:[String] = []
var satClose:[String] = []
var sunOpen:[String] = []
var sunClose:[String] = []
var openDescription:String = ""
let openHours = json[0]["OpeningHours"]
var day:JSON
day = openHours["Monday"]
if let open = day["From"].string {
monOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
monClose
= close.componentsSeparatedByString(":")
}
day = openHours["Tuesday"]
if let open = day["From"].string {
tueOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
tueClose = close.componentsSeparatedByString(":")
}
day = openHours["WednesDay"]
if let open = day["From"].string {
wedOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
wedClose = close.componentsSeparatedByString(":")
}
day = openHours["Thursday"]
if let open = day["From"].string {
thuOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
thuClose = close.componentsSeparatedByString(":")
}
day = openHours["Friday"]
if let open = day["From"].string {
friOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
friClose = close.componentsSeparatedByString(":")
}
day = openHours["Saturday"]
if let open = day["From"].string {
satOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
satClose = close.componentsSeparatedByString(":")
}
day = openHours["Sunday"]
if let open = day["From"].string {
sunOpen = open.componentsSeparatedByString(":")
}
if let close = day["To"].string {
sunClose = close.componentsSeparatedByString(":")
}
XCTAssertEqual(monOpen, ["1","2","3"], "")
}
}
}
}
}
输出:
<unknown>:0: Test Case '-[JSONTestTests.JSONTestTests testJSONBinding]' measured [Time, seconds] average: 0.804, relative standard deviation: 5.592%, values: [0.835687, 0.814827, 0.819685, 0.841900, 0.764961, 0.845202, 0.691442, 0.779255, 0.818213, 0.830698], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
<unknown>:0: Test Case '-[JSONTestTests.JSONTestTests testJSONSubscript]' measured [Time, seconds] average: 4.247, relative standard deviation: 3.496%, values: [4.019640, 4.004123, 4.146146, 4.194535, 4.487171, 4.300971, 4.310613, 4.408405, 4.318354, 4.279362], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
<unknown>:0: Test Case '-[JSONTestTests.JSONTestTests testNativeBinding]' measured [Time, seconds] average: 0.223, relative standard deviation: 2.773%, values: [0.221099, 0.227395, 0.218860, 0.225989, 0.227128, 0.222370, 0.229956, 0.214535, 0.210818, 0.229868], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
<unknown>:0: Test Case '-[JSONTestTests.JSONTestTests testNativeSubscript]' measured [Time, seconds] average: 0.362, relative standard deviation: 17.528%, values: [0.346285, 0.316185, 0.333650, 0.339416, 0.330243, 0.354034, 0.378730, 0.269519, 0.486904, 0.467607], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
顺便说一句,如果我是你,我会这样做:
if let hours = orgJson[0]?["OpeningHours"] as? NSDictionary {
let openDescription = hours["OpeningHoursGenericExceptions"] as? String ?? ""
let monOpen = hours["Monday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let monClose = hours["Monday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
let tueOpen = hours["Tuesday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let tueClose = hours["Tuesday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
let wedOpen = hours["Wednesday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let wedClose = hours["Wednesday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
let thuOpen = hours["Thursday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let thuClose = hours["Thursday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
let friOpen = hours["Friday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let friClose = hours["Friday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
let satOpen = hours["Saturday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let satClose = hours["Saturday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
let sunOpen = hours["Sunday"]?["From"]??.componentsSeparatedByString?(":") as? [String] ?? []
let sunClose = hours["Sunday"]?["To"]??.componentsSeparatedByString?(":") as? [String] ?? []
// ...
}
它相当快速、安全且不那么复杂。
为什么 SwiftyJSON 很慢?
在访问 subscript
时,SwiftyJSON 会:
String
self[key:sub]
下标NSDictionary
NSDictionary
JSON
对象返回
也许编译器优化了一些步骤,但是“比native慢”有些不可避免:)
关于ios - SwiftyJSON 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29252158/
我有:- var myArray:Array = array 我想要:- var myJSON:JSON = myArray as? JSON 但我收到警告:-从“数组”转换为无关类型“JSON”总是
真的很麻烦。我在 App Store 上有几个应用程序,其中一些使用 SwiftyJSON,但后一个很奇怪。我正在尝试将其上传到 App Store,但出现了这个奇怪的错误。 No suitable
从我之前的问题中你可以看出,我在解析 JSON 数据时遇到了很多麻烦。经过几天的头痛之后,我认为最好的方法仍然是使用 alamofire/swiftyjson。我还发现了 alamofire-swif
当我将我的 SwiftyJSON 文件添加到我的项目时,它有大约 45 个错误,其中大约 44 个将 as 更改为 as!。现在我被困在第 238 行的最后一个,然后我就可以走了。错误说: 无法将“A
我一直在使用模拟器来测试我的应用程序。今天我决定在模拟器中使用其他设备对其进行测试,令我惊讶的是它在某些设备上启动时崩溃,而在其他设备上却运行良好 我的应用构建运行于: iPad 空气 可调整大小的
这是问题的续集:dyld: Library not loaded: @rpath/SwiftyJSON.framework/SwiftyJSON 从头开始重建项目后(新项目、新 podfile、新文件
我试图弄清楚 SwiftyJSON 但我遇到了问题 下面显示的代码工作正常 import UIKit class ViewController: UIViewController {
我有对象 {"FIELD NAME":{ "SUBNAME":{ "FIELD_ONE":"DATA", "FIELD_TWO":"
我正在使用 SwiftyJSON,这很棒。但是,我在将 JSON(data:) 结果存储在我的 viewController 中时遇到问题。 SwiftyJSON 的标准使用效果很好。 let jso
我一直在学习 SWIFT 应用程序中的 JSON 解析。到目前为止,我一直在使用一个简单的免费 API,没有遇到任何问题,应用程序按设计运行。但现在我正在调用一个 API,该 API 返回一个数组内的
SwiftyJSON.swift 文件给我一个错误“NSNumber”对协议(protocol)“Comparable”的冗余一致性 extension NSNumber: Swift.Compara
我正在使用 Alamofire 从 API 获取数据。我正在使用 SwiftyJSON 来解析 JSON 响应。目前,我正在迭代产品数据,并希望提取产品详细信息并将其显示在 Collection Vi
我试图从我的测试目标包中读取一个文件,将该文件转换为 NSData,并将所述数据传递给我的类,然后类使用它来实例化 SwiftyJSON 实例。似乎我的数据生成正确,但是我的 SwiftyJSON 实
我尝试使用 SwiftyJSON 解析以下 JSON 结构,但我无法弄清楚如何循环遍历“sections”节点。我当前的代码返回每个部分的第一个部分节点,但我无法访问“Section 1b/Secti
我有一个由 .rawString() 转换的 JSON 对象的 NSArray,保存在 NSUserDefaults 上。 这是 lanesToReturn NSArray 的打印结果: ( "{\n
我有一个字典数组: var dictArray = [[String:String]]() 我附加了一些字典并将 dictArray 转换为 JSON: let json = JSON(dictArr
如何在不循环的情况下从一个数组到下一个数组? JSON:/image/34BtT.png 我尝试这样做 guard let items = self.json["data"]["reels_media
我有一个包含一些变量的类,我想通过使用 SwiftyJSON 解析 JSON 输出来创建此类的对象数组(我是 SwiftyJSON 的新手,因此在创建时遇到了一些问题一)并使用表格显示它们 谁能帮我创
我尝试用这种结构创建 JSON: var json: JSON = [ "params": [ "token": Utilities.token,
键“children”中包含的数组包含 100 项。 有没有办法告诉 SwiftyJSON 去抓取一个随机索引?我尝试创建一个随机数 var random = arc4random_uniform(2
我是一名优秀的程序员,十分优秀!