gpt4 book ai didi

arrays - 如何使用 for 循环加速数组中的字符串比较?

转载 作者:行者123 更新时间:2023-12-03 16:57:28 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2013 开发一个 C# 程序来比较两个文本文件。有两个文本框(dataTextOnedataTextTwo)包含每个文件的数据。有一个按钮 (findNextLineButton) 用于检查两个文本框之间的下一个不匹配行。以下是单击 findNextLineButton 时运行的代码。

private void findNextLineButton_Click(object sender, EventArgs e)
{
//set the starting point of the search to the lowest currently selected line of the two text boxes.
int start = Math.Min(dataTextOne.GetLineFromCharIndex(dataTextOne.GetFirstCharIndexOfCurrentLine()), dataTextTwo.GetLineFromCharIndex(dataTextTwo.GetFirstCharIndexOfCurrentLine())) + 1;
//set the ending point of the search to the length of the shortest text box.
int length = Math.Min(dataTextOne.Lines.Length, dataTextTwo.Lines.Length);

//loop through the lines of each textbox, stopping at the first point where the corresponding lines differ in value.
for (int i = start; i < length; i++)
{
if (dataTextOne.Lines[i] != dataTextTwo.Lines[i])
{
//selects and scrolls to the non-matching text.
dataTextOne.Focus();
dataTextTwo.Focus();
dataTextOne.SelectionStart = dataTextOne.GetFirstCharIndexFromLine(i);
dataTextOne.SelectionLength = dataTextOne.Lines[i].Length;
dataTextOne.ScrollToCaret();
dataTextTwo.SelectionStart = dataTextTwo.GetFirstCharIndexFromLine(i);
dataTextTwo.SelectionLength = dataTextTwo.Lines[i].Length;
dataTextTwo.ScrollToCaret();
return;
}
}

//in the case that the method has not yet returned, informs the user that no ingcongruities were found.
MessageBox.Show("Could not find incongruous line.");
}

这段代码的问题在于它运行得非常慢,i 每秒只增加大约 50。到目前为止,我一直使用的文件每行不超过 3 个字符,并且没有特殊符号。

我怎样才能加快这个过程?

最佳答案

感谢一些有用的用户,我已经修复了这个问题。

要解决这个问题,您只需将文本框的行复制到一个数组中。这样,您无需在每次执行 for 循环时访问整个 TextBox,而只需访问原始字符串数据,这就是本例中所需的全部内容。

关于arrays - 如何使用 for 循环加速数组中的字符串比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com