log4jを使ったログ作成と単純なファイル読み書きでJavaプログラムのレイテンシを比較しています
String content = "Writing";
File file = new File("D://filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
long startTimeF=System.nanoTime();
for(int i=0;i<10;i++)
{
bw.write(content);
}
bw.close();
long endTimeF=System.nanoTime();
System.out.println("Time elapsed for file write:"+(endTimeF-startTimeF));
long startTimeL=System.nanoTime();
for(int i=0;i<10;i++)
{
_logger.info("Writing..:"+strDate); }
long endTimeL=System.nanoTime();
System.out.println("Time elapsed for logger ="+(endTimeL-startTimeL));
o/p: ファイル書き込みの経過時間:1826647 ロガーの経過時間 =3909112
そして、私が好きなセリフを交換するとします
長い startTimeL=System.nanoTime();
for(int i=0;i<10;i++)
{
_logger.info("writing..:"+strDate); }
long endTimeL=System.nanoTime();
System.out.println("Time elapsed for logger ="+(endTimeL-startTimeL));
long startTimeF=System.nanoTime();
for(int i=0;i<10;i++)
{
bw.write(content);
}
bw.close();
long endTimeF=System.nanoTime();
System.out.println("Time elapsed for file write:"+(endTimeF-startTimeF));
o/p: ロガーの経過時間 = 4018071 ファイル書き込みの経過時間: 3115210
What cause to get different output by only inter changing lines....(using Asynchronous Logging) ?