Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/65912772/xcode-instrument-time-profiler-total-time-estimated-wrong-when-using-third-party

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...