`

不同方式读写文件的性能比较

DOS 
阅读更多

注:bufferedRead读写的速度最快

package com.test;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class Test {
	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 long runTest() {
			System.out.print(name + ":   ");
			try {
				long startTime = System.currentTimeMillis();
				test();
				long endTime = System.currentTimeMillis();
				return (endTime - startTime);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		public abstract void test() throws IOException;
	}

	private static Tester[] tests = { new Tester("Stream   Write") {
		public void test() throws IOException {
			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("Mapped   Write") {
		public void test() throws IOException {
			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);
			fc.close();
		}
	}, new Tester("Stream   Read") {
		
		public void test() throws IOException {
			DataInputStream dis = new DataInputStream(new BufferedInputStream(
					new FileInputStream("temp.tmp")));
			for (int i = 0; i < numOfInts; i++)
				dis.readInt();
			dis.close();
		}
	}, new Tester("Mapped   Read") {
		public void test() throws IOException {
			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();
			fc.close();
		}
	}, new Tester("BufferReader   Read") {
		public void test() throws IOException {
			File file = new File("temp.tmp");
			InputStreamReader read = new InputStreamReader(
			new FileInputStream(file), "UTF-8");
			BufferedReader reader = new BufferedReader(read);
			String line;
			while ((line = reader.readLine()) != null){
				
			}
			read.close();
		}
	}, new Tester("Stream   Read/Write") {
		public void test() throws IOException {
			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("Mapped   Read/Write") {
		public void test() throws IOException {
			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 (int i = 0; i < tests.length; i++)
			System.out.println(tests[i].runTest());
	}
 
分享到:
评论

相关推荐

    文件读写文件读写文件读写

    用C语言实现的简单的文件读写,,适合C初学者,,,熟悉文件读写操作,

    Properties文件读写;Property文件读写;Property

    读/写属性文件的工具类. PropertyUtil.java对Property文件读写进行了封装, 使开发人员对Property文件的读写更加容易。 在性能、实用性 方面还是可以的。

    linux下读写日志文件

    从执行体程序库中的CLLogger类可知,通过缓存要写入文件中的数据,能够提高读写磁盘的性能。编写一个文件操作的封装类,调用该类的写操作接口时,数据要首先写到缓存,然后再根据策略写到文件中。调用该类的读操作...

    Linux下各种主要文件系统的读写性能测试

    本文介绍了Linux下各种主要文件系统的读写性能测试方法。

    多线程文件读写测试

    这是个关于多线程文件读写测试的源代码,来源于网路!可以作为文件读写加速的典型事例!

    Java读写文件API的用法指南,性能分析与对比。

    NULL 博文链接:https://hayesfrank.iteye.com/blog/1220974

    板卡文件系统性能测试

    1 板卡linux操作系统下文件系统性能测试 2 通用Makefile模板。

    Android读取Txt文件源码及性能分析

    Andriod读取Txt文件,并且统计单词出现的个数。偷了下懒没用listview

    STM32F429+SFUD+LittleFS实现文件系统文件读写

    单片机使用正点原子的STM32F429核心板,移植了SFUD来驱动spiFlash,移植了LittleFS文件系统,实现官方的demo,每次main运行时更新一个名为boot_count的文件,程序可以在任何时候被中断,而不会丢失它已经启动了多少...

    c语言实现ini文件读写操作

    此外,由于C语言的接近硬件的特性,该读写操作也具有较高的性能,能够在资源受限的环境中可靠地工作。 总之,C语言实现的INI文件读写操作为软件开发提供了一种简单、高效且可靠的配置管理解决方案。无论是对于初学...

    简单的python创建读写文件脚本

    简单的python创建读写文件脚本,给os模块中的linesep起了给别名ls,这样做的好处一方面简化了长长的变量名,另一方面也是主要原因用于提高代码性能,因为访问这个变量时首先要检测os模块,然后再解析linesep,...

    文件系统性能测试工具iozone

    iozone是一个文件系统的benchmark工具,可以测试不同的操作系统中文件系统的读写性能。 可以测试 Read, write, re-read,re-write, read backwards, read strided, fread, fwrite, random read, pread, mmap, aio_...

    C# 配置文件存储 各种序列化算法性能比较 程序1.1

    本文比较多个方式进行配置文件的存储,对比各个不同算法的读写性能。 在应用软件启动的时候,需要读取配置文件,但是启动的性能很重要,所以需要有一个很快的读取配置文件的方法 这就是本文使用的程序,请使用 ...

    worker_manager.rar_文件读写

    简易职工管理系统,没有做界面,只是控制台,但性能比较稳定,读写文件而不是数据库,读写速度快。

    编写带缓存的文件操作类

    从执行体程序库中的CLLogger类可知,通过缓存要写入文件中的数据,能够提高读写磁盘的性能 请编写一个文件操作的封装类,其要求如下: 需要提供open/read/write/lseek/close等函数的封装函数 该类要提供数据缓存服务...

    驱动级磁盘级文件读写完整代码

    (编译成sys后作为服务加载)实现驱动级别磁盘读写

    NTFS文件附加数据流读写类

    摘要:NTFS是Microsoft公司开发的一种有着良好安全性和稳定性的高性能文件系统,NTFS的文件或文件夹中附加多个额外的数据流,但是其访问一直没有很好的解决办法,本文使用VB2003实现NTFS文件附加数据流的读写类,...

    java IO流读写

    InputStream和OutputStream 1.DataInputStream,DataOutputStream 从Stream里读取基本类型的数据 BufferedReader,BufferedWriter...2.BufferedInputStream,BufferedOutputStream 封装了从缓冲区读写指定数量数据的方法

    WinCE 磁盘IO读写性能测试工具(含参考代码文件)

    从SD卡选择一个大的文件,然后程序就会开启一个线程读取,并把读取的进度、速度显示到界面。压缩包中含主要的测试代码。

    Java文件读写IO/NIO及性能比较详细代码及总结

    主要介绍了Java文件读写IO/NIO及性能比较详细代码及总结,具有一定借鉴价值,需要的朋友可以参考下。

Global site tag (gtag.js) - Google Analytics