ITeye博客

测试NIO性能

wutao8818 · 2007-06-27 14:46 · io · Java · junit · OS · Unix · Markdown

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

 

  1. package com;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.nio.ByteBuffer;
  7. import java.nio.channels.FileChannel;
  8. import junit.framework.TestCase;
  9. /**
  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 < 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 代码

 

  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. public class NIOCopier {
  5. public static void main(String[] args) throws IOException {
  6. FileInputStream inFile = new FileInputStream(args[0]);
  7. FileOutputStream outFile = new FileOutputStream(args[1]);
  8. FileChannel inChannel = inFile.getChannel( );
  9. 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.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。