簡體   English   中英

使用Java查找文件中有多少個非空行的最快方法是什么?

[英]What is the fastest way to find out how many non-empty lines are in a file, using Java?

使用Java查找文件中有多少個非空行的最快方法是什么?

最簡單的方法是使用BufferedReader,並檢查哪些行為空。 但是,這是一種相對較慢的方法,因為它需要為文件中的每一行創建一個String對象。 一種更快的方法是使用read()將文件讀入數組,然后遍歷數組以計算換行符。

這是兩個選項的代碼; 第二個大約花了我機器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);

}

關於NIO的建議,我支持Limbic System。 我在Daphna的測試代碼中添加了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);

}

以下是89MB文件的預熱結果:

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

NIO比FileReader快2.5倍,比BufferedReader快4倍!

盡管預熱時間要長得多,但使用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

隨心所欲。

最簡單的方法是使用掃描儀(是的,我喜歡冗長的代碼...您可以使其物理上更短)。 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;
    }
}

如果確實必須盡可能快,則應考慮使用NIO 然后,在目標平台上測試代碼,以查看使用NIO是否真的更好。 我為Netflix獎使用的某些代碼獲得了一個數量級的改進。 它涉及將數千個文件解析為更緊湊,快速加載的二進制格式。 NIO對我的(緩慢的)開發筆記本電腦有很大的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM