Look into it ~

          present
          隨筆 - 32, 文章 - 0, 評論 - 3, 引用 - 0
          數據加載中……

          Fast stream reading in Java

          To increase the performance of your Java™ application when reading from an InputStream, there are a few key areas to look into. If possible, don't make any reallocations of memory. Allocate the input buffer once. Let the Java™ VM do the bulk of the reading, i.e. read data in big chunks. Some coding examples show a loop that reads data a small amount at a time. Using a too small buffer is inefficient. A much better technique is to use a relatively large buffer.

          If you know the maximum content length, the most optimal read would be a single line, like this:
          len = instream.read( buf, 0, MAX_BUFFER_SIZE );

          In this case we assume that the buffer has already been allocated with a size set to MAX_BUFFER_SIZE.

          If the content is of varying size, we have to make a tradeoff between performance and memory usage. If you keep your buffer size just above the average content length, then the number of reallocations of the data buffer and the number of reads will be kept at a minimum. There is no defined size of what a large buffer is but a good rule of thumb might be to keep the buffer size at most about 0,5 to 1 MB.

          posted on 2008-08-15 10:11 LukeW 閱讀(146) 評論(0)  編輯  收藏 所屬分類: Tips, Tricks, Hints & Code

          主站蜘蛛池模板: 福清市| 万山特区| 余姚市| 桐庐县| 丰顺县| 兰西县| 扎兰屯市| 垫江县| 宝应县| 惠安县| 岳阳县| 金阳县| 册亨县| 西安市| 噶尔县| 古浪县| 梧州市| 临清市| 西乌珠穆沁旗| 玉龙| 东乡族自治县| 蛟河市| 元朗区| 海原县| 巴南区| 平度市| 阿合奇县| 从江县| 阿鲁科尔沁旗| 策勒县| 巴林左旗| 嘉黎县| 泗洪县| 滨州市| 永昌县| 屏边| 建昌县| 文登市| 和田县| 格尔木市| 洞头县|