C
C#11mo ago
bl4ck

Fastest way to read specific line from string

I'm working on a project that involves reading specific lines of a file multiple times. I'm afraid this ReadLine function is the reason it takes a painfully long time to finish so I'm trying to optimize it, but I'm not sure what is the fastest way to do it This was my old code:
//public static string ReadLine(string text, int lineNumber)
//{
// var reader = new StringReader(text);
//
// string line;
// int currentLineNumber = 0;
//
// do
// {
// currentLineNumber += 1;
// line = reader.ReadLine();
// }
// while (line != null && currentLineNumber < lineNumber);
//
// return (currentLineNumber == lineNumber) ? line :
// string.Empty;
//}
//public static string ReadLine(string text, int lineNumber)
//{
// var reader = new StringReader(text);
//
// string line;
// int currentLineNumber = 0;
//
// do
// {
// currentLineNumber += 1;
// line = reader.ReadLine();
// }
// while (line != null && currentLineNumber < lineNumber);
//
// return (currentLineNumber == lineNumber) ? line :
// string.Empty;
//}
This is my new code: (prevText is in place in case it reads the same thing so that it skips splitting it)
private string[] splitString = null;
public static string prevText = "";
public static string ReadLine(string text, int lineNumber)
{

if (!text.Equals(prevText)) Instance.splitString = text.Split('\n');

prevText = text;
return Instance.splitString[lineNumber - 1];
}
private string[] splitString = null;
public static string prevText = "";
public static string ReadLine(string text, int lineNumber)
{

if (!text.Equals(prevText)) Instance.splitString = text.Split('\n');

prevText = text;
return Instance.splitString[lineNumber - 1];
}
5 Replies
Thinker
Thinker11mo ago
Firstly this Instance thing looks... very concerning I don't know why you're using static fields instead of local variables Anyway, you could just loop through the entire string, incrementing the line number on every newline character, then when you get to the target line just slice the string until the next newline.
bl4ck
bl4ckOP11mo ago
it is pretty concerning but works for now would that be more efficient?
Thinker
Thinker11mo ago
Well it wouldn't be allocating the array So... maybe Benchmark it if you really need to But god, please get rid of this static/non-static nonsense.
br4kejet
br4kejet11mo ago
Unless you store all of the text in code and cache each line, there isn't really any other way to optimise A text editor for something like an IDE store all characters ofc, but also the character index of specific control characters like new line chars, which get updated when the original text changes. So maybe could you create something like that?
MarkPflug
MarkPflug11mo ago
Suggestion: don't create a string[]. Instead create an int[] where each element indicates the offset of the next '\n'. The first element implicitly starts and 0, then use IndexOf until you reach the end of the text. You can then use the n and n-1 indices to get the span of the string and only create the new string (Substring) if it is needed. You could also "lazily" evaluate the indices as needed, meaning if they ask for the first line there is no reason to calculate the indices of every line in the file, you can stop after you get to the requested index, and resume from there if they request again. Hard to make suggestions without understanding the expected access/usage patterns though.
Want results from more Discord servers?
Add your server