iOS应用程序能效指南----能源要素

Energy Essentials

能源要素

原文:
https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/index.html#//apple_ref/doc/uid/TP40015243-CH3-SW1

1 Energy Efficiency and the User Experience

1 能效和用户体验

A great user experience requires:

  • Great battery life. Users expect all-day battery life on their iOS devices.
  • Awesome speed. iOS is designed to provide great performance during complex operations—and to make your app fly.
  • Responsiveness. Too many resources being consumed at once can result in a laggy interface that’s slow to respond to user input.
  • Cool device. As more apps use more resources, the system works harder and faster, and the physical temperature of a device gradually rises. When this occurs, the system takes steps to cool down to a more acceptable level.

好的用户体验需要:

  • 电池续航时间很长。用户希望他们的设备全天都有点。
  • 惊人的速度。iOS 可以在复杂的操作中提供出色的性能。
  • 快速响应。同时消耗太多资源会导致用户输入的响应变慢。
  • 凉设备。随着使用更多的,设备会升温;系统会采取措施降温。

1.1 iOS Energy-Saving Technologies

1.1 iOS 节能技术

1.1.1 Integrated Hardware and Software

1.1.1 集成硬件和软件

iOS integrates with advanced hardware features such as a power efficient CPU, accelerated graphics, and wireless antennas. Hardware and software work together to deliver an optimized user experience that’s great for battery life.

iOS 设备集成了优秀的软件和硬件

1.1.2 Intelligent App Management

1.1.2 智能应用管理

iOS apps have a life cycle that’s managed by the system. When a user finishes interacting with an app, the app is placed into a background state, where activity is throttled and the app may be suspended. Apps generating high CPU usage for extended periods of time while running in the background may be terminated by the system, if necessary.

iOS 应用的生命周期是被系统管理的。系统会限制应用在后台的活动。

1.1.3 Network Operation Deferral

1.1.3 网络操作延迟

APIs let you designate criteria that indicate when and how often a network operation should be deferred, how long it can be deferred, and under what circumstances. The system uses this information to defer the operation until an energy efficient time.

支持通过 API 控制延迟网络请求。

1.1.4 Task Prioritization

1.1.4 任务优先级

Tasks that affect the user, such as downloading and playing music, take priority over background and discretionary work. Quality of service class APIs allow you to assign priority levels to the work your app performs, giving you fine-grained control over task prioritization.

影响用户的任务,优先级高于后台任务。也有 API 可以精细控制任务优先级。

1.1.5 Developer Tools

1.1.5 开发者工具

Xcode and Instruments help you identify and address energy problems as you develop your app, rather than after those problems are encountered by users.

Xcode 和 Instruments 有助于在开发阶段就发现和解决能源问题。

1.2 Your Obligation as a Developer

1.2 开发者的责任

Even small inefficiencies in apps add up, significantly affecting battery life, performance, and responsiveness. As an app developer, you have an obligation to make sure your app runs as efficiently as possible. Use recommended APIs so the system can make smart decisions about how best to manage your app and the resources it uses. Whenever possible, batch and reduce network operations, and avoid unnecessary updates to the user interface. Power-intensive operations should be under the user’s control. If a user is playing a graphics-heavy game, for example, the user should not be surprised if the activity consumes power. Strive to make your app absolutely idle when it is not responding to user input.

作为应用程序开发人员,您有义务确保您的应用程序尽可能高效地运行。

使用推荐的 API,以便系统能够明智地决定如何最好地管理应用程序及其使用的资源。只要有可能,批处理和减少网络操作,并避免对用户界面进行不必要的更新。功率密集型操作应由用户控制。努力让你的应用程序在没有响应用户输入时完全进入空闲状态。

2 Fundamental Concepts

2 基本概念

There’s no single solution for conserving energy on a device. Numerous technologies and operations influence how energy is used:

