I/O流性能比拼
来源:互联网 发布:java环境配置 编辑:程序博客网 时间:2024/05/29 13:20
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(); } }}
0 0
- I/O流性能比拼
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- 服务端 I/O 性能大比拼:Node、PHP、Java 和 Go
- 服务端 I/O 性能大比拼:Node、PHP、Java 和 Go
- 服务端I/O性能大比拼,显然Go是冠军 其次Java、Node 最后是PHP
- 磁盘 I/O 性能
- 磁盘 I/O 性能
- 调整 Java I/O 性能
- 调整 Java I/O 性能
- 调整 Java I/O 性能
- 磁盘 I/O 性能(ZT)
- OAF学习笔记-不基于EO的数据处理
- C 标准库 strstr 函数的实现
- Idea 15 64位安装
- LIGHTOJ-1043 -Triangle Partitioning
- POJ3295——Tautology
- I/O流性能比拼
- HDU 4325 Flowers(线段树+离散化)
- Android - User Interface
- 【OpenCV】边缘检测:梯度,sobel算子的理解
- ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、
- Introduction to Recommender System 之 Module 4 User-User Collaborative Filtering
- 南大软院大神养成计划第二十一天
- int与float在内存中的存储形式_对比与转换
- SSL/TLS 协议详解