2009-03-27 1 views

答えて

5

私はNIO勧告でLimbic Systemを使用しています。私は彼の二つの方法に対してそれをマークしダフナのテストコードとベンチにNIOメソッドを追加しました:ここ

public static void timeNioReader() throws IOException { 
    long bef = System.currentTimeMillis(); 

    File file = new File("/Users/stu/test.txt"); 
    FileChannel fc = (new FileInputStream(file)).getChannel(); 
    MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, 0, file.length()); 
    boolean emptyLine = true; 
    int  counter = 0; 

    while (buf.hasRemaining()) 
    { 
     byte element = buf.get(); 

     if (element == '\r' || element == '\n') { 
      if (!emptyLine) { 
       counter += 1; 
       emptyLine = true; 
      } 
     } else 
      emptyLine = false; 

    } 

    long after = System.currentTimeMillis() - bef; 

    System.out.println("timeNioReader  Time: " + after + " Result: " + counter); 

} 

は、89メガバイトのファイルのためのウォームアップの結果は以下のとおりです。

timeBufferedReader Time: 947 Result: 747656 
timeFileReader  Time: 670 Result: 747656 
timeNioReader  Time: 251 Result: 747656 

NIOが2.5倍高速でありますBufferedReaderよりもFileReaderおよび4x fastserよりも優れています。

6.4MBのファイルでは、ウォームアップ時間がはるかに長いものの、結果はさらに向上します。

//jvm start, warming up 
timeBufferedReader Time: 121 Result: 53404 
timeFileReader  Time: 65 Result: 53404 
timeNioReader  Time: 40 Result: 53404 

//still warming up 
timeBufferedReader Time: 107 Result: 53404 
timeFileReader  Time: 60 Result: 53404 
timeNioReader  Time: 20 Result: 53404 

//ripping along 
timeBufferedReader Time: 79 Result: 53404 
timeFileReader  Time: 56 Result: 53404 
timeNioReader  Time: 16 Result: 53404 

あなたはそれをしてください。

2

最も簡単なのはスキャナです(私は冗長コードが好きです...物理的に短くすることができます)。 Scanner()はFile、Readerなども受け取ります。あなたが持っているものを渡すことができます。

import java.util.Scanner; 


public class Main 
{ 
    public static void main(final String[] argv) 
    { 
     final Scanner scanner; 
     final int  lines; 

     scanner = new Scanner("Hello\n\n\nEvil\n\nWorld"); 
     lines = countLines(scanner); 
     System.out.println("lines = " + lines); 
    } 

    private static int countLines(final Scanner scanner) 
    { 
     int lines; 

     lines = 0; 

     while(scanner.hasNextLine()) 
     { 
      final String line; 

      line = scanner.nextLine(); 

      if(line.length() > 0) 
      { 
       lines++; 
      } 
     } 

     return lines; 
    } 
} 
+0

あなたが投票した理由を言わなかったので、「最速」には2つの意味があります。最速の実行と最速の開発です。 「最も速い」という意味の場合には、最も簡単に開発することができました。それが別の理由であった場合、下院の理由を知ってうれしいです。 – TofuBeer

6

最も簡単な方法は、BufferedReaderを使用して、どの行が空であるかを確認することです。ただし、ファイル内のすべての行に対してStringオブジェクトを作成する必要があるため、これは比較的遅い方法です。より高速な方法は、read()を使ってファイルを配列に読み込んだ後、配列を繰り返して改行を数えさせることです。

ここでは2つのオプションのコードを示します。 2番目のマシンはマシンの約50%を占めていました。

public static void timeBufferedReader() throws IOException 
{ 
    long bef = System.currentTimeMillis(); 

    // The reader buffer size is the same as the array size I use in the other function 
    BufferedReader reader = new BufferedReader(new FileReader("test.txt"), 1024 * 10); 
    int counter = 0; 
    while (reader.ready()) 
    { 
     if (reader.readLine().length() > 0) 
      counter++; 
    } 

    long after = System.currentTimeMillis() - bef; 

    System.out.println("Time: " + after + " Result: " + counter); 

} 

public static void timeFileReader() throws IOException 
{ 
    long bef = System.currentTimeMillis(); 

    FileReader reader = new FileReader("test.txt"); 
    char[] buf = new char[1024 * 10]; 
    boolean emptyLine = true; 
    int  counter = 0; 
    while (reader.ready()) 
    { 
     int len = reader.read(buf,0,buf.length); 
     for (int i = 0; i < len; i++) 
     { 
      if (buf[i] == '\r' || buf[i] == '\n') 
      { 
       if (!emptyLine) 
       { 
        counter += 1; 
        emptyLine = true; 
       } 
      } 
      else emptyLine = false; 
     } 
    } 

    long after = System.currentTimeMillis() - bef; 

    System.out.println("Time: " + after + " Result: " + counter); 

} 
2

実際にできるだけ速くする必要がある場合は、NIOを調べる必要があります。そして、あなたのターゲットプラットフォーム上のあなたのコードが、NIOを使って本当に本当に良いかどうかを見てください。私はNetflix Prizeのために遊んでいたいくつかのコードで、大きさの改善を得ることができました。何千ものファイルをよりコンパクトで高速なバイナリ形式に構文解析することが含まれていました。 NIOは私の(遅い)開発ラップトップの大きな助けとなりました。

関連する問題