在设备上没有单一的节能解决方案。许多技术和操作影响能源的使用:

  • CPU. The CPU is a major consumer of energy. Periods of high CPU use rapidly drain a user’s battery.
  • CPU。CPU 是能源的主要消耗者。应该通过批处理、调度和优先级控制,让 CPU 只在必要时才工作。
  • Device wake。 iOS devices rely on sleep for great battery life. Whenever a device wakes, there is a high overhead cost, as the screen and other resources must be powered up.
  • 设备唤醒。iOS 通过睡眠来延长电池使用时间。设备唤醒时,屏幕和其他资源都必须通电。
  • Networking operations. When networking occurs, components such as cellular radios and Wi-Fi power up and use energy.
  • 网络操作。当网络操作发生时,蜂窝无线电和Wi-Fi等组件将通电并使用能源。
  • Graphics, animations, and video. Every time your app’s content updates on screen, it uses energy to produce those pixels. Animations and videos can be especially taxing.
  • 图形、动画和视频。每次屏幕内容更新时,都要使用能源来产生像素。动画和视频尤其费电。
  • Location. Energy use increases with greater precision and longer location requests.
  • 定位。随着更高的精度和更远的定位要求,能源消耗也在增加。
  • Motion. Continuous unwarranted requests for accelerometer, gyroscope, magnetometer, and other motion data waste energy.
  • 运动。对加速度计、陀螺仪、磁强计和其他运动数据的不断不必要的请求浪费了能量。
  • Bluetooth. High periods of Bluetooth activity can drain the battery of the iOS device and the Bluetooth device.
  • 蓝牙。长时间的蓝牙活动会耗尽iOS设备和蓝牙设备的电池电量。

2.1 Energy and Power

2.1 能量与功率

Energy and power are two separate but related concepts. Power is an instantaneous measurement (watts) of energy required at any given point in time (Figure 2-1). Energy is a measurement of power used (joules) over a period of time (watt hours).

能量与功率是两个独立但相关的概念。功率是对任何给定时间点所需能量的瞬时测量(瓦特)。能量是对一段时间内(瓦时)所用功率的测量(焦耳)。

Figure 2-1 Energy is power over time
Figure 2-1

2.2 CPU Usage and Power Draw

2.2 CPU 使用与功耗

As more CPU is used, more power draw occurs, more energy is used, and the device’s battery drains faster. Power draw varies based on the device, processor, resources, and so on.

越多的 CPU 被使用,越多的功率被消耗,越多的能量被使用,设备的电池消耗越快。功率消耗因设备、处理器、资源等而异。

Table 2-1 Example of idle vs. CPU power draw

Idle 10x greater power draw over sleep
1% CPU use 10% greater power draw over idle
10% CPU use 2x power draw over idle
100% CPU use 10x power draw over idle

2.3 Fixed Cost and Dynamic Cost

2.3 固定开销和动态开销

Tasks your app performs have a dynamic cost—how much energy your app uses by doing actual work. They also have a fixed cost—how much energy is used by bringing the system and various resources up in order for your app to do work, and back down after that work is complete. When lots of sporadic work is occurring, there are dynamic costs and a significant fixed cost too, as resources may never get the chance to reach true idle between the sporadic tasks. This situation results in a lot of energy being used for a relatively small amount of actual work. See Figure 2-2.

应用程序执行的任务有动态开销——使用多少取决于实际的工作。同时也有固定开销——通过提升系统和各种资源以完成工作,并在工作完成后回落。

当大量零星工作发生时,会产生动态开销和大量的固定开销,因为在零星任务之间资源可能永远无法达到真正的空闲状态。这就导致了大量的能量被用于相对较少的实际工作。见图 2-2。

Figure 2-2 Fixed vs. dynamic energy costs
Figure 2-2

2.4 Trading Dynamic Cost for Fixed Cost

2.4 用动态开销替代固定开销

Your app can avoid sporadic work by batching tasks and performing them less frequently. For example, instead of performing a series of sequential tasks on the same thread, distribute those same tasks simultaneously across multiple threads, as shown in Figure 2-3. By batching activity, components can be powered up once and used over a shorter period of time.

可以通过批处理任务或降低它们的执行频率来避免零星工作。例如,不要在同一个线程上执行一系列连续的任务,而是将这些相同的任务同时分布在多个线程上,如图2-3所示。通过批处理活动,组件可以通电一次并在较短的时间内完成使用。

This strategy incurs a greater up-front dynamic cost. In exchange, you get a dramatic reduction in fixed cost, which results in tremendous energy savings over time.

这种策略需要更大的前期动态开销。作为交换,您可以大幅降低固定成本,这会随着时间的推移节省大量的能源。

As you develop your app, think holistically about its behavior, and try to reduce fixed costs wherever possible.

在开发应用程序时,要全面考虑其行为,并尽可能降低固定成本。

Figure 2-3 Use multithreading to trade power for energyFigure 2-3