|
| 1 | +package org.javacore.nio; |
| 2 | + |
| 3 | +import org.javacore.io.RandomAccessFileT; |
| 4 | + |
| 5 | +import java.io.BufferedInputStream; |
| 6 | +import java.io.BufferedOutputStream; |
| 7 | +import java.io.DataInputStream; |
| 8 | +import java.io.DataOutputStream; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.FileOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.RandomAccessFile; |
| 14 | +import java.nio.IntBuffer; |
| 15 | +import java.nio.channels.FileChannel; |
| 16 | + |
| 17 | +/* |
| 18 | + * Copyright [2015] [Jeff Lee] |
| 19 | + * |
| 20 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + * you may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at |
| 23 | + * |
| 24 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Jeff Lee |
| 35 | + * @since 2015-10-15 18:38:17 |
| 36 | + * MappedByteBuffer与Old IO的性能比 |
| 37 | + */ |
| 38 | +public class MappedIO { |
| 39 | + private static int numOfInts = 4000000; |
| 40 | + private static int numOfUbuffInts = 200000; |
| 41 | + |
| 42 | + private abstract static class Tester { |
| 43 | + private String name; |
| 44 | + public Tester(String name){ |
| 45 | + this.name = name; |
| 46 | + } |
| 47 | + |
| 48 | + // 打印测试时间 |
| 49 | + public void runTests(){ |
| 50 | + System.out.print(name + ": "); |
| 51 | + try { |
| 52 | + long start = System.nanoTime(); |
| 53 | + test(); |
| 54 | + double duration = System.nanoTime() - start; |
| 55 | + System.out.format("%.2f\n",duration/1.0e9); |
| 56 | + } catch (IOException e) { |
| 57 | + throw new RuntimeException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public abstract void test() throws IOException; |
| 62 | + } |
| 63 | + |
| 64 | + private static Tester[] tests = { |
| 65 | + new Tester("Stream Write") { |
| 66 | + @Override |
| 67 | + public void test() throws IOException { |
| 68 | + DataOutputStream dos = new DataOutputStream( |
| 69 | + new BufferedOutputStream(new FileOutputStream(new File("data.txt")))); |
| 70 | + for (int i = 0; i < numOfInts; i++) |
| 71 | + dos.writeInt(i); |
| 72 | + dos.close(); |
| 73 | + } |
| 74 | + }, |
| 75 | + new Tester("Mapped Write") { |
| 76 | + @Override |
| 77 | + public void test() throws IOException { |
| 78 | + FileChannel fc = |
| 79 | + new RandomAccessFile("data.txt" , "rw").getChannel(); |
| 80 | + IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, |
| 81 | + 0,fc.size()).asIntBuffer(); |
| 82 | + for (int i = 0; i < numOfInts; i++) |
| 83 | + ib.put(i); |
| 84 | + fc.close(); |
| 85 | + } |
| 86 | + }, |
| 87 | + new Tester("Stream Read") { |
| 88 | + @Override |
| 89 | + public void test() throws IOException { |
| 90 | + DataInputStream dis = new DataInputStream( |
| 91 | + new BufferedInputStream(new FileInputStream(new File("data.txt")))); |
| 92 | + for (int i = 0; i < numOfInts; i++) |
| 93 | + dis.readInt(); |
| 94 | + dis.close(); |
| 95 | + } |
| 96 | + }, |
| 97 | + new Tester("Mapped Read") { |
| 98 | + @Override |
| 99 | + public void test() throws IOException { |
| 100 | + FileChannel fc = |
| 101 | + new FileInputStream(new File("data.txt")).getChannel(); |
| 102 | + IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, |
| 103 | + 0 , fc.size()).asIntBuffer(); |
| 104 | + while (ib.hasRemaining()) |
| 105 | + ib.get(); |
| 106 | + fc.close(); |
| 107 | + } |
| 108 | + }, |
| 109 | + new Tester("Stream Read/Write") { |
| 110 | + @Override |
| 111 | + public void test() throws IOException { |
| 112 | + RandomAccessFile raf = new RandomAccessFile( |
| 113 | + new File("data.txt"), "rw"); |
| 114 | + raf.writeInt(1); |
| 115 | + for (int i = 0; i < numOfUbuffInts; i++) { |
| 116 | + raf.seek(raf.length() - 4); |
| 117 | + raf.writeInt(raf.readInt()); |
| 118 | + } |
| 119 | + raf.close(); |
| 120 | + } |
| 121 | + }, |
| 122 | + new Tester("Mapped Read/Write") { |
| 123 | + @Override |
| 124 | + public void test() throws IOException { |
| 125 | + FileChannel fc = new RandomAccessFile( |
| 126 | + new File("data.txt"),"rw").getChannel(); |
| 127 | + IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, |
| 128 | + 0,fc.size()).asIntBuffer(); |
| 129 | + ib.put(0); |
| 130 | + for (int i = 1; i < numOfUbuffInts; i++) |
| 131 | + ib.put(ib.get(i-1)); |
| 132 | + fc.close(); |
| 133 | + } |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + public static void main(String[] args) { |
| 138 | + for (Tester tester : tests) |
| 139 | + tester.runTests(); |
| 140 | + } |
| 141 | +} |
0 commit comments