Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write efficient android code

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to write efficient android code, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Based on Android related devices as embedded devices, we should pay special attention to efficiency when writing App applications, and it is limited by battery power. This leads to many considerations and limited processing capacity of embedded devices, so we are required to write efficient code as much as possible. Here are a number of ways for developers to make their programs run more effectively, and by following these methods, you can maximize the effectiveness of your programs.

Introduction

For resource-intensive systems, there are two basic principles:

Don't do anything unnecessary.

Do not allocate unnecessary memory

All of the following follow these two principles.

1. Avoid creating objects

There is no free object in the world. Although GC sets up a temporary object pool for each thread, which makes it less expensive to create objects, allocating memory is always more expensive than not allocating memory.

If you allocate object memory in the user interface loop, it will trigger periodic garbage collection and users will feel that the interface burps like a meal.

Therefore, unless necessary, try to avoid instances of the best effort object. The following example will help you understand this principle:

When you intercept a string from the data entered by the user, try to use the substring function to get a substring of the original data instead of creating a separate copy of the substring. So you have a new String object that shares a char array with the original data.

If you have a function that returns a String object, and you know exactly that the string will be appended to a StringBuffer, then change the parameters and implementation of this function to append the result directly to the StringBuffer instead of creating a short-lived temporary object.

A more extreme example is to divide a multidimensional array into multiple one-dimensional arrays:

The int array is better than the Integer array, which also sums up the basic fact that two parallel int arrays perform much better than (int,int) object arrays. In the same way, this applies to all basic types of combinations.

If you want to Foo,Bar tuples in a container, try using two separate Foo [] arrays and Bar [] arrays, which must be more efficient than (Foo,Bar) arrays. (there are exceptions when you create an API and let someone else call it.) At this point, you should pay attention to the design of API excuses at the expense of a little speed. Of course, within API, you still need to improve the efficiency of the code as much as possible)

In general, it is to avoid creating short-lived temporary objects. Reducing the creation of objects reduces garbage collection, which in turn reduces the impact on the user experience.

2. Use the local method

When you are dealing with strings, don't hesitate to use special implementations such as String.indexOf () and String.lastIndexOf (). All of these methods are implemented using CCompact +, which is 10 to 100 times faster than the Java loop.

However, it is not necessary to use the local method completely, and it is more expensive to call the local method than to call the interpreted method. So if it can be avoided, local methods should not be used to do some uncomplicated operations.

3. Choose virtual class instead of interface

Suppose you have a HashMap object, and you can declare it as HashMap or Map:

Map myMap1 = new HashMap ()

HashMap myMap2 = new HashMap (); which is better?

Traditionally, Map is better because you can change its concrete implementation class, as long as it inherits from the Map interface. The traditional view is correct for traditional programs, but it is not suitable for embedded systems. Calling a reference to an interface takes twice as long as calling a reference to an entity class. If HashMap is a perfect fit for your program, there is little value in using Map. If there's something you're not sure about, avoid using Map and leave the rest to the refactoring capabilities provided by IDE. (of course, public API is an exception: a good API often sacrifices some performance.)

4. Static method is better than virtual method.

If you do not need to access a member variable of an object, declare the method as static. The virtual method performs faster because it can be called directly without the need for a virtual function table. You can also declare that the call to this function does not change the state of the object.

5. Do not use getter and setter

In many native languages such as C++, getter (for example: I = getCount ()) is used to avoid direct access to member variables (I = mCount). This is a very good habit in C++ because the compiler can access it inline, and if you need to constrain or debug variables, you can add code at any time. On Android, this is not a good idea. Virtual methods are much more expensive than directly accessing member variables. In general interface definitions, getters and setters can be defined as OO, but in general classes, you should access variables directly.

6. Cache member variables locally

Accessing member variables is much slower than accessing local variables, the following code:

For (int I = 0; I < this.mCount; items +) dumpItem (this.mItems [I]); you should write: int count = this.mCount; Item [] items = this.mItems; for (int I = 0; I < count; items +) dumpItems (items [I]); (the display uses "this" to indicate that these are member variables)

A similar principle is that you should never call any method in the second condition of for. As shown in the following method, the getCount () method is called during each loop, which is much more expensive than saving the results in an int.

For (int I = 0; I < this.getCount (); iTunes +)

DumpItems (this.getItem (I)); similarly, if you want to access a variable multiple times, create a local variable for it first, for example:

Protected void drawHorizontalScrollBar (Canvas canvas, int width, int height) {if (isHorizontalScrollBarEnabled ()) {int size = mScrollBar.getSize (false); if (size)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report