ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
เขียนโปรแกรมบน iPhone ด้วย MonoTouch
News
Php Tips
Ubuntu
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
C# Design Pattern
Linux Quick Tips
C# Tips & Technique
C# using Linq น่าใช้จริงๆ
Java & JavaScript Tips
MAVEN
Database & SQL
ZengCode Framework Guide
Mac OSx
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     JAVA : Buffering usually appropriate  (2011-11-10)

Buffering usually appropriate

Buffering input/output มักจำเป็นในการใช้งาน และควรจะหัดให้เป็นนิสัยนะครับ

Unbuffered input/output classes มักจะทำงานกับ 1 byte ต่อหนึ่งช่วงเวลา การใช้ buffer จะช่วยให้ performmance ดีขึ้น

Example runy ตัวอย่างไฟล์ข้างล่าง , ถ้าใช้ buffer จะเร็วขึ้นประมาณ 3 เท่า.
Size -  624 bytes :
With buffering: 10 ms
Without buffering: 30 ms

Size - 10,610 bytes :
With buffering: 30 ms
Without buffering: 80 ms

Size - 742,702 bytes :
With buffering: 180 ms
Without buffering: 741 ms
 

import java.io.*;

public final class BufferDemo {

  public static void main (String... aArguments) {
    File file = new File("C:\\Temp\\blah.txt");
    verifyFile( file );

    Stopwatch stopwatch = new Stopwatch();

    stopwatch.start();
    readWithBuffer( file );
    stopwatch.stop();
    System.out.println("With buffering: " + stopwatch);

    stopwatch.start();
    readWithoutBuffer( file );
    stopwatch.stop();
    System.out.println("Without buffering: " + stopwatch);
  }

  /**
* @param aFile is a file which already exists and can be read.
*/
static public void readWithBuffer(File aFile) { try { //use buffering, with default buffer size of 8K
Reader input = new BufferedReader( new FileReader(aFile) ); try { int data = 0; while ((data = input.read()) != -1){ //do nothing
} } finally { input.close(); } } catch (IOException ex){ ex.printStackTrace(); } } /**
* @param aFile is an existing file which can be read.
*/
static public void readWithoutBuffer(File aFile) { try { Reader input = new FileReader( aFile ); try { //do not use buffering
int data = 0; while ((data = input.read()) != -1){ //do nothing
} } finally { input.close(); } } catch (IOException ex){ ex.printStackTrace(); } } private static void verifyFile( File aFile ) { if (aFile == null) { throw new IllegalArgumentException("File should not be null."); } if (!aFile.exists()) { throw new IllegalArgumentException ("File does not exist: " + aFile); } if (!aFile.isFile()) { throw new IllegalArgumentException("Should not be a directory: " + aFile); } if (!aFile.canWrite()) { throw new IllegalArgumentException("File cannot be written: " + aFile); } } }


Comment
Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.094228 seconds to load.