swift - 测量调用方法 "didUpdateValueFor"之间的时间

标签 swift methods time timer

我想测量一个函数被调用到下次调用之间的时间。

目前我有一个带蓝牙的应用程序。 Arduino 将字节数组发送到应用程序以显示在表格 View 中。我有两个蓝牙模块,我想测量时间以查看速度差异。

当应用程序收到字节数组时,会调用函数 didUpdateValueFor。并将接收到的数据发送到另一个函数。

我的问题是,我使用的所有计时器和计数器都只能用于获取函数的整个执行时间。

我想要的只是测量接收到第一个字节数组到接收到下一个打包之间的时间。

func printTimeElapsedWhenRunningCode(title:String, operation:()->()) {
    let startTime = CFAbsoluteTimeGetCurrent()
    operation()
    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
    print("Time elapsed for \(title): \(timeElapsed) s.")
}

使用我上面发布的函数,我确信效果很好。但我希望它测量每次调用 didUpdateValueFor 之间的时间。

所以:

Byte array receievd - didUpdateValueFor is called
Timer starts
New array received - didUpdateValueFor is called
Timebetween is: "...ms"

最佳答案

我确实找到了答案。不是最佳的,但目前有效:

    var start = Date()

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    var diff = Date().timeIntervalSince(start)
    print("differanse is: \(diff) seconds")
    start = Date()
    print("Starting at 0")
    diff = 0
    print("diff = 0")

足够好

关于swift - 测量调用方法 "didUpdateValueFor"之间的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587881/

相关文章:

ios - 创建一个函数以在Swift中分隔输入数组和输出数组分号

ios - 隐藏 View Controller 的导航栏

JavaScript 回调令人困惑的行为

java - 按一天中的时间过滤日期(初学者)

python - 在 Python 中访问 datetime.now() 值

Javascript 日期时间字符串到日期对象

swift - CLLocationManager.location 在 10.10 中为 nil,但在 10.11 中有效

ios - 带过滤器的 AudioKit 导出文件

ruby - 如何编写我自己的 max 函数

python - 创建类后在方法上设置属性会引发 "' instancemethod' object has no attribute“但属性显然存在