|
| 1 | +// newio/MappedIO.java |
| 2 | +// (c)2017 MindView LLC: see Copyright.txt |
| 3 | +// We make no guarantees that this code is fit for any purpose. |
| 4 | +// Visit http://OnJava8.com for more book information. |
| 5 | +import java.util.*; |
| 6 | +import java.nio.*; |
| 7 | +import java.nio.channels.*; |
| 8 | +import java.io.*; |
| 9 | + |
| 10 | +public class MappedIO { |
| 11 | + private static int numOfInts = 4_000_000; |
| 12 | + private static int numOfUbuffInts = 100_000; |
| 13 | + private abstract static class Tester { |
| 14 | + private String name; |
| 15 | + public Tester(String name) { |
| 16 | + this.name = name; |
| 17 | + } |
| 18 | + public void runTest() { |
| 19 | + System.out.print(name + ": "); |
| 20 | + long start = System.nanoTime(); |
| 21 | + test(); |
| 22 | + double duration = System.nanoTime() - start; |
| 23 | + System.out.format("%.3f%n", duration/1.0e9); |
| 24 | + } |
| 25 | + public abstract void test(); |
| 26 | + } |
| 27 | + private static Tester[] tests = { |
| 28 | + new Tester("Stream Write") { |
| 29 | + @Override |
| 30 | + public void test() { |
| 31 | + try( |
| 32 | + DataOutputStream dos = |
| 33 | + new DataOutputStream( |
| 34 | + new BufferedOutputStream( |
| 35 | + new FileOutputStream( |
| 36 | + new File("temp.tmp")))) |
| 37 | + ) { |
| 38 | + for(int i = 0; i < numOfInts; i++) |
| 39 | + dos.writeInt(i); |
| 40 | + } catch(IOException e) { |
| 41 | + throw new RuntimeException(e); |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + new Tester("Mapped Write") { |
| 46 | + @Override |
| 47 | + public void test() { |
| 48 | + try( |
| 49 | + FileChannel fc = |
| 50 | + new RandomAccessFile("temp.tmp", "rw") |
| 51 | + .getChannel() |
| 52 | + ) { |
| 53 | + IntBuffer ib = |
| 54 | + fc.map(FileChannel.MapMode.READ_WRITE, |
| 55 | + 0, fc.size()).asIntBuffer(); |
| 56 | + for(int i = 0; i < numOfInts; i++) |
| 57 | + ib.put(i); |
| 58 | + } catch(IOException e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + new Tester("Stream Read") { |
| 64 | + @Override |
| 65 | + public void test() { |
| 66 | + try( |
| 67 | + DataInputStream dis = |
| 68 | + new DataInputStream( |
| 69 | + new BufferedInputStream( |
| 70 | + new FileInputStream("temp.tmp"))) |
| 71 | + ) { |
| 72 | + for(int i = 0; i < numOfInts; i++) |
| 73 | + dis.readInt(); |
| 74 | + } catch(IOException e) { |
| 75 | + throw new RuntimeException(e); |
| 76 | + } |
| 77 | + } |
| 78 | + }, |
| 79 | + new Tester("Mapped Read") { |
| 80 | + @Override |
| 81 | + public void test() { |
| 82 | + try( |
| 83 | + FileChannel fc = new FileInputStream( |
| 84 | + new File("temp.tmp")).getChannel() |
| 85 | + ) { |
| 86 | + IntBuffer ib = |
| 87 | + fc.map(FileChannel.MapMode.READ_ONLY, |
| 88 | + 0, fc.size()).asIntBuffer(); |
| 89 | + while(ib.hasRemaining()) |
| 90 | + ib.get(); |
| 91 | + } catch(IOException e) { |
| 92 | + throw new RuntimeException(e); |
| 93 | + } |
| 94 | + } |
| 95 | + }, |
| 96 | + new Tester("Stream Read/Write") { |
| 97 | + @Override |
| 98 | + public void test() { |
| 99 | + try( |
| 100 | + RandomAccessFile raf = |
| 101 | + new RandomAccessFile( |
| 102 | + new File("temp.tmp"), "rw") |
| 103 | + ) { |
| 104 | + raf.writeInt(1); |
| 105 | + for(int i = 0; i < numOfUbuffInts; i++) { |
| 106 | + raf.seek(raf.length() - 4); |
| 107 | + raf.writeInt(raf.readInt()); |
| 108 | + } |
| 109 | + } catch(IOException e) { |
| 110 | + throw new RuntimeException(e); |
| 111 | + } |
| 112 | + } |
| 113 | + }, |
| 114 | + new Tester("Mapped Read/Write") { |
| 115 | + @Override |
| 116 | + public void test() { |
| 117 | + try( |
| 118 | + FileChannel fc = new RandomAccessFile( |
| 119 | + new File("temp.tmp"), "rw").getChannel() |
| 120 | + ) { |
| 121 | + IntBuffer ib = |
| 122 | + fc.map(FileChannel.MapMode.READ_WRITE, |
| 123 | + 0, fc.size()).asIntBuffer(); |
| 124 | + ib.put(0); |
| 125 | + for(int i = 1; i < numOfUbuffInts; i++) |
| 126 | + ib.put(ib.get(i - 1)); |
| 127 | + } catch(IOException e) { |
| 128 | + throw new RuntimeException(e); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }; |
| 133 | + public static void main(String[] args) { |
| 134 | + Arrays.stream(tests).forEach(Tester::runTest); |
| 135 | + } |
| 136 | +} |
| 137 | +/* Output: |
| 138 | +Stream Write: 0.269 |
| 139 | +Mapped Write: 0.022 |
| 140 | +Stream Read: 0.262 |
| 141 | +Mapped Read: 0.010 |
| 142 | +Stream Read/Write: 1.620 |
| 143 | +Mapped Read/Write: 0.007 |
| 144 | +*/ |
0 commit comments