在网上那里能接单做网站,江苏盐城有做淘宝网站的吗,成立公司需要哪些人员,wordpress中文免费企业模板下载引言
软件的性能是评价一个软件质量的重要指标#xff0c;尤其在今天这个时代#xff0c;性能已成为大型项目不可或缺的考虑因素之一。对于用户量极大的软件#xff0c;如网银系统、在线购物商城等#xff0c;更是必须保证其高效稳定的性能。在这种背景下#xff0c;优化…引言
软件的性能是评价一个软件质量的重要指标尤其在今天这个时代性能已成为大型项目不可或缺的考虑因素之一。对于用户量极大的软件如网银系统、在线购物商城等更是必须保证其高效稳定的性能。在这种背景下优化项目的编译速度就显得尤为重要。本文将介绍如何使用 Swift 代码优化项目编译速度。
找出编译耗时过长的文件
要优化项目的编译速度首先需要把耗时过长的文件找出来然后进行重点优化。这里会用到 Xcode build 的两个OTHER_SWIFT_FLAGS
-Xfrontend如果编译或类型检查时耗时多长则在 Xcode 中输出警告。-debug-time-function-bodies输出每个函数的编译时长。
添加这些 flag 的方法为
选中 Target选中 Build Settings搜索 “Other Swift Flags”添加 -Xfrontend -debug-time-function-bodies
基于这两个 flag有 3 个方法可以找到耗时过长的文件
方法一使用克魔助手
克魔助手是一款专为苹果手机 iOS 应用开发设计的辅助工具提供了丰富的性能监控功能帮助开发者优化应用的性能。以下是使用克魔助手找到编译耗时过长的文件的步骤 下载克魔助手用户可以前往克魔助手工具官网进行免费下载该工具提供绿色软件版本无需其他安装流程。下载后解压即可开始使用。下载地址是 https://www.keymob.com 。 注册和登录为了使用克魔助手工具用户需要在电脑上安装并登录该工具。在登录后用户可以继续其他操作流程。克魔开发助手提供了简单的登录码获取流程确保用户能够方便地使用该工具。 3.选择文件管理界面并开始监听双击克魔开发助手.exe 启动克魔助手后点击右上角的登录按钮输入邮箱后没登录码的点击获取登录码有的可以直接输入登录码登录成功后选择文件管理界面。然后自定义选择列并点击 “开始监听” 来查看应用程序的性能情况。
方法二使用 Xcode 控制台
在 Xcode 的控制台中输入以下命令即可输出时间最久的前 10 个文件
find . -type f -name *.swift -exec \sh -c echo {} swiftc -v -c {} 21 | awk /^.{10}[ ][0-9\.]ms/{print \$0} | sort -rn | head -10 \;方法三使用 Xcode 插件
使用 Xcode 插件 SwiftLint 可以自动输出编译耗时最长的文件。安装方法如下
打开 Terminal输入以下命令
brew install swiftlint在 Xcode 中打开 Preferences快捷键 Command ,选择 Text Editing然后选择 Code completion。在 Code completion 下面的 Fuzzy matching 一栏中勾选 Show snippets using: 和 Swift。然后再选择 Edit All-in-One Snippet…。进入 Snippet 编辑页面复制以下代码并保存
// MARK: - Performance Metrics// Prints the compile time of each function or method in this file.
// Add to the OTHER_SWIFT_FLAGS build setting for the target.
// -Xfrontend -debug-time-function-bodies
//
// For more information about how to use debug-time-function-bodies see:
// - https://pspdfkit.com/blog/2018/how-to-reduce-swift-compile-times/
// - https://github.com/apple/swift/pull/13132#issuecomment-312358040
// - WWDC 2018 session 404 Optimizing Swift build times
// - https://youtu.be/nJwVabxL5Gw?t11m16s
//
// If you add this to a Swift file, it will print the compile time of each
// method in this file to the console on build.
//
// Usage:
// In Xcode 9.x and later:
// 1. Open your project
// 2. Go to the Build Phases tab of your apps target
// 3. Click the button in the top left and select New Run Script Phase
// 4. Add the following code to the script area below the shell:
// ${PODS_ROOT}/SwiftLint/swiftlint --no-cache --config ${PODS_ROOT}/SwiftLint/.swiftlint.yml
// ${PROJECT_DIR}/Scripts/perf.swift
// Note: You may need to customise the paths to suit your project structure.
// 5. Drag the new run script phase to be just before the Compile Swift Sources
// phase in the list.
// 6. Rebuild your project (Command-B)
// 7. The compile time for each function in each source file will now be logged
// to the console, sorted by longest compile time first.import Foundationlet numBitsInByte 8extension Double {func rounded(toPlaces places: Int) - Double {let divisor pow(10.0, Double(places))return (self * divisor).rounded() / divisor}
}extension String {func padding(toLength length: Int, withPad padCharacter: Character) - String {let padding String(repeatElement(padCharacter, count: max(0, length - count)))return self padding}
}func readableTime(_ time: Double) - String {if time 0.001 {return 0.001ms} else if time 1 {return \(time.rounded(toPlaces: 3))ms} else {var timeStr \(time.rounded(toPlaces: 2))sif time 60 {let minutes Int(time / 60)let seconds time.truncatingRemainder(dividingBy: 60)timeStr \(minutes)m \(seconds.rounded(toPlaces: 2))s}return timeStr.padding(toLength: 10, withPad: )}
}func measureA(name: String, _ f: () - A) - A {let start DispatchTime.now()let result f()let end DispatchTime.now()let time Double(end.uptimeNanoseconds - start.uptimeNanoseconds) / Double(numBitsInByte * 1000000)print(\(readableTime(time)): \(name))return result
}
在 Xcode 的 Build Phases 中添加一个 Run Script将以下代码复制到 Run Script 中
${PODS_ROOT}/SwiftLint/swiftlint --no-cache --config ${PODS_ROOT}/SwiftLint/.swiftlint.yml
${SRCROOT}/Scripts/perf.swift总结
优化项目的编译速度是一个不断迭代的过程需要不断地寻找和解决问题。本文介绍了如何使用 Swift 代码来找出编译耗时过长的文件并介绍了三种方法使用克魔助手、使用 Xcode 控制台和使用 Xcode 插件 SwiftLint。希望这些方法能够帮助读者优化项目的编译速度。
参考资料
克魔助手官网