`

Android高效编程注意事项

 
阅读更多
原文:http://kuikui.javaeye.com/blog/256665

最近用Android开发了几个模块,感觉有点慢,后来好好看了相关优化Android代码的知识,优化之后,感觉快了很多。在这里与大家分享一下,下面只是说的一些很基础有很重要的知识,你想要编写运行速度很快、占用内存少的代码可能有点帮助。

概述

There are two basic rules for resource-constrained systems

Don't do work that you don't need to do.

Don't allocate memory if you can avoid it.

All the tips below follow from these two basic tenets.

知识点

1,Avoid Creating Objects

能不使用包装类就不使用包装类。

尽量使用StringBuffer来处理字符串

尽量使用一维数组代替多维数组

2,Use Native Methods

尽量使用系统提供的接口方法,因为系统提供的接口方法使用C编写的,比自己用Java编写的效率高

3,Prefer Virtual Over Interface

多使用接口的具体实现类。

<1>,Map myMap1 = new HashMap();

<2>,HashMap myMap2 = new HashMap();

两者比较结果:第一种是一向大家比较推崇的,因为他对以后的维护成本低,但是接口方法的调用比实现类方法的调用更耗时。

4,Prefer Static Over Virtual

多使用静态的方法和属性

5,Avoid Internal Getters/Setters

避免使用C++C形式的(i=this.getCounter())这样子的代码

6,Cache Field Lookups

访问对象的属性比访问本地变量花费时间多。

Accessing object fields is much slower than accessing local variables.

Instead of writing:

for (int i = 0; i < this.mCount; i++)

dumpItem(this.mItems[i]);

You should write:

int count = this.mCount;

Item[] items = this.mItems;

for (int i = 0; i < count; i++)

dumpItems(items[i]);

7,Declare Constants Final

声明一些final类型的常量

static int intVal = 42;

static String strVal = "Hello, world!";

可以写成如下:

static final int intVal = 42;

static final String strVal = "Hello, world!";

8,Use Enhanced For Loop Syntax With Caution

谨慎使用增强的for循环,因为它创建多余临时变量。

public class Foo {

int mSplat;

static Foo mArray[] = new Foo[27];

public static void zero() {

int sum = 0;

for (int i = 0; i < mArray.length; i++) {

sum += mArray[i].mSplat;

}

}

public static void one() {

int sum = 0;

Foo[] localArray = mArray;

int len = localArray.length;

for (int i = 0; i < len; i++) {

sum += localArray[i].mSplat;

}

}

public static void two() {

int sum = 0;

for (Foo a: mArray) {

sum += a.mSplat;

}

}

}

zero()返回两次静态字段、每次循环的时候都要请求数组的长度

one()将所有的属性存放到本地,避免查找。

two()使用jdk1.5以上版本的增强for循环,这是有编译器拷贝数组的引用和长度到本地这在主循环体会产生额外的本地装载和存储,这跟one()相比,比其运行时间长一小点,同时也比one()4byte的存储空间u

To summarize all that a bit more clearly: enhanced for loop syntax performs well with arrays, but be cautious when using it with Iterable objects since there is additional object creation.

9,Avoid Enums

避免使用枚举。

10,Use Package Scope with Inner Classes

建议使用内部类

public class Foo {

private int mValue;

public void run() {

Inner in = new Inner();

mValue = 27;

in.stuff();

}

private void doStuff(int value) {

System.out.println("Value is " + value);

}

private class Inner {

void stuff() {

Foo.this.doStuff(Foo.this.mValue);

}

}

}

11,Avoid Float

尽量避免使用float类型

12,Some Sample Performance Numbers

一些常用操作的占用时间相对比较:

操作时间

Add a local variable1

Add a member variable4

Call String.length()5

Call empty static native method5

Call empty static method12

Call empty virtual method12.5

Call empty interface method15

Call Iterator:next() on a HashMap165

Call put() on a HashMap600

Inflate 1 View from XML22,000

Inflate 1 LinearLayout containing 1 TextView25,000

Inflate 1 LinearLayout containing 6 View objects100,000

Inflate 1 LinearLayout containing 6 TextView objects135,000

Launch an empty activity3,000,000

这些时间相对值,值得我们好好的权衡哦,很有帮助。

分享到:
评论

相关推荐

    Android 高效编程注意事项

    最近用 Android 开发了几个模块,感觉有点慢,后来好好看了相关优化 Android 代码的知识,优化之后,感觉快了很多。 在这里与大家分享一下,下面只是说 的一些很基础有很重要的知识,你想要编写运行速度很快、 占用内存少...

    Android编程之高效开发App的10个建议

    主要介绍了Android编程之高效开发App的10个建议,较为详细的分析了Android开发中的常见问题与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下

    《Android应用开发揭秘》附带光盘代码.

     14.3.2 游戏引擎设计注意事项  14.4 游戏引擎实现  14.4.1 Activity类实现  14.4.2 流程控制和线程  14.4.3 游戏对象与对象管理  14.4.4 图形引擎  14.4.5 物理引擎  14.4.6 事件模块  14.4.7 工具模块  ...

    Android应用开发揭秘pdf高清版

    14.3.2 游戏引擎设计注意事项 14.4 游戏引擎实现 14.4.1 Activity类实现 14.4.2 流程控制和线程 14.4.3 游戏对象与对象管理 14.4.4 图形引擎 14.4.5 物理引擎 14.4.6 事件模块 14.4.7 工具模块 14.4.8 脚本引擎、...

    《Android应用开发揭秘》源码

     14.3.2 游戏引擎设计注意事项  14.4 游戏引擎实现  14.4.1 Activity类实现  14.4.2 流程控制和线程  14.4.3 游戏对象与对象管理  14.4.4 图形引擎  14.4.5 物理引擎  14.4.6 事件模块  14.4.7 工具模块  ...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    7.4 使用动画的注意事项 292 第8章 理解Window和WindowManager 294 8.1 Window和WindowManager 294 8.2 Window的内部机制 297 8.2.1 Window的添加过程 298 8.2.2 Window的删除过程 301 8.2.3 Window的更新过程...

    Android开发艺术探索

    7.4 使用动画的注意事项 / 292 第8章 理解Window和Window Manager / 294 8.1 Window和Window Manager / 294 8.2 Window的内部机制 / 297 8.2.1 Window的添加过程 / 298 8.2.2 Window的删除过程 / 301 ...

    android开发艺术探索高清完整版PDF

    282 7.3.5 属性动画的工作原理 / 288 7.4 使用动画的注意事项 / 292 第8章 理解Window和Window Manager / 294 8.1 Window和Window Manager / 294 8.2 Window的内部机制 / 297 8.2.1 Window的添加过程 / 298 ...

Global site tag (gtag.js) - Google Analytics