macos:High-performance
出自UXGuide.net
请帮助我们修订和更新本文的中文部分
本文的中文部分虽然取自官方最新中文版本,但与英文原版相比已落后了一年多,可能无法反映英文版本中最近的修改,且文中笔误、前后译法矛盾、未翻译完全的片断较多,请您帮助一起修订完善。详情参见中文版修订完善计划。
高性能
High Performance
Performance is the perceived measure of how fast or efficient your software is and it is critical to the success of all software. If your software seems slow, users may be less inclined to buy it. Even software that uses the most optimal algorithms may seem slow if it spends more time processing data than responding to the user.
性能能够衡量您的软件是否运行的够快,是所有软件成功的至关重要的因素。如果您的软件太慢,用户很难对购买您的产品感兴趣。如果软件花费更多的时间来处理数据而不是响应用户的操作的话,即使它采用了最优的算法,它也可能让用户感觉很慢。
Developers who have experience programming on other platforms (including Mac OS 9) should take the time to learn about the factors that influence performance on Mac OS X. Understanding these factors can help you make better choices in your design and implementation. For an overview of performance factors and links to information on how to identify problems, see Performance Overview.
即使您在别的平台上(包括 Mac OS 9)上有丰富的编程经验,您也应该花点时间来了解 Mac OS X 影响性能的因素,这样能够帮助您在设计和实现时做出更好的选择。关于影响性能的因素以及怎样定位问题的更多信息,请参考性能概述。
Here are some performance-related guidelines to keep in mind:
下面是和性能相关的一些准则:
- Use metrics to identify performance problems. Never try to tune the performance of your software based on assumptions. Use the Apple-provided tools, such as Shark, to gather data about where your software is performing poorly. Use that data to isolate problems and fix them. You might also want to create your own tools to gather metrics that are specific to your software.
- 使用测试数据来确定性能问题。永远不要基于假设来调整您的产品的性能。使用 Apple 提供的工具,如 Shark,来收集产品的性能数据,并使用这些数据来分析和解决问题。您也可以创建您自己的测试工具来收集您产品特定的数据。
- Avoid waiting until the end of your development cycle to do performance tuning. Include specific goals in your product requirements. Gather baseline metrics early and continue gathering metrics during development to measure progress against those goals. If you see performance degrading, take immediate corrective actions to fix the problem.
- 避免在产品开发周期的后期进行性能调整。将性能目标加入您的产品需求中,在早期就开始收集性能数据来测试是否达到预定的目标,并在开发过程中一直坚持这一行为。如果您发现性能下降,那么立即采取动作来修正它。
- Choose modern APIs over legacy APIs. Modern interfaces are built for Mac OS X and take advantage of the latest technology and design information to deliver the best possible performance.
- 使用新的 API 而不是早期的 API。新的 API 针对 Mac OS X 采用了最新的技术和设计,从而提供了良好的性能。
- Choose appropriate technologies for the task at hand. For example, Cocoa distributed objects may be easier to use, but if your program needs maximum performance over the network, CFNetwork or BSD sockets may be a better choice. See CFNetwork Programming Guide for more information.
- 选择合适的技术。例如,基于 Cocoa 的对象可能易于使用,但如果您的程序对网络操作的性能有很高的要求,CFNetwork 或者 BSD 套接字 API 可能是更好的选择。更多信息请参考 CFNetwork 编程指南。
- Use threads to improve the responsiveness of your code. Taking advantage of the parallelism offered by threads can offer significant performance advantages, especially on multiprocessor systems. Technical Note TN2028, “Threading Architectures” includes an excellent overview of threading architectures. See “Threads” for more information.
- Avoid polling the system for information. Polling wastes a significant amount of CPU time and is unnecessary with most modern APIs. Most modern APIs provide asynchronous callback mechanisms to notify you when conditions change or requested data is available. Use these mechanisms instead.
- 避免使用轮询来获取信息。轮询机制浪费了大量的 CPU 周期,而且是完全没有必要的,因为现在的 API 可以提供异步的回调机制来让您在条件变化或者需求的数据可用时获得通知。
- Eliminate any unnecessary I/O operations. Accessing a hard drive or optical drive is one of the slowest operations you can perform on any computer. Minimizing these operations can improve performance tremendously. See File-System Performance Guidelines for more information.
- 减少不必要的 I/O 操作。访问硬盘或者光驱在任何计算机上都是最慢的操作之一。减少这些操作能够显著的提高程序的性能。更多信息请参考文件系统性能指南。
- Optimize your memory usage to take advantage of the Mac OS X virtual memory system. Understanding how virtual memory works in Mac OS X can help you make more efficient use of memory. See Memory Usage Performance Guidelines for information about the Mac OS X virtual memory system.
- 优化内存使用以更好的利用 Mac OS X 的虚拟内存系统。了解 Mac OS X 的虚拟内存工作机制能够帮助您更有效的利用内存。更多 Mac OS X 虚拟内存机制的信息请参考内存使用的性能指南。
- Avoid loading resources until they are actually needed by your software. Loading resources early wastes memory and can trigger paging before the resource is ever used. Wait until you need the resource and then cache it as appropriate.
- 避免加载不必要的资源。过早的加载资源文件会导致内存的浪费,并且可能触发内存换页。直到需要的时候才加载资源文件,然后进行合适的缓存。
- Use the Mach-O executable format. Mach-O is the native executable format of Mac OS X and is used by all system frameworks. Using the legacy Code Fragment Manager (CFM) executable format requires additional bridging code between your code and system libraries. This bridging incurs a small performance penalty that can add up over time.
- 执行文件使用 Mach-O 格式。Mach-O 是原生的执行文件格式,并且被 Mac OS X 上所有系统框架使用。如果您使用早期的 CFM(Code Fragment Manager)格式,则需要在您的执行文件和系统库之间进行桥接,这种桥接会导致额外的一点性能开销。