[Design optimization]-use buffer to improve program performance

Source: Internet
Author: User

A buffer zone is a specific memory area. The purpose of opening up a buffer is to mitigatePerformance differencesTo improve system performance.

The buffer can coordinate the performance differences between the upper and lower components. When the performance of the Upper-layer components is lower-layer components, the wait time for the upper-layer components to the lower-layer components can be effectively reduced. For example, in the operating system, in order to improve the contradiction between the speed of the CPU and the I/O device does not match, the buffer is set, and the data output by the program is first sent to the buffer for temporary storage, it is then processed by the I/O device. At this time, the CPU does not have to wait and can continue to run the program. Parallel work between CPU and I/O devices is realized. In fact, any place where the arrival rate of data is different from the departure rate, you can set a buffer to ease the conflict of speed mismatch between them.

The most frequently used buffer scenario is to increase the I/O speed. Therefore, many I/O components in JDK provide the buffer function. For exampleFilewriterThe code for file write operations is as follows:

public class BufferTest {public static final int CIRCLE = 100000;public static void test1() throws Exception {Writer writer = new FileWriter(new File("file1.txt"));for (int i = 0; i < CIRCLE; i++) {writer.write(i);}writer.close();}public static void test2() throws Exception {Writer writer = new BufferedWriter(new FileWriter(new File("file2.txt")));for (int i = 0; i < CIRCLE; i++) {writer.write(i);}writer.close();}public static void main(String[] args) throws Exception {test1();test2();}}

Test1 () uses the normal filewriter class to write files, while Test2 () uses bufferedwriter to add? The cache function is provided.

Bufferedwriter has two constructors:

public BufferedWriter(Writer out)public BufferedWriter(Writer out, int sz) 

The first constructor constructs an 8 K buffer. In general, the buffer zone should not be too small. A buffer zone that is too small cannot be used as a real buffer, and the buffer zone cannot be too large. A buffer zone that is too large will waste system memory? GC burden.

The second constructor agrees to manually specify the buffer size.

Execute two methods respectively, and use tptp to analyze the execution time:




It can be observed that the time spent on writing files using the buffer zone is different from that of the files not using the buffer zone.3 ~ 5 times.

Another practical buffer component isBufferedoutputstream. It can wrap all outputstreams and provide them with a buffer function to improve the efficiency of the output stream. Similar to bufferedwriter, it also provides two Constructors

public BufferedOutputStream(OutputStream out)public BufferedOutputStream(OutputStream out, int size)

The function is similar to the bufferedwriter mentioned above.

In addition to improving I/O performance, the buffer zone can be very effective for any scenario where the upper and lower components have performance differences. For example, A plotting program can use a memory buffer to plot the image in the memory and then display the entire image at a time.

In addition to performance optimization, the buffer zone can also be used as a communication tool between the upper and lower layers. This decouples the upper and lower components to optimize the design structure. For example, the producer consumer mode is used to connect the buffer zone between the producer and the consumer.

[Design optimization]-use buffer to improve program performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.