How efficient is the Java byte buffer stream? Test it

How efficient is the Java byte buffer stream? Test it

Blog description

The information involved in this article comes from Internet collation and personal summary, which means personal learning and experience summary. If there is any infringement, please contact me to delete, thank you!

Explain

It's said that buffer flow is very efficient, so let's test it today

Basic flow

Use normal byte stream

Code
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Buffered
 * @date 2020/4/24 4:16 Afternoon
 */
public class Buffered {
    public static void main(String[] args) throws FileNotFoundException {
        long start = System.currentTimeMillis();   //Get millisecond value
        try (
                FileInputStream fis = new FileInputStream("azlg.zip");
                FileOutputStream fos = new FileOutputStream("copy.zip")
        ) {
            // Read and write data
            int b;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();   //Get millisecond value
        System.out.println("Normal stream copy time:" + (end - start) + "Millisecond");
    }
}

time

Buffer stream

Use byte buffer stream to copy files

Code
import java.io.*;

/**
 * @author Buffered
 * @date 2020/4/24 4:16 Afternoon
 */
public class Buffered {
    public static void main(String[] args) throws FileNotFoundException {
        long start = System.currentTimeMillis();   //Get millisecond value
        try (
                BufferedInputStream fis = new BufferedInputStream(new FileInputStream("azlg.zip"));
                BufferedOutputStream fos = new BufferedOutputStream( new FileOutputStream("copy.zip"))
        ) {
            // Read and write data
            int b;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();   //Get millisecond value
        System.out.println("Buffer stream copy time:" + (end - start) + "Millisecond");
    }
}

time

Buffer stream plus byte array

Using byte array while using buffer stream can improve the speed of replication

Code
import java.io.*;

/**
 * @author Buffered
 * @date 2020/4/24 4:16 Afternoon
 */
public class Buffered {
    public static void main(String[] args) throws FileNotFoundException {
        long start = System.currentTimeMillis();   //Get millisecond value
        try (
                BufferedInputStream fis = new BufferedInputStream(new FileInputStream("azlg.zip"));
                BufferedOutputStream fos = new BufferedOutputStream( new FileOutputStream("copy.zip"))
        ) {
            // Read and write data
            int b;
            byte[] bytes = new byte[8*1024];
            while ((b = fis.read(bytes)) != -1) {
                fos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();   //Get millisecond value
        System.out.println("Buffer stream uses byte array copy time:" + (end - start) + "Millisecond");
    }
}

time

Result

By analyzing the execution time of three pieces of code, using buffer stream can greatly increase the speed of file copying, and using Shuyong byte array can greatly improve the speed of file copying

A 5.7MB file with a speed of 34589-186-10ms

Thank

Black horse programmer

And the industrious self

Tags: Programming Java

Posted on Fri, 24 Apr 2020 08:53:26 -0700 by articlesocial