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(); } } }
相关文章推荐
- 选定虚拟主机 性能凸显优势
- 修改一行代码提升 Postgres 性能 100 倍
- 推荐Sql server一些常见性能问题的解决方法
- SQL Server误区30日谈 第9天 数据库文件收缩不会影响性能
- 和表值函数连接引发的性能问题分析
- SQLServer 2000 升级到 SQLServer 2008 性能之需要注意的地方之一
- 数据库性能优化三:程序操作优化提升性能
- VBS中的字符串连接的性能问题
- mysql 性能的检查和调优方法
- 数据库性能优化二:数据库表优化提升性能
- 如何用分表存储来提高性能 推荐
- ASP中使用FileSystemObject时提高性能的方法
- 如何改进javascript代码的性能
- JavaScript脚本性能优化注意事项
- JQuery Tips(4) 一些关于提高JQuery性能的Tips
- jQuery性能优化28条建议你值得借鉴
- 十个迅速提升JQuery性能让你的JQuery跑得更快
- jquery选择器的选择使用及性能介绍
- 对于jQuery性能的一些优化建议
- 做好七件事帮你提升jQuery的性能