java nio 性能_Java NIO性能测试

这篇博客对比了传统I/O与Java NIO在文件读写上的性能。通过一个简单的4.3GB文件复制测试,发现使用NIO比传统I/O快约16%,而Finder的复制速度最快。示例代码展示了如何使用NIO进行文件读写操作。
摘要由CSDN通过智能技术生成

时间(ms)

文件大小(byte)

Buffer(byte)

434

603900

10000

0

0

1000

0

46

100

0

188

50

0

281

5

0

2406

1

47

12000

java 代码:

packagecom;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.nio.ByteBuffer;

importjava.nio.channels.FileChannel;

importjunit.framework.TestCase;

/**

* NIO read write test

*

* @author wutao

*

*/

publicclassNioDemoextendsTestCase {

publicvoidtestRead()throwsIOException {

int[] sizes = {10000,1000,100,50,5,1};

// Arrays.sort(sizes);

System.out.println(newFile("text.txt").length());

for(inti =0; i 

intsize = sizes[i];

FileInputStream fins =newFileInputStream("text.txt");

FileChannel fc = fins.getChannel();

if(!newFile("text2.txt").exists()) {

newFile("text2.txt").createNewFile();

}

ByteBuffer buffer = ByteBuffer.allocate(size);

FileOutputStream fouts =newFileOutputStream("text2.txt");

FileChannel fc2 = fouts.getChannel();

longstart = System.currentTimeMillis();

while(true) {

buffer.clear();

intr = fc.read(buffer);

if(r == -1) {

break;

}

buffer.flip();

fc2.write(buffer);

}

longend = System.currentTimeMillis();

System.out.println("---------"+ size +"---------");

System.out.println(end - start);

fc.close();

fc2.close();

fins.close();

fouts.close();

}

}

}

Java™ I/O, 2nd Edition

By Elliotte Rusty Harold

...............................................

Publisher: O'Reilly

Pub Date: May 2006

Print ISBN-10: 0-596-52750-0

Print ISBN-13: 978-0-59-652750-1

Pages: 726

importjava.io.*;

importjava.nio.*;

importjava.nio.channels.*;

publicclassNIOCopier {

publicstaticvoidmain(String[] args)throwsIOException {

FileInputStream inFile =newFileInputStream(args[0]);

FileOutputStream outFile =newFileOutputStream(args[1]);

FileChannel inChannel = inFile.getChannel( );

FileChannel outChannel = outFile.getChannel( );

for(ByteBuffer buffer = ByteBuffer.allocate(1024*1024);

inChannel.read(buffer) != -1;

buffer.clear( )) {

buffer.flip( );

while(buffer.hasRemaining( )) outChannel.write(buffer);

}

inChannel.close( );

outChannel.close( );

}

}

In a very unscientific test, copying one large (4.3-GB) file on one platform (a dual 2.5-GHz PowerMac G5 running Mac OS X 10.4.1) using traditional I/O with buffered streams and an 8192-byte buffer took 305 seconds. Expanding and reducing the buffer size didn't shift the overall numbers more than 5% and if anything tended to increase the time to copy. (Using a one-megabyte buffer like Example 14-1's actually increased the time to over 23 minutes.) Using new I/O as implemented in Example 14-1 was about 16% faster, at 255 seconds. A straight Finder copy took 197 seconds. Using the Unix cp command actually took 312 seconds, so the Finder is doing some surprising optimizations under the hood.

原文链接:http://wutaoo.iteye.com/blog/94666

【编辑推荐】

【责任编辑:小林 TEL:(010)68476606】

点赞 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值