Java IO开销测试比较(不包含直接存取内存方式),请不吝赐教.
package ringBuffer;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
public class PerformanceWriteTest {
/**
* @param args
*/
public static void main(String[] args) {
String outputFile = "F:\\test\\ioTest.txt";
Long length = 0L ;
Long totalTime = 0L;
for (int j = 0; j
//for (int j = 0; j
StringBuffer sb = new StringBuffer();
for (Integer i = 0; i
//for (Integer i = 0; i
sb.append(j+i.toString() + "V");
}
sb.append("S");
//byte[] msgs = sb.toString().getBytes();
length = (long) sb.toString().length() ;
long start = System.currentTimeMillis() ;
appendFileTest(outputFile,sb.toString());
totalTime = totalTime + (System.currentTimeMillis() - start) ;
}
System.out.println(" Total Data is : " + length*5/1000 + " Kbytes! ") ;
System.out.println(" Total Time is : " + totalTime) ;
System.out.println(" Averge Speed is :" + length*5/(totalTime*1000) + " Kbytes");
}
private static void appendFileTest(String outputFile, String msgs) {
// append1(outputFile, msgs) ; //FileOutputStream
// append2(outputFile, msgs) ; //FileWriter
append3(outputFile, msgs) ; //RandomAccessFile
}
private static void append1(String outputFile, String msgs) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFile, true)));
out.append(msgs) ;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void append2(String outputFile, String msgs) {
try {
FileWriter writer = new FileWriter(outputFile, true); // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer.write(msgs);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void append3(String outputFile, String msgs) {
try {
RandomAccessFile randomFile = new RandomAccessFile(outputFile, "rw"); // 打开一个随机访问文件流,按读写方式
long fileLength = randomFile.length(); // 文件长度,字节数
randomFile.seek(fileLength); // 将写文件指针移到文件尾
randomFile.writeBytes(msgs);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[GC 32704K->2872K(124992 K), 0.0015308 secs]
[GC 35576K->5152K(157696K), 0.0018430 secs]
[GC 70560K->9768K(157696K), 0.0032342 secs]
[GC 75176K->28240K(223104K), 0.0056030 secs]
[GC 159056K->28888K(223104K), 0.0009104 secs]
[GC 159704K->37464K(353856K), 0.0033345 secs]
[GC 299096K->42152K(354048K), 0.0022436 secs]
[GC 303784K->39848K(615 744K), 0.0009303 secs]
[GC 563112K->55968K(616 192K), 0.0087540 secs]
Total Data is : 39444 Kbytes!
Total Time is : 159
Averge Speed is :248 Kbytes
2.826G
[GC 32704K->2872K(124992K), 0.0136790 secs]
[GC 35576K->5184K(157696K), 0.0019457 secs]
[GC 70592K->9792K(157696K), 0.0031822 secs]
[GC 75200K->28240K(223104K), 0.0056500 secs]
[GC 152655K->43624K(223104K), 0.0051765 secs]
[GC 174440K->52848K(353856K), 0.0034980 secs]
[GC 314480K->55276K(354176K), 0.0013858 secs]
[GC 314139K->71372K(615488K), 0.0058115 secs]
[Full GC 71372K->18926K(610816K), 0.0094205 secs]
[GC 542190K->43678K(610880K), 0.0038294 secs]
Total Data is : 39444 Kbytes!
Total Time is : 153
Averge Speed is :257 Kbytes
3.43G
[GC 32704K->2872K(124992K), 0.0012018 secs]
[GC 35576K->5184K(124992K), 0.0021390 secs]
[GC 37888K->9800K(124992K), 0.0031359 secs]
[GC 42504K->9824K(157696K), 0.0005724 secs]
[GC 75232K->28208K(157696K), 0.0059463 secs]
[GC 93616K->28224K(223040K), 0.0007707 secs]
[GC 159040K->32852K(223360K), 0.0095536 secs]
[GC 163668K->46740K(354880K), 0.0055367 secs]
[GC 308372K->55892K(355008K), 0.0032216 secs]
[GC 317524K->60484K(511168K), 0.0017500 secs]
[GC 478468K->65092K(511808K), 0.0033206 secs]
Total Data is : 39444 Kbytes!
Total Time is : 68
Averge Speed is :580 Kbytes
2.87G
综上可见,RandomAccessFile 性价比最高。
------解决思路----------------------
使用jconsole.exe这个工具,在jdk的bin目录下
------解决思路----------------------
楼主做这个测试的目的是什么呢?
楼主有没有看过在硬盘里的这个文件的内容呢?
------解决思路----------------------
引用:Quote: 引用:楼主做这个测试的目的是什么呢?
楼主有没有看过在硬盘里的这个文件的内容呢?
就是看下哪种方式更快. 硬盘里的文件是字符文件啊。
其实我觉得你这几个操作并不是等价的。