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 print("update group topic item consume time: current=\(currentElapsedTimeStr), total=\(totalElapsedTimeStr)") |
最后用了:
class CalculateElapsedTime: NSObject { let uniqueId:String var startTime:NSDate var elapsedTimeInSec:Double { self.startTime = NSDate() func start() -> NSDate { func reset(){ } func calcTimeStart(uniqueId:String){ gLog.debug("\(uniqueId)") let calcTime = CalculateElapsedTime() func calcTimeEnd(uniqueId:String){ } } 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中调试代码时间占用消耗