AceInfinity
Emeritus, Contributor
So I know there's lots of people that use the Peek() method to read through each line, but this is actually a bit slower because with the Peek() method you're reading characters and returning an integer value for what the Peek() method finds.
Here i'm skipping that character check, and moving right to read and checking by line.
The beauty of Functions and the ref keyword in parameters, i'm doing 2 useful things at once for what i'm trying to accomplish. The reason why I don't call ReadLine() anywhere but in the function is because ReadLine() moves the buffer or position to the next line after it's called, so if you call it once within the while loop conditional, and then another time even to just assign it to a string within the actual while loop, you've already skipped the position or buffer, 2 lines down in the process... That's why I've devised a handy little function instead, which returns a Boolean so that we're not in an endless loop, but also assigns the value to our string value to be used within the while loop containing that line in the file.
NOTE: The only thing about this... You cannot have lines that are ""... Or the while loop will stop at that point in the file :)
To fix that, try a function like this instead in combination with my MainMethod() above:
:beerchug2:
EDIT: I just read through 2.2 million lines in a text file within 1/4 second (~250ms) using my method above. Try that with the Peek() method.
Any overhead is because of what you do with each line through the iteration process. Strictly reading each line though took me approx. ~250ms...
Here i'm skipping that character check, and moving right to read and checking by line.
C#:
private void MainMethod()
{
using (StreamReader sr = new StreamReader(@"F:\file.txt"))
{
string lnBuffer = string.Empty;
while (LinesAvailable(ref lnBuffer, sr.ReadLine()))
{
//Do something with lnBuffer here...
}
}
}
private bool LinesAvailable(ref string lnBuffer, string readLine)
{
lnBuffer = readLine;
return !string.IsNullOrEmpty(lnBuffer);
}
The beauty of Functions and the ref keyword in parameters, i'm doing 2 useful things at once for what i'm trying to accomplish. The reason why I don't call ReadLine() anywhere but in the function is because ReadLine() moves the buffer or position to the next line after it's called, so if you call it once within the while loop conditional, and then another time even to just assign it to a string within the actual while loop, you've already skipped the position or buffer, 2 lines down in the process... That's why I've devised a handy little function instead, which returns a Boolean so that we're not in an endless loop, but also assigns the value to our string value to be used within the while loop containing that line in the file.
NOTE: The only thing about this... You cannot have lines that are ""... Or the while loop will stop at that point in the file :)
To fix that, try a function like this instead in combination with my MainMethod() above:
C#:
private bool LinesAvailable(ref string lnBuffer, string readLine)
{
lnBuffer = readLine;
return !(lnBuffer == null);
}
:beerchug2:
EDIT: I just read through 2.2 million lines in a text file within 1/4 second (~250ms) using my method above. Try that with the Peek() method.
Any overhead is because of what you do with each line through the iteration process. Strictly reading each line though took me approx. ~250ms...
Last edited: