最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

[已解决]swift中调试代码时间占用消耗

Swift crifan 385浏览 0评论

swift中,需要找网络请求相关的代码,想要分析其时间占用情况

swift calculate elapse time

Swift type for calculating elapsed time

Measure elapsed time in swift – Stack Overflow

swift calculate spend time

swift calculate time for timers running in background – Stack Overflow

ios – How to log a method’s execution time exactly in milliseconds? – Stack Overflow

先是用了:

    var totalUpdateGroupTimeInSec:NSTimeInterval = 0
                let beginTime = NSDate()
                //do something
               
                let endTime = NSDate()
                let currentElapsedTime = endTime.timeIntervalSinceDate(beginTime)
                let currentElapsedTimeFloat = Float(currentElapsedTime)
                let currentElapsedTimeStr = String(format: "%.4f", currentElapsedTimeFloat)

                self.totalUpdateGroupTimeInSec += currentElapsedTime
                let totalElapsedTimeFloat = Float(self.totalUpdateGroupTimeInSec)
                let totalElapsedTimeStr = String(format: "%.4f", totalElapsedTimeFloat)

                print("update group topic item consume time: current=\(currentElapsedTimeStr), total=\(totalElapsedTimeStr)")

最后用了:

class CalculateElapsedTime: NSObject {
    let uniqueId:String

    var startTime:NSDate
    var endTime:NSDate

    var elapsedTimeInSec:Double {
        let diffTimeInSec:Double = self.endTime.timeIntervalSinceDate(self.startTime)
        return diffTimeInSec
    }
   
    init(uniqueId:String = ""){
        self.uniqueId = uniqueId

        self.startTime = NSDate()
        self.endTime = self.startTime
       
//        start()
    }

    func start() -> NSDate {
        self.reset()
       
        return self.startTime
    }
   
    func stop() -> Double {
        self.endTime = NSDate()
       
        return elapsedTimeInSec
    }

    func reset(){
        self.startTime = NSDate()
        self.endTime = self.startTime
    }

}
func calcTimeStart(uniqueId:String){
    gLog.debug("\(uniqueId)")

    let calcTime = CalculateElapsedTime()
    gCalcTime[uniqueId] = calcTime
}

func calcTimeEnd(uniqueId:String){
    if gCalcTime.keys.contains(uniqueId) {
        let calcTime = gCalcTime[uniqueId]!
        let elapsedTime = calcTime.stop()
        gLog.debug("\(uniqueId) elapsedTime=\(String(format: "%.4f", elapsedTime))")
        calcTime.reset()

    }
}
    func loginWithUserid(){
        calcTimeStart("loginWithUserid")
        getUrlRespJsonDict_async(genAuthUrl, extraParas: extraParas, respJsonDictHandler: getAcessTokenHandler)
    }
   
    func getAcessTokenHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON) {
        calcTimeEnd("loginWithUserid")
    }

即可。

输出:

2016-03-30 21:37:10.287 [Debug] … calcTimeStart > …
2016-03-30 21:37:14.785 [Debug]… elapsedTime=3.1823

转载请注明:在路上 » [已解决]swift中调试代码时间占用消耗

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
18 queries in 0.238 seconds, using 10.37MB memory