I/O benchmarking on Android
I created my own benchmark for I/O but I cannot understand if it's real or not.
Do you have a sample code to show me about performances concerning i/o?
Any suggestion to optimize it?
thank you in advance :)
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 (IOExce开发者_如何学Pythonption 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();
}
}
These type of micro benchmarks are notoriously hard to write in a way such that you can analyze the results.
There are various questions regarding this here on SO, and the conclusion is usually that it's hard to draw any conclusions :-)
Consider for instance the fact that you have a JIT compilation, non-deterministic garbage collection and various implementations of the runtime and VM...
I'm for instance not even sure doing something in a tight loop for 2000 iterations will trigger JIT.
Related questions:
- How do I write a correct micro-benchmark in Java?
- Create quick/reliable benchmark with java?
精彩评论