缓冲Buffer,缓存Cache,池Pool
来源:互联网 发布:mac版芒果tv缓存在哪 编辑:程序博客网 时间:2024/06/05 15:22
缓冲
缓冲可以理解为漏斗一样,是为了解决上下层的性能差异而出现的,缓冲可以有效的减少上层组件对下层组件的等待时间
在JDK中,IO使用缓冲是最多的
import java.io.*;public class Demo {static int len = 100000;static String content = "Hello World !\n";static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";public static void main(String[] args) {Writer writer = null;try {writer = new BufferedWriter(new FileWriter(new File(path)));//默认构造8K的缓冲区} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}long begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {writer.write(content);}writer.close();} catch (IOException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println(end-begin);//-----------------------------------try {writer =new FileWriter(new File(path));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {writer.write(content);}writer.close();} catch (IOException e) {e.printStackTrace();}end = System.currentTimeMillis();System.out.println(end-begin);}}
2760从结果中可以看出来差异了,有明显的性能差异
import java.io.*;public class Demo {static int len = 100000;static String content = "Hello World !\n";static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";public static void main(String[] args) {Reader reader = null;try {reader = new BufferedReader(new FileReader(new File(path)));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}long begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {reader.read();}reader.close();} catch (IOException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println(end-begin);//-----------------------------------try {reader =new FileReader(new File(path));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {reader.read();}reader.close();} catch (IOException e) {e.printStackTrace();}end = System.currentTimeMillis();System.out.println(end-begin);}}
940读取差异
import java.io.*;public class Demo {static int len = 100000;static String content = "Hello World !\n";static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";public static void main(String[] args) {InputStream reader = null;try {reader = new BufferedInputStream(new FileInputStream(new File(path)));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}long begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {reader.read();}reader.close();} catch (IOException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println(end-begin);//-----------------------------------try {reader = new FileInputStream(new File(path));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {reader.read();}reader.close();} catch (IOException e) {e.printStackTrace();}end = System.currentTimeMillis();System.out.println(end-begin);}}
8172差异很明显
import java.io.*;public class Demo {static int len = 100000;static String content = "Hello World !\n";static String path = new File("").getAbsolutePath()+File.separator+"demo.txt";public static void main(String[] args) {OutputStream writer = null;try {writer = new BufferedOutputStream(new FileOutputStream(new File(path)));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}long begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {writer.write(content.getBytes());}writer.close();} catch (IOException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println(end-begin);//-----------------------------------try {writer = new FileOutputStream(new File(path));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}begin = System.currentTimeMillis();try {for(int i=0;i<len;i++) {writer.write(content.getBytes());}writer.close();} catch (IOException e) {e.printStackTrace();}end = System.currentTimeMillis();System.out.println(end-begin);}}
50392从以上四种缓冲类来看,明显比非缓冲类效果更佳
建议JAVA IO操作全部采用缓冲类来实现
缓存
缓存就是存储用户最近最频繁访问的数据,免得用户每次都从主存中读取数据
JAVA中最为简单的缓存就是HashMap实现
池
数据库连接池
- 缓冲Buffer,缓存Cache,池Pool
- Buffer缓冲和Cache缓存的区别
- Buffer Cache Pool
- Buffer Cache(缓冲区缓存)篇:keep pool(保留池)
- Cache(缓存)和 Buffer(缓冲),主要区别是什么?
- SQLServer 2014 缓冲池扩展(Buffer Pool Extension)
- Buffer cache(缓冲区缓存)
- 我对 缓冲(buffer)和 缓存(cache)的理解
- Buffer Cache与Shared Pool原理
- Oracle Buffer Cache 中 Keep Pool 说明
- Oracle Buffer Cache 中的 Recycle Pool 说明
- Oracle Buffer Cache 中的 Recycle Pool 说明
- Oracle Buffer Cache 中 Keep Pool 说明
- Oracle Buffer Cache 中 Keep Pool 说明
- share pool 与buffer cache详细解读
- MySQL Dumping and Reloading the InnoDB Buffer Pool(MySQLdump和重载InnoDB缓冲池)
- mysql内核源代码深度解析 缓冲池 buffer pool 整体概述(bufferpool部分一)
- 【MySQL 5.7 Reference Manual】15.4.1 Buffer Pool(缓冲池)
- 多能源互补的分布式供能系统专题四:正逆耦合循环
- Use Case Diagram(用例图)——UML
- MD5加密
- RCP布局
- nginx 缓存按目录删除
- 缓冲Buffer,缓存Cache,池Pool
- SVN服务器搭建和使用(一)
- LOB SPACE enq HW – contention 等待事件
- 日语敬语小总结
- WCF over JMS(ActiveMQ) 与 gSoap over JMS(ActiveMQ) 通信总结
- OpenCV学习--cvTermCriteria介绍
- 用orabm测试oracle服务器的TPS值
- 转给10几年后的自己看
- Linux下的USB总线驱动(三)