2011-10-07 35 views
2

我爲I/O創建了自己的基準測試,但我無法理解它是否真實。Android上的I/O基準測試

您是否有示例代碼向我展示有關I/O的性能?

任何建議來優化它?

預先感謝您:)

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.util.Random; 

import android.content.Context; 
import android.os.Environment; 


public class MainHandler { 

    protected Context mContext; 
    public String Data = new String(""); 
    /** Called when the activity is first created. 
    * @throws Exception */ 



    public MainHandler(Context c) throws Exception{ 
     this.mContext = c; 
     generateString(); 
     writeOnHd(); 
     readOnHd(); 
     writeOnSd(); 
     readOnSd(); 
    } 




    public void writeOnHd() throws Exception { 
     System.out.println("-------------------------------------------------------"); 
     long startTime = System.currentTimeMillis(); 


     FileOutputStream fOut = mContext.openFileOutput("samplefile.txt", 777); 
     OutputStreamWriter osw = new OutputStreamWriter(fOut); 

     int c = 2000; 
     for (int i = 0; i < c; i++) { 
      osw.write(this.Data); 
      osw.flush(); 
     } 
     osw.close(); 
     System.out.println("Total time to write: " 
       + ((System.currentTimeMillis() - startTime)) 
       + " miliseconds "); 

     System.gc(); 
    } 





    public void readOnHd() throws Exception { 

     long startTime = System.currentTimeMillis(); 

     FileInputStream fIn = mContext.openFileInput("samplefile.txt"); 
     InputStreamReader isr = new InputStreamReader(fIn); 

     char[] inputBuffer = new char[1024]; 
     int len = 0; 
     int length= 1024; 

     while((len = isr.read(inputBuffer)) != -1){ 
      //System.out.println("Total bytes read: " + len); 


     } 

     fIn.close(); 


     System.out.println("Total time to read: " 
       + ((System.currentTimeMillis() - startTime)) 
       + " miliseconds number block "); 
     mContext.deleteFile("samplefile.txt"); 

     System.gc(); 
    } 







    public void writeOnSd(){ 

     long startTime = System.currentTimeMillis(); 

     File sd = Environment.getExternalStorageDirectory(); 
     File f = new File(sd, "provafile.txt"); 


     FileWriter fw = null; 
     BufferedWriter bw = null; 
     try{ 
      fw = new FileWriter(f, true); 
      bw = new BufferedWriter(fw); 
      int c = 2000; 
      for (int i = 0; i < c; i++) { 
       bw.write(this.Data); 
       bw.flush(); 
      } 
      bw.close(); 
      fw.close(); 


      } 
      catch (IOException e) {  
       e.printStackTrace(); 
      } 
      System.gc(); 
      System.out.println("Total time to write on sd: " 
        + ((System.currentTimeMillis() - startTime)) 
        + " miliseconds "); 
     } 





    public void readOnSd(){ 

     long startTime = System.currentTimeMillis(); 
     try{ 

      File f = new File(Environment.getExternalStorageDirectory()+"/provafile.txt"); 
      FileInputStream fIn = new FileInputStream(f); 
      InputStreamReader isr = new InputStreamReader(fIn); 

      char[] inputBuffer = new char[1024]; 
      int len = 0; 
      int length= 1024; 

      while((len = isr.read(inputBuffer)) != -1){ 
       //System.out.println("Total bytes read on sd: " + len); 

      } 


      fIn.close(); 
      f.delete(); 



     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     System.gc(); 
     System.out.println("Total time to read on sd: " + 
       ((System.currentTimeMillis() - startTime)) + 
       " miliseconds "); 

    } 

    public void generateString(){ 
     int c = 2000; 
     for(int i =0; i<=c; i++) { 
      Random randomGenerator = new Random(); 
      String container[]= {"a", "D","£","&","e","f","ç","h","§","j","*","]","{","n","o","|",")","4","s","t","u","=","z"}; 
      int n = randomGenerator.nextInt(22); 
      this.Data += container[n]; 
     } 
     System.gc(); 
    } 



} 

回答

2

這些類型的微基準測試中是出了名難的方式,這樣你可以分析結果來寫。

這裏有對SO對此的各種問題,並得出結論通常是很難得出任何結論:-)

例如考慮,你有一個JIT編譯,非確定性垃圾收集的事實以及運行時和虛擬機的各種實現...我想,例如,甚至不確定在2000次迭代的緊密循環中執行某些操作將觸發JIT。

相關的問題:

+0

謝謝您的回答! 順便說一下,有時我會得到像24mb/s這樣的值......我不明白我是否可以將其用作可靠的測試... – fran

+0

可能。可能不是:)如上所述,這是非常難以分辨;-) – aioobe

+0

我想我會嘗試5次相同的操作與不同的文件,我會計算一個average..maybe我可以得到一個更好的價值..btw謝謝你的支持:) – fran