---
title: "Android最佳实践摘要：Java性能设计"
author: "alanwu"
date: "2009-11-02T10:31:48Z"
tags: []
slug: "android-java"
draft: false
lastmod: "2009-11-02T10:31:48Z"
id: 508413
url: "https://alanwu.iteye.com/blog/508413"
---

原文有例子做解释，很容易理解。

有些观点和传统的想法，比如不要用接口，不要用set/get方法，多用静态方法，字段...

而大部分是放在哪里都适用的最佳实践。

 

两个原则：

1\. 不要做你不需要的

2\. 不要分配你可以避免的内存

 

摘要：

避免创建对象

使用本地方法(Native Methods)

用实际类比接口好

用静态方法，字段

避免使用get/set

缓存查询字段

将常量声明为final

使用增强的循环

避免枚举

在内类中使用包范围

避免浮点

 

一些性能数据

 

 

Action Time Add a local variable 1 Add a member variable 4 Call String.length() 5 Call empty static native method 5 Call empty static method 12 Call empty virtual method 12.5 Call empty interface method 15 Call Iterator:next() on a HashMap 165 Call put() on a HashMap 600 Inflate 1 View from XML 22,000 Inflate 1 LinearLayout containing 1 TextView 25,000 Inflate 1 LinearLayout containing 6 View objects 100,000 Inflate 1 LinearLayout containing 6 TextView objects 135,000 Launch an empty activity 3,000,000

[]()

http://developer.android.com/guide/practices/design/performance.html

 

关于Scala在Android上的性能问题请看：

http://groups.google.com/group/scalacn/t/a2fef8243909d7e6