缓存与缓冲的区别
緩存與緩存的區(qū)別
1.1、緩沖 Buffer
-
很多時候上層應用的數(shù)據(jù)傳輸效率遠遠大于下次應用,所以要傳輸數(shù)據(jù)就要需要上層應用等待下層應用接收數(shù)據(jù),會浪費很多時間
-
緩沖的作用就是協(xié)調上下層應用之間的性能差異,可以有效減少上層組件對下層組件的等待時間,可提升整個系統(tǒng)的性能
-
當需要把一盆水倒入一個細口瓶時,緩沖區(qū)就像中間的一個漏斗,可以將水(數(shù)據(jù))完完整整且以較高效率的倒入瓶中
1.2、測試
//使用緩沖區(qū)BufferedWriter 將0-1000000的數(shù)字輸出到指定文件 private static void testBuffer() {try {BufferedWriter bw=new BufferedWriter(new FileWriter("textBuffer.txt"));int num = 1000000;long beginTime=System.currentTimeMillis();for(int i=0;i<num;i++){bw.write(i);}long endTime=System.currentTimeMillis();System.out.println(endTime-beginTime);} catch (IOException e) {e.printStackTrace();} }平均用時60ms
//不用緩沖區(qū) 使用普通Writer public static void test() {Writer writer = null;try {writer = new FileWriter("textBuffer.txt");int num = 10000;long beginTime=System.currentTimeMillis();for(int i=0;i<num;i++){writer.write(i);}long endTime=System.currentTimeMillis();System.out.println(endTime-beginTime);} catch (IOException e) {e.printStackTrace();}finally {try {writer.close();} catch (IOException e) {e.printStackTrace();}} }用時138ms左右
1.3、BufferWriter 的兩個構造函數(shù)
BufferedWriter(Writer writer); BufferedWriter(Writer writer,int size);- BufferedWriter 默認創(chuàng)建8KB的緩沖區(qū)
- BufferedWriter 中的size參數(shù)即可以自己指定緩沖大小
2.1、緩存 Cache
緩存是為了提升系統(tǒng)性能而開辟的一塊空間,與緩沖不同的是,緩存是將被反復使用的數(shù)據(jù)存儲起來,供程序直接調用,避免程序反復的從數(shù)據(jù)庫中讀取相同的數(shù)據(jù)。
存 Cache
緩存是為了提升系統(tǒng)性能而開辟的一塊空間,與緩沖不同的是,緩存是將被反復使用的數(shù)據(jù)存儲起來,供程序直接調用,避免程序反復的從數(shù)據(jù)庫中讀取相同的數(shù)據(jù)。
總結
- 上一篇: 张益博计算机学院,张 益
- 下一篇: 【心得】白嫖网站