[Design optimization]-use Buffer to improve program performance and buffer
A buffer zone is a specific memory area. The purpose of opening up a buffer is to mitigatePerformance differencesTo improve system performance.
Buffering can coordinate the performance differences between upper-layer components and lower-layer components. When the performance of the Upper-layer component is lower-layer, the wait time for the upper-layer component to the lower-layer component 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, you can continue to execute the program. Parallel work between CPU and I/O devices is realized. In fact, whenever the data arrival rate is different from the departure rate, you can set a buffer to ease the conflict between the speed mismatch between them.
The most common scenario of buffering 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 caching function for FileWriter objects.
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 the system memory and increase the GC burden.
The second constructor allows you to manually specify the buffer size.
Run the following two methods:
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 useful buffer component isBufferedOutputStream. It can be used to encapsulate 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 there is a performance difference between the upper and lower layers of components. For example, A plotting program can use a memory buffer to draw a picture in the memory and then display the entire image at a time.
In addition to performance optimization, the buffer 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 of the producer and consumer.
How to optimize java programming and coding to improve java Performance
The following provides some methods and skills that are often used in JAVA program design and coding to improve JAVA program performance:
1. Object generation and size adjustment.
A common problem in JAVA programming is that the functions provided by the JAVA language are not used properly, so a large number of objects (or instances) are often generated ). Because the system not only takes time to generate objects, it may take time to recycle and process these objects. Therefore, generating too many objects will greatly affect the program performance.
Example 1: String, StringBuffer, +, and append
The JAVA language provides operations for String-type variables. However, improper use may affect the program performance. The following statement:
String name = new String ("HuangWeiFeng ");
System. out. println (name + "is my name ");
It seems to have been quite streamlined, but not actually. To generate binary code, perform the following steps and operations:
(1) generate a new String (STR_1 );
(2) copy the string;
(3) load the String constant "HuangWeiFeng" (STR_2 );
(4) Constructor );
(5) Save the string to the array (starting from position 0 );
(6) obtain the static out variable from the java. io. PrintStream class;
(7) generate a new string buffer variable new StringBuffer (STR_BUF_1 );
(8) copy the buffer variable of the string;
(9) Call the Constructor );
(10) Save the string to the array (starting from position 1 );
(11) using STR_1 as the parameter, call the append method in the string buffer class;
(12) load the String constant "is my name" (STR_3 );
(13) using STR_3 as the parameter, call the append method in the string buffer class;
(14) execute the toString command for STR_BUF_1;
(15) Call the println method in the out variable to output the result.
The two simple lines of code generate five object variables STR_1, STR_2, STR_3, STR_4, and STR_BUF_1. The instances of these generated classes are generally stored in the heap. The heap needs to initialize the super classes and class instances of all classes, and also call the framework of every super class of the class. These operations consume a lot of system resources. Therefore, it is necessary to limit the generation of objects.
After modification, the above Code can be replaced with the following code.
StringBuffer name = new StringBuffer ("HuangWeiFeng ");
System. out. println (name. append ("is my name."). toString ());
The system will perform the following operations:
(1) generate a new string buffer variable new StringBuffer (STR_BUF_1 );
(2) copy the buffer variable of this string;
(3) load the String constant "HuangWeiFeng" (STR_1 );
(4) Call the Constructor );
(5) Save the string to the array (starting from position 1 );
(6) obtain the static out variable from the java. io. PrintStream class;
(7) load the remaining full text of ST...>
What is the template Buffer)
Template buffering is similar to deep buffering. In fact, it uses a part of the deep buffer (therefore, the deep buffer is often called the depth-stencer buffer ). Deep buffering allows programmers to set a template function test reference template value-a global value-each time the pixel is drawn when the value is still buffered in the template.
The template test result determines whether the pixel color value is written to the rendering target, and whether the pixel depth value is written to the depth buffer.
For example, when the reference template value is 0, some objects are drawn to the scene, and the template buffer is cleared to 1, the template buffer is 0 when these objects are drawn. If you set the reference value to 1 and StencilFunction to CompareFunction. LessEqual, only pixels in the corresponding regions whose template value is not 0 will be drawn. This is a basic usage of using template buffer to create a restricted area for the current painting.
There are many more advanced usage of template buffering. In addition to replacement or discard operations, StencilOperations can also be specified to reduce or increase template buffering after each template test, this can be combined with the StencilMask value to ensure that the template test only applies to a part of the template buffer area.
To use template buffering, DepthFormat must retain some bytes for template buffering. The DepthFormat. Depth24Stencil8 depth format uses 8 bits for a template buffer. When you combine the format with the RenderState. StencilMask attribute, it provides eight different template buffers. The Depth24Stencil4 depth format uses 4 bits for template buffering, while the DepthFormat. Depth15Stencil1 format only allows 1 bits. You can pass ClearOptions. StencilBuffer to GraphicsDevice. Clear to Clear the template buffer separately.
You can use the DepthStencilBuffer class to create your own depth-stencer buffer. When creating a custom rendering target, you need to create your own depth-stencer buffer.
The depth buffer is a buffer of the same size as your rendering target, which records the depth of each pixel.
When a pixel is drawn for the second time-for example, when an object is drawn after another object-the depth buffer either retains the previous depth value, either use the depth value of the second pixel to replace the current depth value. The depth reserve which depth discard depends on the depth function you choose. For example, if the current depth function is CompareFunction. LessEqual, only values smaller than or equal to the current depth value will be retained, and values greater than the current depth value will be discarded. This is called a deep test. A deep test is performed every time pixels are drawn. When a pixel is tested in depth, its color is written to the rendering target, and its depth is written to the depth buffer.
The depth value of a pixel is determined by the view matrix and projection matrix. The pixel depth value on the near cut plane is 0, and the pixel depth value on the far cut plane is 1. Every object in a scenario needs to be drawn, and the pixels closest to the camera are usually retained, which blocks the visibility of the objects following them.
The depth buffer usually contains stencel bits, so the depth buffer is also called the depth-stencel buffer. The depth format indicates the structure of the depth buffer. The depth buffer is always 32 bits, but can be combined in different ways, similar to the texture format. Common Depth & #266 ...... remaining full text>