I/O流性能比拼

本文介绍了一个 Java NIO 的文件映射 I/O 性能测试程序,通过对比不同 I/O 操作(如 Stream、Channel 和 Mapped I/O)的效率,展示了文件读写操作的不同实现方式及其性能表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.nio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class MappedIo
{
    private static int numOfInts = 4000000;

    private static int numOfUbuffInts = 200000;

    private abstract static class Tester
    {
        private String name;

        public Tester(String name)
        {
            this.name = name;
        }

        public void runTest()
        {
            System.out.print(name + " : ");
            try
            {
                // 一秒的10亿分之一,即等于10的负9次方秒
                long start = System.nanoTime();
                test();
                double duration = System.nanoTime() - start;
                System.out.format("%.2f\n", duration / 1.0e9);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        public abstract void test()
            throws Exception;

    }

    private static Tester[] tests = {

    new Tester("Stream Writer")
    {
        public void test()
            throws Exception
        {
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("temp.tmp"))));

            for (int i = 0; i < numOfInts; i++)
            {
                dos.writeInt(i);
            }
            dos.close();
        }
    }, new Tester("channel writer")
    {
        @SuppressWarnings("resource")
        public void test()
            throws Exception
        {
            FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
            ByteBuffer bb = ByteBuffer.allocate(10000);

            for (int i = 0; i <= numOfInts; i = i + 10000)
            {
                for (int j = 0; j < 10000; j++)
                {
                    bb.asIntBuffer().put(i);
                }
                fc.write(bb);
                bb.flip();
                bb.clear();
            }
            fc.close();
        }
    }, new Tester("mapped writer")
    {
        @SuppressWarnings("resource")
        public void test()
            throws Exception
        {
            FileChannel fc = new RandomAccessFile(new File("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);
            }
            fc.close();
        }
    }, new Tester("Stream read")
    {
        public void test()
            throws Exception
        {
            DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("temp.tmp"))));
            for (int i = 0; i < numOfInts; i++)
            {
                dis.readInt();
            }
            dis.close();
        }
    }, new Tester("channel read")
    {
        @SuppressWarnings("resource")
        public void test()
            throws Exception
        {
            FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
            ByteBuffer bb = ByteBuffer.allocate(10000);
            while (fc.read(bb) != -1)
            {
                bb.flip();
                bb.clear();
            }
            fc.close();
        }
    }, new Tester("mapped read")
    {
        @SuppressWarnings("resource")
        public void test()
            throws Exception
        {
            FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
            IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
            while (ib.hasRemaining())
            {
                ib.get();
            }
            fc.close();
        }
    }, new Tester("Stream read/writer")
    {
        public void test()
            throws Exception
        {
            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());
            }
            raf.close();
        }
    }, new Tester("channel read/writer")
    {
        @SuppressWarnings("resource")
        public void test()
            throws Exception
        {
            FileChannel in = new FileInputStream("temp.tmp").getChannel(), out = new FileOutputStream("temp.tmp2").getChannel();

            ByteBuffer buff = ByteBuffer.allocate(10000);// 关乎性能
            while (in.read(buff) != -1)
            {
                buff.flip();
                out.write(buff);
                buff.clear();
            }

            in.close();
            out.close();
        }
    }, new Tester("mapped read/writer")
    {
        @SuppressWarnings("resource")
        public void test()
            throws Exception
        {
            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));
            }
            fc.close();
        }
    }};

    public static void main(String[] args)
    {
        for (Tester test : tests)
        {
            test.runTest();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值