Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4.  
  5. /**
  6. * Compares the performance of streaming data to disk vs using memory mapped files.
  7. *.
  8. public class MappedIO {
  9. private static int numOfInts = 4000000;
  10. private static int numOfUbuffInts = 200000;
  11. private abstract static class Tester {
  12. private String name;
  13. public Tester(String name) { this.name = name; }
  14. public long runTest() {
  15. System.out.print(name + ": ");
  16. try {
  17. long startTime = System.currentTimeMillis();
  18. test();
  19. long endTime = System.currentTimeMillis();
  20. return (endTime - startTime);
  21. } catch (IOException e) {
  22. throw new RuntimeException(e);
  23. }
  24. }
  25. public abstract void test() throws IOException;
  26. }
  27. private static Tester[] tests = {
  28. new Tester("Stream Write") {
  29. public void test() throws IOException {
  30. DataOutputStream dos = new DataOutputStream(
  31. new BufferedOutputStream(
  32. new FileOutputStream(new File("temp.tmp"))));
  33. for(int i = 0; i < numOfInts; i++)
  34. dos.writeInt(i);
  35. dos.close();
  36. }
  37. },
  38. new Tester("Mapped Write") {
  39. public void test() throws IOException {
  40. FileChannel fc =
  41. new RandomAccessFile("temp.tmp", "rw")
  42. .getChannel();
  43. IntBuffer ib = fc.map(
  44. FileChannel.MapMode.READ_WRITE, 0, fc.size())
  45. .asIntBuffer();
  46. for(int i = 0; i < numOfInts; i++)
  47. ib.put(i);
  48. fc.close();
  49. }
  50. },
  51. new Tester("Stream Read") {
  52. public void test() throws IOException {
  53. DataInputStream dis = new DataInputStream(
  54. new BufferedInputStream(
  55. new FileInputStream("temp.tmp")));
  56. for(int i = 0; i < numOfInts; i++)
  57. dis.readInt();
  58. dis.close();
  59. }
  60. },
  61. new Tester("Mapped Read") {
  62. public void test() throws IOException {
  63. FileChannel fc = new FileInputStream(
  64. new File("temp.tmp")).getChannel();
  65. IntBuffer ib = fc.map(
  66. FileChannel.MapMode.READ_ONLY, 0, fc.size())
  67. .asIntBuffer();
  68. while(ib.hasRemaining())
  69. ib.get();
  70.  
  71. fc.close();
  72. }
  73. },
  74. new Tester("Stream Read/Write") {
  75. public void test() throws IOException {
  76. RandomAccessFile raf = new RandomAccessFile(
  77. new File("temp.tmp"), "rw");
  78. raf.writeInt(1);
  79. for(int i = 0; i < numOfUbuffInts; i++) {
  80. raf.seek(raf.length() - 4);
  81. raf.writeInt(raf.readInt());
  82. }
  83. raf.close();
  84. }
  85. },
  86. new Tester("Mapped Read/Write") {
  87. public void test() throws IOException {
  88. FileChannel fc = new RandomAccessFile(
  89. new File("temp.tmp"), "rw").getChannel();
  90. IntBuffer ib = fc.map(
  91. FileChannel.MapMode.READ_WRITE, 0, fc.size())
  92. .asIntBuffer();
  93. ib.put(0);
  94. for(int i = 1; i < numOfUbuffInts; i++)
  95. ib.put(ib.get(i - 1));
  96.  
  97. fc.close();
  98. }
  99. }
  100. };
  101. public static void main(String[] args) {
  102. for(int i = 0; i < tests.length; i++)
  103. System.out.println(tests[i].runTest());
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement