Efficiency issues with Android development code

Source: Internet
Author: User

In fact, not only Android phone development, all software development should pay attention to the problem of code efficiency! In general, to achieve the same functionality as little as possible using memory, the least instruction is a good developer need to pay attention to! Here we discuss this question from two points:

(i) data type selection

First, discuss the selection of basic data types. For a 32-bit arm, let's take a look at the assembly code with two 16-bit numbers added:

000016d0 <add_16_16>

                                                                                         16d0:e0810000       add  r0, R1, R0

                                                                                         16d4:e6bf0070         Sxth r0,r0

16D8:E12FFF1E BX 1r

You can see that 16-bit data addition requires three instructions. Let's take a look at two double data to add the assembly instructions:

                                                                                 00001728 <add_double_double>

                                                                                             1728: Ec410b16      vmov  d6, r0, R1

                                                                                             172c: Ec432b17       Vmov D7,R2,R3

                                                                                             1730: ee366b07       faddd   d6, D6,d7

                                                                                             1734: Ec510b16      vmov  r0, R1, D6

1738:E12FFF1E BX 1r

It is possible to conclude that, in fact, the use of different data types is not exactly the same as their efficiency!

In fact, it's not accurate to say that using a smaller data type is highly efficient! For 32 of processors, its 32 data types are the highest in efficiency! In general, however, two principles are followed in data type selection:

(1) Try to choose a small data type

(2) Try to choose the same type of data type to operate: arithmetic/comparison, etc. That is, try to prevent unnecessary casts.

In general, 16-bit data sequencing is much faster than 32-bit 64-bit efficiency.

(ii) cache

And then we're going to say cache. Objectively speaking: The more instructions, the greater the amount of data that is being manipulated, the less efficient it is! For this reason, the processor has its own way, it is reasonable to use the cache! Processor cache technology is now common and often has multiple caches! Features of the cache: high efficiency! High cost! So in general the cache cannot be used indefinitely! The purpose of the cache is to ensure the efficiency of the processor, the use of common data/instructions or the data and instructions he thinks will be used to save to meet the processor time can be changed quickly find!

        so, to ensure efficiency, we need to maximize the processor's ability to find the data and instructions he needs in the cache. In general, those often used in the cache is easy to exist, or adjacent to the same data type, the inevitable array, etc., are likely to be read in advance to the cache so that the processor can be changed quickly find! Therefore, in general, sequential access to the data in the array is more efficient than random access data!                          ,         &NB Sp                          ,         &NB Sp                            & nbsp;                                                                                                                         

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.