【问题标题】:is there a way to measure Swift's performance without building UI on iPhone with Xcode 11? [closed]有没有一种方法可以在不使用 Xcode 11 在 iPhone 上构建 UI 的情况下测量 Swift 的性能? [关闭]
【发布时间】:2020-03-12 20:43:17
【问题描述】:

我需要测量不同 swift 库的性能,而无需在 iPhone 上编写 UI。有许多 Swift/UI 示例通常本质上是多线程的。因此,如果可能的话,我想测量的是没有 gui 开销的原始性能。例如,我们可以在调试器中将输出打印到 Xcode 11 控制台窗口中。能否请您指出我在哪里可以找到这样的示例代码?非常感谢您的帮助。

【问题讨论】:

  • 构建一个 macOS 命令行工具作为测量的包装器。
  • macOS 命令行工具会在 iPhone 上运行吗?
  • 不,但是由于您没有使用任何 UI 并且只是比较原始性能,所以这无关紧要。如果您需要在 iPhone 上运行,则必须构建一个最小的应用程序包装器,例如来自“单页应用”模板。
  • 如果我使用单页应用程序选项,我需要使用什么功能来打印/重定向到 Xcode 控制台?
  • NSLog() 或 print() 可以正常工作

标签: ios swift xcode


【解决方案1】:

这是一个如何测量数字总和的时间的示例

let numbersArray = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6]

func showBenchmarkTime(title:String, operation:()->()) {

    let startTime = CFAbsoluteTimeGetCurrent()

    operation()

    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime

    print("Time elapsed for \(title): \(timeElapsed) s.")
}

showBenchmarkTime(title: "sum an array of numbers") {

    print(numbersArray.reduce(0, +))
}

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多