Java顺序IO性能
編碼
import java.io.*; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel;import static java.lang.Integer.MAX_VALUE; import static java.lang.System.out; import static java.nio.channels.FileChannel.MapMode.READ_ONLY; import static java.nio.channels.FileChannel.MapMode.READ_WRITE;public final class TestSequentialIoPerf {public static final int PAGE_SIZE = 1024 * 4;public static final long FILE_SIZE = PAGE_SIZE * 2000L * 1000L;public static final String FILE_NAME = "test.dat";public static final byte[] BLANK_PAGE = new byte[PAGE_SIZE];public static void main(final String[] arg) throws Exception{preallocateTestFile(FILE_NAME);for (final PerfTestCase testCase : testCases){for (int i = 0; i < 5; i++){System.gc();long writeDurationMs = testCase.test(PerfTestCase.Type.WRITE,FILE_NAME);System.gc();long readDurationMs = testCase.test(PerfTestCase.Type.READ,FILE_NAME);long bytesReadPerSec = (FILE_SIZE * 1000L) / readDurationMs;long bytesWrittenPerSec = (FILE_SIZE * 1000L) / writeDurationMs;out.format("%s\twrite=%,d\tread=%,d bytes/sec\n",testCase.getName(),bytesWrittenPerSec, bytesReadPerSec);}}deleteFile(FILE_NAME);}private static void preallocateTestFile(final String fileName)throws Exception{RandomAccessFile file = new RandomAccessFile(fileName, "rw");for (long i = 0; i < FILE_SIZE; i += PAGE_SIZE){file.write(BLANK_PAGE, 0, PAGE_SIZE);}file.close();}private static void deleteFile(final String testFileName) throws Exception{File file = new File(testFileName);if (!file.delete()){out.println("Failed to delete test file=" + testFileName);out.println("Windows does not allow mapped files to be deleted.");}}public abstract static class PerfTestCase{public enum Type { READ, WRITE }private final String name;private int checkSum;public PerfTestCase(final String name){this.name = name;}public String getName(){return name;}public long test(final Type type, final String fileName){long start = System.currentTimeMillis();try{switch (type){case WRITE:{checkSum = testWrite(fileName);break;}case READ:{final int checkSum = testRead(fileName);if (checkSum != this.checkSum){final String msg = getName() +" expected=" + this.checkSum +" got=" + checkSum;throw new IllegalStateException(msg);}break;}}}catch (Exception ex){ex.printStackTrace();}return System.currentTimeMillis() - start;}public abstract int testWrite(final String fileName) throws Exception;public abstract int testRead(final String fileName) throws Exception;}private static PerfTestCase[] testCases ={new PerfTestCase("RandomAccessFile"){public int testWrite(final String fileName) throws Exception{RandomAccessFile file = new RandomAccessFile(fileName, "rw");final byte[] buffer = new byte[PAGE_SIZE];int pos = 0;int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){byte b = (byte)i;checkSum += b;buffer[pos++] = b;if (PAGE_SIZE == pos){file.write(buffer, 0, PAGE_SIZE);pos = 0;}}file.close();return checkSum;}public int testRead(final String fileName) throws Exception{RandomAccessFile file = new RandomAccessFile(fileName, "r");final byte[] buffer = new byte[PAGE_SIZE];int checkSum = 0;int bytesRead;while (-1 != (bytesRead = file.read(buffer))){for (int i = 0; i < bytesRead; i++){checkSum += buffer[i];}}file.close();return checkSum;}},new PerfTestCase("BufferedStreamFile"){public int testWrite(final String fileName) throws Exception{int checkSum = 0;OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));for (long i = 0; i < FILE_SIZE; i++){byte b = (byte)i;checkSum += b;out.write(b);}out.close();return checkSum;}public int testRead(final String fileName) throws Exception{int checkSum = 0;InputStream in = new BufferedInputStream(new FileInputStream(fileName));int b;while (-1 != (b = in.read())){checkSum += (byte)b;}in.close();return checkSum;}},new PerfTestCase("BufferedChannelFile"){public int testWrite(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();ByteBuffer buffer = ByteBuffer.allocate(PAGE_SIZE);int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){byte b = (byte)i;checkSum += b;buffer.put(b);if (!buffer.hasRemaining()){buffer.flip();channel.write(buffer);buffer.clear();}}channel.close();return checkSum;}public int testRead(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();ByteBuffer buffer = ByteBuffer.allocate(PAGE_SIZE);int checkSum = 0;while (-1 != (channel.read(buffer))){buffer.flip();while (buffer.hasRemaining()){checkSum += buffer.get();}buffer.clear();}return checkSum;}},new PerfTestCase("MemoryMappedFile"){public int testWrite(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();MappedByteBuffer buffer = channel.map(READ_WRITE, 0,Math.min(channel.size(), MAX_VALUE));int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){if (!buffer.hasRemaining()){buffer = channel.map(READ_WRITE, i,Math.min(channel.size() - i , MAX_VALUE));}byte b = (byte)i;checkSum += b;buffer.put(b);}channel.close();return checkSum;}public int testRead(final String fileName) throws Exception{FileChannel channel = new RandomAccessFile(fileName, "rw").getChannel();MappedByteBuffer buffer = channel.map(READ_ONLY, 0,Math.min(channel.size(), MAX_VALUE));int checkSum = 0;for (long i = 0; i < FILE_SIZE; i++){if (!buffer.hasRemaining()){buffer = channel.map(READ_WRITE, i,Math.min(channel.size() - i , MAX_VALUE));}checkSum += buffer.get();}channel.close();return checkSum;}},}; }結果
 400MB file 
 =========== 
 RandomAccessFile write=294,041,636 read=1,494,890,510 bytes/sec  
 BufferedStreamFile寫入= 98,178,331讀取= 286,433,566字節/秒 
 BufferedStreamFile寫入= 100,244,738讀取= 288,857,545字節/秒 
 BufferedStreamFile寫入= 82,948,562讀取= 154,100,827字節/秒 BufferedStreamFile寫入= 108,503,311讀取= 153,869,271字節/秒 BufferedStreamFile寫入= 113,055,478讀取= 152,608,047字節/秒 
 BufferedChannelFile寫入= 228,443,948讀取= 356,173,913字節/秒 
 BufferedChannelFile寫入= 265,629,053讀取= 374,063,926字節/秒 
 BufferedChannelFile寫= 223,825,136讀= 1,539,849,624字節/秒BufferedChannelFile寫= 232,992,036讀= 1,539,849,624字節/秒BufferedChannelFile寫= 212,779,220讀= 1,534,082,397字節/秒 MemoryMappedFile寫入= 300,955,180讀取= 305,899,925字節/秒 MemoryMappedFile寫入= 313,149,847讀取= 310,538,286字節/秒 MemoryMappedFile寫入= 326,374,501讀取= 303,857,566字節/秒 MemoryMappedFile寫入= 327,680,000讀取= 304,535,315字節/秒 MemoryMappedFile寫入= 326895450讀取= 303632320字節/秒 
 8GB文件 
 ============ 
 RandomAccessFile寫入= 167,402,321讀取= 251,922,012字節/秒 RandomAccessFile寫入= 193,934,802讀取= 257,052,307字節/秒 RandomAccessFile寫入= 192,948,159讀取= 248,460,768字節/秒 RandomAccessFile寫入= 191,814,180讀取= 245,225,408字節/秒 RandomAccessFile寫入= 190,635,762讀取= 275,315,073字節/秒 
 BufferedStreamFile寫入= 154,823,102讀取= 248,355,313字節/秒 
 BufferedStreamFile寫入= 152,083,913讀取= 253,418,301字節/秒 
 BufferedStreamFile寫入= 133,099,369讀取= 146,056,197字節/秒 BufferedStreamFile寫入= 131,065,708讀取= 146,217,827字節/秒 BufferedStreamFile寫入= 132694052讀取= 148116004字節/秒 
 BufferedChannelFile寫入= 186703740讀取= 215075218字節/秒 
 BufferedChannelFile寫入= 190,591,410讀取= 211,030,680字節/秒BufferedChannelFile寫入= 187,220,038讀取= 223,087,606字節/秒 
 BufferedChannelFile寫入= 191,585,397讀取= 221,297,747字節/秒 BufferedChannelFile寫入= 192,653,214讀取= 211,789,038字節/秒 
 MemoryMappedFile寫入= 123,023,322讀取= 231,530,156字節/秒 
 MemoryMappedFile寫入= 121,961,023讀取= 230,403,600字節/秒 
 MemoryMappedFile寫入= 123,317,778讀取= 229,899,250字節/秒 MemoryMappedFile寫入= 121,472,738讀取= 231,739,745字節/秒 MemoryMappedFile寫入= 120,362,615讀取= 231,190,382字節/秒 
