/// 测量执行时间
/// - Parameters:
/// - closure: 需要被测量的方法
/// - methodName: 需要被测量的方法,用于打印
func measureExecutionTime(methodName: String, closure: () -> Void) {
let startTime = DispatchTime.now()
closure()
let endTime = DispatchTime.now()
let elapsedTime = endTime.uptimeNanoseconds - startTime.uptimeNanoseconds
let executionTime = Double(elapsedTime) / 1_000_000_000 // 将纳秒转换为秒
print("\(methodName) execution time: \(executionTime) seconds")
}
网友评论