Java之nio性能比较
结果:
used time:53574684
used time:1800077620
used time:12563690
可见MappedByteBuffer读写数据是最快的, 其次是FileChannel, 再其次就是RandomAccessFile.
public class BufferedCache {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
test3();
test2();
test1();
}
public static void test1() throws IOException {
long start = System.nanoTime();
FileChannel fc = new RandomAccessFile("/tmp/test.tmp", "rw").getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 8);
for (int i = 0; i < 100000; i++) {
mbb.putLong(0, i);
}
long end = System.nanoTime();
System.out.println("used time:" + (end - start));
fc.close();
}
public static void test2() throws IOException {
long start = System.nanoTime();
RandomAccessFile raf = new RandomAccessFile("/tmp/test.tmp", "rw");
for (int i = 0; i < 100000; i++) {
raf.seek(0);
raf.writeLong(i);
}
long end = System.nanoTime();
System.out.println("used time:" + (end - start));
raf.close();
}
public static void test3() throws IOException {
long start = System.nanoTime();
FileChannel fc = new RandomAccessFile("/tmp/test.tmp", "rw").getChannel();
ByteBuffer buf = ByteBuffer.allocate(8);
for (int i = 0; i < 100000; i++) {
buf.putLong(0, i);
fc.write(buf, 0);
}
long end = System.nanoTime();
System.out.println("used time:" + (end - start));
fc.close();
}
}
最新文章
- Exchange环境搭建心得
- crontab中执行任务定位到秒级
- 使用ContentProvider进行应用程序间的数据交互
- X-UA-Compatible/IE=EmulateIE7/IE=7
- 洛谷P2014 选课 (树形dp)
- 【洛谷P3398】仓鼠找sugar
- ch2 创建和销毁对象
- composer autoload
- java提高篇---HashTable
- VMware 进入bios
- kail新手安装
- poj 2104 划分树
- C语言 打印圣诞树
- 由问题引出的fsck命令
- Jerry的UI5框架代码自学教程
- 【题解】P1119 灾后重建
- JAVA ArrayList实现随机生成数字,并把偶数放入一个列表中
- [书籍]用UWP复习《C#并发编程经典实例》
- Linux的SIGUSR1和SIGUSR2信号
- SSD详解
热门文章
- 使用Perl批量读取文件最后行
- 用live555将内网摄像机视频推送到外网server,附源代码
- iOS开发ARC与MRC下单例的完整写法与通用宏定义
- Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏
- 监听text等的改变事件
- <;Linux>; Xen虚拟机镜像的安装
- ios 第一篇文章-xcode6.2键盘调不出来
- 【codeforces 757B】 Bash's Big Day
- Vert.x ——概述
- 剑指Offer面试题10(Java版):二进制中的1的个数