---
title: "测试NIO性能"
author: "wutao8818"
date: "2007-06-27T14:46:08Z"
tags: ["Java", "OS", "Unix", "junit"]
slug: "nio"
draft: false
lastmod: "2007-06-27T14:46:08Z"
id: 94666
url: "https://wutaoo.iteye.com/blog/94666"
---

时间(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 代码

 

01. package com;
02. import java.io.File;
03. import java.io.FileInputStream;
04. import java.io.FileOutputStream;
05. import java.io.IOException;
06. import java.nio.ByteBuffer;
07. import java.nio.channels.FileChannel;
08. import junit.framework.TestCase;
09. /\**
10. * NIO read write test
11. \*
12. * @author wutao
13. \*
14. \*/
15. public class NioDemo extends TestCase {
16. public void testRead() throws IOException {
17. int\[] sizes = { 10000, 1000, 100, 50, 5, 1 };
18. // Arrays.sort(sizes);
19. System.out.println(new File("text.txt").length());
20. for (int i = 0; i &lt; sizes.length; i++) {
21. int size = sizes\[i];
22. FileInputStream fins = new FileInputStream("text.txt");
23. FileChannel fc = fins.getChannel();
24. if (!new File("text2.txt").exists()) {
25. new File("text2.txt").createNewFile();
26. }
27. ByteBuffer buffer = ByteBuffer.allocate(size);
28. FileOutputStream fouts = new FileOutputStream("text2.txt");
29. FileChannel fc2 = fouts.getChannel();
30. long start = System.currentTimeMillis();
31. while (true) {
32. buffer.clear();
33. int r = fc.read(buffer);
34. if (r == -1) {
35. break;
36. }
37. buffer.flip();
38. fc2.write(buffer);
39. }
40. long end = System.currentTimeMillis();
41. System.out.println("---------" + size + "---------");
42. System.out.println(end - start);
43. fc.close();
44. fc2.close();
45. fins.close();
46. fouts.close();
47. }
48. }
49. }

 

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

java 代码

 

01. import java.io.\*;
02. import java.nio.\*;
03. import java.nio.channels.\*;
04. public class NIOCopier {
05. public static void main(String\[] args) throws IOException {
06. FileInputStream inFile = new FileInputStream(args\[0]);
07. FileOutputStream outFile = new FileOutputStream(args\[1]);
08. FileChannel inChannel = inFile.getChannel( );
09. FileChannel outChannel = outFile.getChannel( );
10. for (ByteBuffer buffer = ByteBuffer.allocate(1024\*1024);
11. inChannel.read(buffer) != -1;
12. buffer.clear( )) {
13. buffer.flip( );
14. while (buffer.hasRemaining( )) outChannel.write(buffer);
15. }
16. inChannel.close( );
17. outChannel.close( );
18. }
19. }

 

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.