分析
多年來,我一直是直接使用RandomAccessFile的忠實擁護者 ,因為它提供了控制和可預測的執行。 從性能的角度來看,我從來沒有發現使用緩沖流會很有用,而且情況似乎仍然如此。 在最近的測試中,我發現使用NIO FileChannel和ByteBuffer的性能要好得多。 使用Java 7,此編程方法的靈活性已得到改善,可以使用SeekableByteChannel進行隨機訪問。 似乎在某些情況下,讀取RandomAccessFile和NIO可以很好地使Memory Mapped文件贏得寫操作。 我看到這些結果因平臺而異。 文件系統,操作系統,存儲設備和可用內存都會產生重大影響。 在某些情況下,我看到內存映射文件的性能明顯優于其他文件,但這需要在您的平臺上進行測試,因為您的行程可能會有所不同…… 推送最大吞吐量時,應特別注意使用內存映射的大文件。 我經常發現操作系統可能由于虛擬內存子系統上的壓力而變得無響應。 結論 從Java執行順序文件IO的不同方法在性能上存在顯著差異。 并非所有方法都遙遙相等。 對于大多數IO,我發現使用ByteBuffers和Channels是IO庫中最優化的部分。 如果緩沖流是您的IO庫的選擇,那么值得進行分支并熟悉Channel和Buffer的實現,甚至可以使用舊的RandomAccessFile進行回退。 參考: Mechanical Sympathy博客上的JCG合作伙伴 Martin Thompson提供的Java順序IO性能 。翻譯自: https://www.javacodegeeks.com/2012/07/java-sequential-io-performance.html
總結
以上是生活随笔為你收集整理的Java顺序IO性能的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 微信渐变国旗头像怎么弄
 - 下一篇: 示廓灯怎么读 示廓灯的读音和解释