Xcode instrument time profiler total time estimated wrong when using third party pods libraries

Viewed 91

I am using Xcode instrument time profiler to measure the performance of my app. At the same time, I am also using CFAbsoluteTimeGetCurrent function in my code to print the time I used. However, I have found that sometimes the execution time of a function measured by these two methods are the same, but sometimes there is a big difference.

Here is an example

import UIKit
import ZippyJSON

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        let encoder = JSONEncoder()
        var students = [Student]()
        var scores = [Int]()
        for _ in 0...100 {
            scores.append(99984)
        }
        for _ in 0...100 {
            students.append(Student(name: "StudentName", age: 99, scores: scores))
        }
        var dataList = [Data]()
        for _ in 0...100 {
            let data = try! encoder.encode(students)
            dataList.append(data)
        }
        testJson(dataList: dataList)
    }
}

func testJson(dataList: [Data]) {
    let t1 = CFAbsoluteTimeGetCurrent()
    let decoder = JSONDecoder()
    var results = [[Student]]()
    for data in dataList {
        let r1 = CFAbsoluteTimeGetCurrent()
        let result = try! decoder.decode([Student].self, from: data)
        let r2 = CFAbsoluteTimeGetCurrent()
        results.append(result)
        print("single time \(r2 - r1)")
    }
    let t2 = CFAbsoluteTimeGetCurrent()
    print("total time \(t2 - t1)")
}

At this time, I am using Apple's JSONDecoder to decode the data created by me. The printing of the program is

total time 1.428820013999939

And it corresponds to what Time Profiler produce

Result of Time Profiler under JSONDecoder

However, if I change to use a third party library ZippyJSONDecoder to decode the data

func testJson(dataList: [Data]) {
    let t1 = CFAbsoluteTimeGetCurrent()
    let decoder = ZippyJSONDecoder()
    var results = [[Student]]()
    for data in dataList {
        let r1 = CFAbsoluteTimeGetCurrent()
        let result = try! decoder.decode([Student].self, from: data)
        let r2 = CFAbsoluteTimeGetCurrent()
        results.append(result)
        print("single time \(r2 - r1)")
    }
    let t2 = CFAbsoluteTimeGetCurrent()
    print("total time \(t2 - t1)")
}

The printing of the program is

total time 3.199104905128479

But the Time Profile produces this result

Result of Time Profiler under ZippyJSONDecoder

It is only 325ms, totally different from what produces by the measurement of CFAbsoluteTimeGetCurrent

I am wondering why this problem occured? Which result is the correct one?

0 Answers
Related