Well, this code would be simpler, if you're using .NET 4 or later you can use File.ReadLines
:
foreach (var line in File.ReadLines())
{
// Do something
}
Note that this is not the same as ReadAllLines
, as ReadLines
returns an IEnumerable<string>
which reads lines lazily, instead of reading the whole file in one go.
The effect at execution time will be broadly the same as your original code (it won't improve performance) - this is just simpler to read.
Fundamentally, if you're reading a large file, that can take a long time - but reading just a million lines shouldn't take "lots and lots of time". My guess is that whatever you're doing with the lines takes a long time. You might want to parallelize that, potentially using a producer/consumer queue (e.g. via BlockingCollection
) or TPL Dataflow, or just use Parallel LINQ, Parallel.ForEach
etc.
You should use a profiler to work out where the time is being spent. If you're reading from a very slow file system, then it's possible that it really is the reading which is taking the time. We don't have enough information to guide you on that, but you should be able to narrow it down yourself.