私は IO についていくつかの調査を行っていましたが、バッファリング手法について説明している次の記事を読みました。基盤となるオペレーティング システムによるディスク アクセスと作業を最小限に抑えるために、バッファリング手法では、読み取り操作のたびにディスクから直接データを読み取るのではなく、チャンク単位でデータを読み取る一時バッファーを使用します。
バッファリングなしとありの例を示しました。
バッファリングなし:
try
{
File f = new File("Test.txt");
FileInputStream fis = new FileInputStream(f);
int b; int ctr = 0;
while((b = fis.read()) != -1)
{
if((char)b== '\t')
{
ctr++;
}
}
fs.close();
// not the ideal way
} catch(Exception e)
{}
バッファリングあり:
try
{
File f = new File("Test.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bs = new BufferedInputStream(fis);
int b;
int ctr = 0;
while((b =bs.read()) != -1)
{
if((char)b== '\t')
{
ctr++;
}
}
fs.close(); // not the ideal way
}
catch(Exception e){}
結論は次のとおりです。
Test.txt was a 3.5MB file
Scenario 1 executed between 5200 to 5950 milliseconds for 10 test runs
Scenario 2 executed between 40 to 62 milliseconds for 10 test runs.
Javaでこれを行うより良い方法は他にありますか? または、パフォーマンスを向上させる他の方法/テクニックはありますか?アドバイスしてください..!