views:

34

answers:

1

Suppose i have to filter some text from a file. Then i have 2 solutions

  1. Either I take all the contents of file into a single variable(like fileinputstream or something else) which can be parsed using regular expression.
  2. Or i use looping to read file line by line. Then i apply either regular expression or some string function on each line.

Which method will be better and faster?

A: 

Most regular expression libraries (such as PCRE) are very efficient and highly optimized, so I say go with the first option.

But of course if performance is very important to you, you should use a profiler anyway; it could give you a better answer for your exact situation.

imgx64