1

I need to read large file(like 400 mb) and make regex match in it. The problem is that it takes like 40 sec to read 800kb file and 15+ min for 5 mb file. Here is code:

 private int regexCompare(String text,Pattern p){
    if (text.isEmpty()) return 0;
    Matcher m = p.matcher(text);
    while (m.find()) {
        word.add(m.group());
    }
    combineBrackets(word);
    return 1;
}

Regex: String symbolsPattern = "([\\p{L}\\p{N}_#]+|[<>+\\-/*&!=.]+|[\\[\\]{}()])"; here is method, where i read file and match it

        Date currentTime = new Date();
    FileInputStream fis = new FileInputStream(filePath);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
    while(br.ready())
    {
        fileContents += br.readLine() + "\n";
    }
    br.close();
    regexCompare(fileContents,pattern);
    Date newTime = new Date();
    double msDelay = newTime.getTime() - currentTime.getTime();
    double toMin = 1000*60;
    timeLabel.setText("Maked in " + msDelay/toMin + " min");
  • 2
    I suggest you to read this answer: http://stackoverflow.com/a/14927864/3707125, and then switch to `StringBuilder` instead of `String` concatenation with `+`. – user3707125 Apr 06 '16 at 21:15
  • String concatenation is a killer here - do you expect the pattern to be across lines? If not why don't you check each `br.readLine()` against the regex? – assylias Apr 06 '16 at 21:16
  • @user3707125, tnx! Using StringBuilder gets 3x speed improvement! Now it takes 8sec! – Vasiliy Bohdanets Apr 06 '16 at 21:29

0 Answers0