How to write an efficient Android code

Source: Internet
Author: User

Time is a valuable thing, writing Android code as much as possible to write efficient Android code can save a lot of time, the following to organize how to improve efficiency, writing efficient Android code! Refer to the Getting Started tutorial for Android app development .

Here are two basic principles for how to judge a system's irrational:
One, do not do not need to do things.
Second, as far as possible to save the use of memory.

The following are some of the most common optimization recommendations:
1. Avoid creating objects (object) as much as possible
Because object creation is not without cost, if you are allocating an object in a loop of a user interface, you have to force a memory recycle, which makes the user experience slightly "hiccup".
When extracting a string from the original input data, try to return a substring from the original string instead of creating one copy.

2, using its own method (use Native Methods)
When dealing with strings, do not hesitate to use as many of the methods of the object as possible, such as String.IndexOf (), String.LastIndexOf (). Because these methods are implemented by using C + +, it is 10-100 times faster than doing the same thing in a Java loop.

3, the use of tools to detect the code you end codes
Android code end, the development of Android APP, with security tools to detect the vulnerability of the code, a minute to finish, do not delay your code next batch of code. The most important thing is that it will also be based on your vulnerability to propose a solution, save you go to one code after another BA la!

4. Buffer attribute Call Cache Field Lookups
Accessing object properties is much slower than accessing local variables. You should not write your code like this:
1 for (int i = 0; i < This.mcount; i++)
2 Dumpitem (This.mitems);
But it should be written like this:
1 int count = This.mcount;
2 item[] items = this.mitems;
3 for (int i = 0; i < count; i++)
4 Dumpitems (items);

(We directly use "this" to indicate that these are its member variables)


5. Declaring final constants
We can take a look at the declaration at the top of the following class:
1 static int intval = 42;
2 static String Strval = "Hello, world!";
When a class is first used, the compiler invokes a class initialization method that Intval 42 into the variable and extracts a reference for Strval in the class file string constant table, which is called directly when the values are referenced later.
We can use the keyword "final" to improve the code:
1 static final int intval = 42;
2 static final String Strval = "Hello, world!";
This class will not invoke the class initialization method, because these constants are directly written to the class file static property initialization, which is handled directly by the virtual machine. Code access Intval will use an integer type of 42, and access Strval will replace a property call with a relatively saved "string constant".
Declaring a class or method as "final" does not result in any benefit of execution, it can be handled with some optimization. For example, if the compiler knows that a get method cannot be overridden by a class, it sets the function to inline.

6. Avoiding the use of floating-point types
Embedded processors often do not support floating-point processing, so all "float" and "double" operations are done by software, and some basic floating-point numbers take milliseconds.
In the same vein, even integers, some chips have only multiplication and no division. In these cases, the Division and modulo operations of integers are implemented by software.

7. Avoid enumerating types avoid Enums
The enumeration type is very useful, when considering the size and speed, it will be very expensive, for example:
1 public class Foo {
2 public enum Shrubbery {GROUND, crawling, hanging}
3}
This translates into a 900-byte class file (Foo$shrubbery.class). For the first time, the initialization of the class is to invoke the method to describe each of the enumerated items, and each object has its own static space, which is stored in an array (a static array called "$VALUE"). That's a lot of code and data, just for three integer values.
Shrubbery shrub = shrubbery.ground;
This causes a call to a static property, and if ground is a static final variable, the compiler will nest it as a constant in the code.

Learn more about Android development at e Mentor

How to write an efficient Android code

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.