Thanks Buffered versus NonBuffered Output Compare the speed

Thanks!
Buffered versus Non-Buffered Output Compare the speed of buffered versus non-buffered output in the Byte Stream classes (as we did in the text for the Character Stream classes). Devise a program that writes 50,000 characters to two different files, once using PrintStream alone and once using PrintStreamwrapped with BufferedOutputStream. Compare execution times

Solution

\"Stream\" uses a system call for each byte to perform write operation which is inefficient and takes more time. Whereas a \"Buffer\" takes thousands of bytes at a time a flushes them into the destination file at one go, which is efficient and takes less amount of time when compared to \"Stream\"

The following code prints the time taken in seconds to perform writer operation for both \"PrintStream\" and \"BufferedOutputStream\".

package com.chegg.writertimes;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

public class CompareTimes {

   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
      
      
       /*====================================================Without buffer==========================================================*/
       FileOutputStream fout=new FileOutputStream(\"C:/Users/RaakiGadde/Desktop/Chegg/CompareTimes/withoutbuffer.txt\");
       PrintStream pout=new PrintStream(fout);
       StringBuilder str=new StringBuilder();
       for(int i=1;i<=50000;i++){
           str.append(\"A\");
       }
       String str_temp=str.toString();
       char[] ch=str_temp.toCharArray();
      
       /*--------------------------------Calculating execution time to write witout buffer----------------------------------------*/
       double time1=System.currentTimeMillis();
       pout.print(ch);
       pout.close();
       double time2=System.currentTimeMillis();
       System.out.println((time2-time1)/1000);           //time taken in seconds
       /*-------------------------------------------------------------------------------------------------------------------------*/
       /*==========================================================================================================================*/
      
      
       /*====================================================With buffer==========================================================*/
       FileOutputStream fout1=new FileOutputStream(\"C:/Users/RaakiGadde/Desktop/Chegg/CompareTimes/withbuffer.txt\");
       PrintStream pout1=new PrintStream(fout1);
       BufferedOutputStream bout=new BufferedOutputStream(pout1);
      
       StringBuilder str1=new StringBuilder();
       for(int i=1;i<=50000;i++){
           str1.append(\"A\");
       }
       String str_temp1=str.toString();
       byte b1[]=str_temp1.getBytes();
      
       /*--------------------------------Calculating execution time to write witout buffer----------------------------------------*/
       double time3=System.currentTimeMillis();
       bout.write(b1);
       bout.flush();
       bout.close();
       double time4=System.currentTimeMillis();
       System.out.println((time4-time3)/1000);           //time taken in seconds
       /*-------------------------------------------------------------------------------------------------------------------------*/
       /*==========================================================================================================================*/
      
   }

}

Thanks! Buffered versus Non-Buffered Output Compare the speed of buffered versus non-buffered output in the Byte Stream classes (as we did in the text for the C
Thanks! Buffered versus Non-Buffered Output Compare the speed of buffered versus non-buffered output in the Byte Stream classes (as we did in the text for the C

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site