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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章