A game that is being developed, because when reading the map, the picture is cut, so the speed is relatively slow. (When I develop a game, I didn't load the cutting picture when I read the map. The speed is very fast, it seems that the speed of IO operation and CreateImage are negligible compared to negligibility), which may not increase significantly for IO's optimization. Speed, but I still tried it.
The code is analyzed, and in the initial code, in order to easily read the various types of data, use the DataInputStream to interface InputStream. But I took a closer look at the data I read, and it was BYTE. The only char is also used by two bytes. This, DataInputStream seems to be unnecessary. So I did an experiment. When I didn't change the map consumption 1242ms, DataInputStream removed directly using the InputStream time-consuming 1065ms, although the results of each trial were slightly different, but probably saved 200ms.
Can you speed up? After observing the code, I found that the data is read through multiple read operations. Will too frequent IO operations reduce speed? If you use a byte array to buffer once to read data, will it be quickly? Well, try it to know. But how do I know the size of a stream? InputStream Avaliable method always returns -1! Open the two streams, first calculate the size first? Yes, there is still a method. Write the file directly to the file. The map file is generated with your own editor, knowing the size is easy. So I recorded the size of the file in front of the file, first read 2 Byte from the stream, get the file size, then use the read (Byte [], int, int, "to get the entire stream to buffer in. All of my data operations are then read from the buffer. Ok, test, the result is: 1154ms. A. slow nearly 100ms. It turns out that this guess is wrong. the reason? Maybe just know the mechanism of KVM.
After getting the problem, I feel that the TRY block read the file is too big because it is to process the data while reading the data, so the TRY block becomes large. The TRY block will increase the size of the Class file. So I use a method to read byte's operation package, of course, this method is to declare the private stat, but can not be inline, only the compiler and KVM know. In this method, the TRY, CATCH structure is used inside the stream, which makes a large TRY block disperse into several small TRY blocks. Test, time consuming 1089ms, oh, still slow. Now that the speed is higher than the space, it is basically ignored after the 10 bytes of the TRY block saves 10 bytes. So this optimization has failed.
Small knot: Do not use complex flow when you can use simple streams, don't trust the theoretical statement, only try it.
Note: The test data is the real-machine test data of the Nokia3100 mobile phone. This data is smaller on the NOKIA 3300, the fastest more than 800 MS