Efficiency of Android development code

Source: Internet
Author: User

In fact, not only android mobile phone development, but all software development should pay attention to code efficiency issues! In general, the memory should be used as little as possible to implement the same function, and the minimum command is worth the attention of a good developer! We will discuss this issue from two points:

(1)Data Type Selection

First, we will discuss the selection of basic data types. For a 32-bit ARM, let's look at the assembly code of two 16-bit numbers:

201716d0

16d0: e0810000 add r0, r1, r0

16d4: e6bf0070 sxth r0, r0

16d8: e12fff1e bx 1r

It can be seen that three commands are required to add 16-bit data. Next, let's take a look at the Assembly commands to be added to the data of two double types:

00001728

1728: ec1_b6 vmov d6, r0, r1

172c: ec432b17 vmov d7, r2, r3

1730: ee0000b07 faddd d6, d6, d7

1734: ec510f8 vmov r0, r1, d6

1738: e12fff1e bx 1r

It can be concluded that the efficiency of different data types is totally different!

In fact, it is not accurate to use small data types with High Efficiency! For 32 processors, the 32 data type is the most efficient! However, in general, there are two principles for data type selection:

(1) try to select one with a small data type

(2) try to select the same data type for Operation: arithmetic/comparison. That is, try to prevent unnecessary forced conversions.

Generally, the efficiency of sorting 16-bit data is much faster than that of 32-bit 64-bit data.

(2)Cache

Then let's talk about cache. Objectively speaking, the more commands, the larger the data volume, the lower the efficiency! To this end, the processor has its own way, that is, to use the cache reasonably! Currently, the processor cache technology is common, and there are usually multi-level caches! Cache Features: High Efficiency! High cost! Therefore, the cache cannot be used without restrictions! The purpose of caching is to ensure the efficiency of the processor, and save frequently-used data/commands or the data and commands he thinks will be used to meet the needs of the processor!

Therefore, to ensure efficiency, we need to maximize the data and instructions that the processor needs in the cache. Under normal circumstances, the frequently used data may easily exist in the cache, or adjacent Data Types of the same, and arrays, may be read to the cache in advance so that the processor can quickly find it! Therefore, it is more efficient to access data in an array in sequence than to access data randomly!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.