有没有比使用循环更快的方法来处理元素序列?
在这里,我使用 for 循环将数据网格的元素存储在字符串生成器中,但是当存在大量行时,它会花费太多时间。是否有另一种方法可以在更短的时间内将数据复制到字符串生成器中?
for (int a = 0; a < grdMass.RowCount; a++)
{
if (a == 0)
{
_MSISDN.AppendLine("'" + grdMass.Rows[a].Cells[0].Value.ToString() + "'");
}
else
{
_MSISDN.AppendLine(",'" + grdMass.Rows[a].Cells[0].Value.ToString() + "'");
}
}
Here I am storing the elements of a datagrid in a string builder using a for loop, but it takes too much time when there is a large number of rows. Is there another way to copy the data in to a string builder in less time?
for (int a = 0; a < grdMass.RowCount; a++)
{
if (a == 0)
{
_MSISDN.AppendLine("'" + grdMass.Rows[a].Cells[0].Value.ToString() + "'");
}
else
{
_MSISDN.AppendLine(",'" + grdMass.Rows[a].Cells[0].Value.ToString() + "'");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
鉴于您提供的信息,无法改进此代码。这是一个简单的
for
循环,它将字符串附加到StringBuilder
- 这里没有太多可以优化的地方。这可能是仅仅因为您正在处理大量数据而需要很长时间的情况之一。也许有一种方法可以缓存这些数据,这样您就不必经常生成它。您还有什么可以告诉我们的,可以帮助我们找到更好的方法来做到这一点吗?
旁注:非常需要验证您对导致速度缓慢的特定代码部分的怀疑。通过分析代码来做到这一点,这样您就不必花时间尝试修复其他地方存在的问题。
There is no way to improve this code given the information you have provided. This is a simply
for
loop that appends strings to aStringBuilder
- there isn't a whole lot going on here that can be optimized.This may be one of those cases where something takes a long time simply because you are processing a lot of data. Perhaps there is a way to cache this data so you don't have to generate it as often. Is there anything else you can tell us that would help us find a better way to do this?
Side note: It is very important that you validate your suspicions as to the particular section of code that is causing the slowness. Do this by profiling your code so that you don't spend time trying to fix a problem that exists elsewhere.
正如其他人所说,
StringBuilder
的速度与您所能达到的速度差不多,因此假设这是唯一可能导致速度变慢的代码,您可能无能为力...但是您可以通过删除正在执行的少量字符串连接来稍微优化它。即:编辑:您还可以清理
if
语句(尽管我非常怀疑它是否具有显着的效果),如下所示:As others have said, the
StringBuilder
is about as fast as you're going to get, so assuming this is the only bit of code that could be causing your slow down, there's probably not much you can do... but you could slightly optimise it by removing the small amount of string concatenation you are doing. I.e:Edit: You could also clean up the
if
statement (although I highly doubt it's having a noticable effect) like so:我怀疑不是字符串构建需要很长时间,也许是访问网格元素很慢。
您可以像这样重写代码:
现在您可以验证哪一部分花费最多时间。是构建 cellValues 数组,还是 String.Join 调用?
I'm suspecting that it's not the string building that takes a long time, perhaps it's accessing the grid elements that is slow.
You could rewrite your code like this:
Now you can verify which part takes the most time. Is it building the cellValues array, or is it the String.Join call?
StringBuilder 构建字符串的速度几乎是最快的——而且速度快得惊人。如果 StringBuilder 太慢,您可能试图一次性处理太多数据。您确定确实是字符串构建速度慢,而不是处理的其他部分?
对于非常大的字符串,可以加快 StringBuilder 速度的一个技巧是:预先设置容量。也就是说,调用
StringBuilder(int)
构造函数而不是默认构造函数,传递您计划写入的字符数的估计值。如果你低估的话,它仍然会扩展——这只是节省了最初的“好吧,1K 不够,有时间分配另一个 2K...4K...等等”。但这只会产生很小的差异,而且只有当您的字符串非常长时。StringBuilder is pretty much as fast as it gets for building up strings -- and that is pretty goshdarned fast. If StringBuilder is too slow, you are probably trying to process too much data in one go. Are you sure it is really the string building which is slow and not some other part of the processing?
One tip that will speed up StringBuilder for very large strings: set the capacity up front. That is, call the
StringBuilder(int)
constructor instead of the default constructor, passing an estimate of the number of characters you plan to write. It will still expand if you underestimate -- this just saves the initial "well, 1K wasn't enough, time to allocate another 2K... 4K... etc." But this will make only a small difference, and only if your strings are very long.这样就更好了......
This would be better....