I have a database in a text file, split up by certain words (NUM/OPP/TUM/YUR/etc) to know where each piece of data should go. The issue is that running this is very slow, it would take about an hour to get through all three thousand things in the .txt file, most likely due to the .Split() (I think).

Is there a faster way to do this?

wholepage = streamReader.ReadToEnd();
while (true)
{
    tempword = wholepage.Split()[tempnum];
    tempnum++;
    if (lastword == "NUM")
    {
        things[number_of_things].num = tempword;
        number_of_things++;
        slist.Add(new string[] { tempword });
        listBox5.Items.Add(tempword);
    }
    lastword = tempword;
}

Thanks in advance.

Edit: Thanks for the help guys...and yes it was infinite loop, which didn't matter at the time since it would never make it through the loop once (unless you waited an hour).

有帮助吗?

解决方案

Yes, split your input only once, on once every time loop is executed:

wholepage = streamReader.ReadToEnd();
var split = wholepage.Split();
while (true)
{
    tempword = split[tempnum];
    // (...)
}

And btw. you doesn't stop your loop so it potentially never ends (well, actually it does, when index is greater than number of items in array and exception is thrown). You should probably use foreach instead of while:

wholepage = streamReader.ReadToEnd();
var split = wholepage.Split();
foreach(var tempword in split)
{
    if(lastword == "NUM")
    {
        things[number_of_things].num = tempword;
        number_of_things++;
        slist.Add(new string[] { tempword });
        listBox5.Items.Add(tempword);
    }
    lastword = tempword;
}

其他提示

Don't repeatedly Split! Take this outside the loop...

myPage = wholepage.Split();

Then in your loop: tempword=myPage[tempnum]

Also, instead of while(true), which will just keep looping, use a for loop where myPage.length < tempnum

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top