| 时间(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 代码:
- package com;
 - import java.io.File;
 - import java.io.FileInputStream;
 - import java.io.FileOutputStream;
 - import java.io.IOException;
 - import java.nio.ByteBuffer;
 - import java.nio.channels.FileChannel;
 - import junit.framework.TestCase;
 - /**
 - * NIO read write test
 - *
 - * @author wutao
 - *
 - */
 - public class NioDemo extends TestCase {
 - public void testRead() throws IOException {
 - int[] sizes = { 10000, 1000, 100, 50, 5, 1 };
 - // Arrays.sort(sizes);
 - System.out.println(new File("text.txt").length());
 - for (int i = 0; i < sizes.length; i++) {
 - int size = sizes[i];
 - FileInputStream fins = new FileInputStream("text.txt");
 - FileChannel fc = fins.getChannel();
 - if (!new File("text2.txt").exists()) {
 - new File("text2.txt").createNewFile();
 - }
 - ByteBuffer buffer = ByteBuffer.allocate(size);
 - FileOutputStream fouts = new FileOutputStream("text2.txt");
 - FileChannel fc2 = fouts.getChannel();
 - long start = System.currentTimeMillis();
 - while (true) {
 - buffer.clear();
 - int r = fc.read(buffer);
 - if (r == -1) {
 - break;
 - }
 - buffer.flip();
 - fc2.write(buffer);
 - }
 - long end = 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
 
- import java.io.*;
 - import java.nio.*;
 - import java.nio.channels.*;
 - public class NIOCopier {
 - public static void main(String[] args) throws IOException {
 - FileInputStream inFile = new FileInputStream(args[0]);
 - FileOutputStream outFile = new FileOutputStream(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
                网页名称:JavaNIO性能测试
                
                文章来源:http://www.csdahua.cn/qtweb/news25/82325.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网