关于读取文件和优化性能 [英] Regarding reading a file and optimizing the performance

查看:159
本文介绍了关于读取文件和优化性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对IO进行一些研究,我阅读了以下有关缓冲技术的文章。为了最大限度地减少底层操作系统的磁盘访问和工作,缓冲技术使用一个临时缓冲区,以缓冲方式读取数据,而不是每次读取操作都直接从磁盘读取数据。

I was doing some research on IO and I read the following article which talks about buffering techniques. To minimize disk accesses and work by the underlying operating system, buffering techniques use a temporary buffer that reads data in a chunk-wise manner, instead of reading data directly from the disk with every read operation.

示例是在没有缓冲和缓冲的情况下给出的。

Examples were given without and with buffering.

没有缓冲:

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中有没有其他方法可以做得更好?或者任何其他方法/技术可以提供更好的性能?请指教..!

Is there any other way to do this in Java that is better? Or any other method / technique to give better performance?Please advise..!

推荐答案


在Java中有没有其他方法可以做得更好?或者提供更好性能的任何其他方法/技术?

Is there any other way to do this in Java that is better? Or any other method / technique to give better performance?

就IO性能而言,如果没有a,那可能是最好的很多其他代码。无论如何,你最有可能成为IO绑定。

In terms of IO performance, that probably is going to be the best without a lot of other code. You are going to be IO bound most likely anyway.


while((b = bs.read())!= -1)

while((b =bs.read()) != -1)

逐字节读取效率非常低。如果您正在阅读文本文件,那么您应该使用 BufferedReader 。这会将字节数组转换为 String

This is very inefficient to read byte-by-byte. If you are reading a text file then you should be using a BufferedReader instead. This converts a byte array into String.

BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
...
while ((String line = reader.readLine()) != null) {
   ...
}

此外,对于任何IO,您应该始终在try / finally块中执行此操作以确保关闭它:

Also, with any IO, you should always do it in a try/finally block to make sure you close it:

FileInputStream fis = new FileInputStream(f);
BufferedReader reader;
try {
    reader = new BufferedReader(new InputStreamReader(fis));
    // once we wrap the fis in a reader, we just close the reader
} finally {
    if (reader != null) {
       reader.close();
    }
    if (fis != null) {
       fis.close();
    }
}

这篇关于关于读取文件和优化性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