Post date: Aug 18, 2009 9:47:40 AM
原文:http://developer.android.com/guide/practices/design/performance.html
手持平台是要特别注意优化滴,java平台更需要注意优化滴。
尽量少创建对象
从字符串里提取数据的时候,使用substring,而不是创建一个新对象。这样可以共用String对象的数据区。
避免创建临时交互用的小对象。比如返回String对象的,同时这个对象又加入String buffer里,直接在函数里添加到String buffer里就少了一个临时对象了。
使用低维度的数组,少用高维度的数组
避免使用对象数组,如包含(int,int)的数组,直接使用2个int数组效率更高
短期对象的频繁创建会导致频繁GC,是需要尽量避免的。
尽量使用SDK的方法
比如 String.indexOf(), String.lastIndexOf(),SDK的方法内部使用C/C++实现效率比自己用java代码实现的要高N倍。
另一方面,调用native method比调用interpreted method要慢,所以要避免使用native method来进行琐碎的计算。
(The flip side of that advice is that punching through to a native method is more expensive than calling an interpreted method. Don't use native methods for trivial computation, if you can avoid it.)
(to be continue...)