<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                <!-- Performance --> ### 性能 雖然舊的 I/O 流的性能通過使用 **NIO** 實現得到了改進,但是映射文件訪問往往要快得多。下例帶來一個簡單的性能比較。代碼示例: ```java // newio/MappedIO.java // 我們無法保證該代碼是否適用于其他用途。 // 訪問 http://OnJava8.com 了解更多本書信息。 import java.util.*; import java.nio.*; import java.nio.channels.*; import java.io.*; public class MappedIO { private static int numOfInts = 4_000_000; private static int numOfUbuffInts = 100_000; private abstract static class Tester { private String name; Tester(String name) { this.name = name; } public void runTest() { System.out.print(name + ": "); long start = System.nanoTime(); test(); double duration = System.nanoTime() - start; System.out.format("%.3f%n", duration/1.0e9); } public abstract void test(); } private static Tester[] tests = { new Tester("Stream Write") { @Override public void test() { try( DataOutputStream dos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( new File("temp.tmp")))) ) { for(int i = 0; i < numOfInts; i++) dos.writeInt(i); } catch(IOException e) { throw new RuntimeException(e); } } }, new Tester("Mapped Write") { @Override public void test() { try( FileChannel fc = new RandomAccessFile("temp.tmp", "rw") .getChannel() ) { IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer(); for(int i = 0; i < numOfInts; i++) ib.put(i); } catch(IOException e) { throw new RuntimeException(e); } } }, new Tester("Stream Read") { @Override public void test() { try( DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream("temp.tmp"))) ) { for(int i = 0; i < numOfInts; i++) dis.readInt(); } catch(IOException e) { throw new RuntimeException(e); } } }, new Tester("Mapped Read") { @Override public void test() { try( FileChannel fc = new FileInputStream( new File("temp.tmp")).getChannel() ) { IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer(); while(ib.hasRemaining()) ib.get(); } catch(IOException e) { throw new RuntimeException(e); } } }, new Tester("Stream Read/Write") { @Override public void test() { try( RandomAccessFile raf = new RandomAccessFile( new File("temp.tmp"), "rw") ) { raf.writeInt(1); for(int i = 0; i < numOfUbuffInts; i++) { raf.seek(raf.length() - 4); raf.writeInt(raf.readInt()); } } catch(IOException e) { throw new RuntimeException(e); } } }, new Tester("Mapped Read/Write") { @Override public void test() { try( FileChannel fc = new RandomAccessFile( new File("temp.tmp"), "rw").getChannel() ) { IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer(); ib.put(0); for(int i = 1; i < numOfUbuffInts; i++) ib.put(ib.get(i - 1)); } catch(IOException e) { throw new RuntimeException(e); } } } }; public static void main(String[] args) { Arrays.stream(tests).forEach(Tester::runTest); } } ``` 輸出結果: ``` Stream Write: 0.615 Mapped Write: 0.050 Stream Read: 0.577 Mapped Read: 0.015 Stream Read/Write: 4.069 Mapped Read/Write: 0.013 ``` **Tester** 使用了模板方法(Template Method)模式,它為匿名內部子類中定義的 `test()` 的各種實現創建一個測試框架。每個子類都執行一種測試,因此 `test()` 方法還提供了執行各種I/O 活動的原型。 雖然映射的寫似乎使用 **FileOutputStream**,但是文件映射中的所有輸出必須使用 **RandomAccessFile**,就像前面代碼中的讀/寫一樣。 請注意,`test()` 方法包括初始化各種 I/O 對象的時間,因此,盡管映射文件的設置可能很昂貴,但是與流 I/O 相比,總體收益非常可觀。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看