您的位置:首页 > 其它

I/O流性能比拼

2015-12-06 17:44 381 查看
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  性能