歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 實戰技巧之性能測試類

Android 實戰技巧之性能測試類

日期:2017/3/1 10:24:40   编辑:Linux編程

通常來說手機上的程序都很金貴,配置不高但要良好的性能。雖然目前的新手機都有著顯赫的配置,但性能方面仍然很重要。

Android程序首推開發語言是Java,易用的同時也帶來了性能上的問題,尤其是在動畫和游戲開發方面。

高性能高效率的程序也是很難求的,通常都是在幾番磨難之後才能誕下這樣的程序。

平時,我們應該多注意。不要以為性能離我們很遠,其實它存在於我們的指尖。

下面是兩個常用的測試類,一個是時間測試類,另一個是內存使用測試類。經常測試一下效果,會得到意想不到的好處的。

大家不妨試試。


時間測試類

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

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved