C#:将字符串插入另一个字符串 - 性能问题
我有一个很长的字符串,以及一个索引和值的排序字典。我应该检查字典中的元素并将值插入到字符串中的指定索引。我编写了以下代码,它工作正常,但速度非常慢:
private string restoreText(string text){
StringBuilder sb = new StringBuilder(text);
foreach(KeyValuePair<int, string> pair in _tags){
sb.Insert(pair.Key, pair.Value);
}
return sb.ToString();
}
字典可能非常大,包含 500,000 个元素。 我认为导致这个函数变慢的是 Insert() 方法。对于10万个元素的字典,花费了将近5秒。
有没有更有效的方法来编写这个方法?
谢谢,
玛雅
I have a string, which is long, and a sorted dictionary of indexes and values. I should go over the elements in the dictionary and insert the value to the specified index in the string. I wrote the following code, which works fine, but very slow:
private string restoreText(string text){
StringBuilder sb = new StringBuilder(text);
foreach(KeyValuePair<int, string> pair in _tags){
sb.Insert(pair.Key, pair.Value);
}
return sb.ToString();
}
The dictionary might be very big and contain 500,000 elements.
I think that what makes this function slow is the Insert() method. For dictionary of 100,000 elements, it took almost 5 seconds.
Is there a more efficient way to write this method?
Thanks,
Maya
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更好的方法是对要插入的项目进行排序,然后将它们一个接一个地附加。
既然您没有对重叠发表评论,也许您首先已经对物品进行了排序?
Better way would be to sort items for insertion and then append them one after another.
Since you didn't comment on the overlap, maybe you have your items sorted in the first place?
您的原始代码将根据从 _tags 返回项目的顺序给出不同的结果;我非常怀疑这不是你的意图。
相反,按顺序对标签进行排序,然后按正确的顺序将它们添加到字符串生成器中:
如果您确实想让此操作尽可能快,请预先初始化
StringBuilder
的容量:更新
我错过了最初用于初始化
StringBuilder
的text
参数。为了避免在内存中打乱文本(由
StringBuilder.Insert()
引起),我们希望坚持使用StringBuilder.Append()
。我们可以通过将原始文本转换为另一个
KeyValuePair
实例序列,将它们与原始列表合并并按顺序处理来实现此目的。它看起来像这样(注意:临时代码):
注意 - 我对您的上下文做了一大堆假设,所以这可能不太适合您。特别要注意的是,
.Union()
将丢弃重复项,尽管有一些简单的解决方法。Your original code will give different results depending on the order that items are returned from _tags; I very much suspect this isn't your intent.
Instead, sort the tags into order and then add them into the string builder in correct sequence:
If you really want to make this go as fast as possible, initialise the capacity of the
StringBuilder
up front:Update
I missed the
text
parameter originally used to initialise theStringBuilder
.In order to avoid shuffling text around in memory (as caused by
StringBuilder.Insert()
), we want to stick with usingStringBuilder.Append()
.We can do this by converting the original text into another sequence of
KeyValuePair
instances, merging those with the original list and processing in order.It would look something like this (note: adhoc code):
Note - I've made a whole heap of assumptions about your context, so this may not work quite right for you. Particularly be aware, that
.Union()
will discard duplicates, though there are easy workarounds for that.如果您设置了索引,以便插入不会更改其他索引,那么我不会得到什么,但正如您的代码所说“是”,我也会这么认为。
你能测试一下这个吗:
它只使用追加,同时与你的原始代码相同
what I don't get if you have your indices setup so that the insert won't change the others but as your code says "yes" I'll assume so too.
Can you test this one:
it only uses append while beeing the same as your original code
我不知道你的数据怎么样。
但在我的测试中,它运行得很快(564ms)。
如果你可以使用 append() 而不是 insert() ,它只需要 35ms...
I donnt know how about your data.
but in my test , it run fast(564ms) .
if you can use append() instead of insert() , it only takes 35ms...