Android 实战技巧之性能测试类


通常来说手机上的程序都很金贵,配置不高但要良好的性能。虽然目前的新手机都有着显赫的配置,但性能方面仍然很重要。

Android程序首推开发语言是Java,易用的同时也带来了性能上的问题,尤其是在动画和游戏开发方面。

高性能高效率的程序也是很难求的,通常都是在几番磨难之后才能诞下这样的程序。

平时,我们应该多注意。不要以为性能离我们很远,其实它存在于我们的指尖。

下面是两个常用的测试类,一个是时间测试类,另一个是内存使用测试类。经常测试一下效果,会得到意想不到的好处的。

大家不妨试试。


时间测试类

  1. package com.linc;  
  2.   
  3.   
  4. import android.util.Log;  
  5.   
  6.   
  7. public class TimeTest {  
  8.     private static long startTime ;  
  9.     private static long endTime ;  
  10.     public static void start()  
  11.     {  
  12.         startTime = System.currentTimeMillis();  
  13.     }  
  14.     public static void end()  
  15.     {  
  16.         endTime = System.currentTimeMillis();  
  17.         long time = endTime - startTime;  
  18.         Log.i("TimeTest""calculateProcessTime is "+time);  
  19.     }  
  20. }  
内存测试类
  1. package com.linc;  
  2.   
  3. import android.util.Log;  
  4.   
  5. public class MemoryTest {  
  6.     private static long startMemory;  
  7.     private static long endMemory;  
  8.       
  9.     private static long memoryUsed()  
  10.     {  
  11.         long total = Runtime.getRuntime().totalMemory();  
  12.         long free = Runtime.getRuntime().freeMemory();  
  13.         return (total - free);  
  14.     }  
  15.       
  16.     public static void start()  
  17.     {  
  18.         startMemory = memoryUsed();  
  19.     }  
  20.       
  21.     public static void end()  
  22.     {  
  23.         endMemory = memoryUsed();  
  24.         long memo = endMemory - startMemory;  
  25.         Log.i("MemoryTest""calculateUsedMemory is "+memo);  
  26.     }  
  27. }  

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容