dannliu
(Danny Liu)
November 15, 2019, 4:02pm
1
How I test the performance:
Create an application with Xcode.
Copy the JSONSerialization.swift into the project and change the name to OPJSONSerialization to avoid name collision
Test with below code and run on iPhone X, iOS 13.2
:
let path = Bundle.main.path(forResource: "test", ofType: "json")
if let data = FileManager.default.contents(atPath: path!) {
print(data.count)
let st = Date().timeIntervalSince1970
_ = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
let et = Date().timeIntervalSince1970
testResultLabel.text = "\(et - st)"
let st2 = Date().timeIntervalSince1970
_ = try? OPJSONSerialization.jsonObject(with: data, options: .mutableLeaves)
let et2 = Date().timeIntervalSince1970
testResultLabel2.text = "\(et2 - st2)"
}
The file data count is 527953
.
The costs on average are 0.014 vs. 0.38. The performance is 27 times worse.
Why the performance is so poor for the opensource version
allevato
(Tony Allevato)
November 15, 2019, 4:26pm
2
First, what build settings are you using in your project? Since you're comparing a system framework (which is always optimized) to code compiled into your app, are you making sure that your code is being compiled with similar optimizations enabled?
The results are still likely to be different since the open-source version is written in Swift while the system framework is C/Objective-C, but let's make sure you're comparing apples to apples first.
2 Likes
dannliu
(Danny Liu)
November 15, 2019, 4:42pm
3
allevato:
First, what build settings are you using in your project? Since you're comparing a system framework (which is always optimized) to code compiled into your app, are you making sure that your code is being compiled with similar optimizations enabled?
The results are still likely to be different since the open-source version is written in Swift while the system framework is C/Objective-C, but let's make sure you're comparing apples to apples first.
Hey Tony, thanks for your reminder. Set the optimization level to -O and tested again, the result looks good now:
0.011 vs 0.051 on average.