gpt4 book ai didi

java - Android 上的 I/O 基准测试

转载 作者:行者123 更新时间:2023-11-30 09:44:51 27 4
gpt4 key购买 nike

我为 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();
}



}

最佳答案

众所周知,这些类型的微基准测试很难以您可以分析结果的方式编写。

SO 上有很多关于这个的问题,结论通常是很难得出任何结论:-)

例如考虑以下事实:您有 JIT 编译、非确定性垃圾收集以及运行时和 VM 的各种实现...

例如,我什至不确定在 2000 次迭代的紧密循环中执行某些操作是否会触发 JIT。

相关问题:

关于java - Android 上的 I/O 基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7684733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com