有没有比使用循环更快的方法来处理元素序列?

发布于 2024-08-15 17:14:13 字数 467 浏览 7 评论 0原文

在这里,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撩人痒 2024-08-22 17:14:13

鉴于您提供的信息,无法改进此代码。这是一个简单的 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 a StringBuilder - 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.

腻橙味 2024-08-22 17:14:13

正如其他人所说,StringBuilder 的速度与您所能达到的速度差不多,因此假设这是唯一可能导致速度变慢的代码,您可能无能为力...但是您可以通过删除正在执行的少量字符串连接来稍微优化它。即:

for (int a = 0; a < grdMass.RowCount; a++)
{
    if (a == 0)
    {
        _MSISDN.Append("'");
    }
    else
    {
        _MSISDN.Append(",'");
    }
    _MSISDN.Append(grdMass.Rows[a].Cells[0].Value);
    _MSISDN.AppendLine("'");
}

编辑:您还可以清理if语句(尽管我非常怀疑它是否具有显着的效果),如下所示:

//First row
if (grdMass.RowCount > 0)
{
    _MSISDN.Append("'");
    _MSISDN.Append(grdMass.Rows[0].Cells[0].Value);
    _MSISDN.AppendLine("'");
}
//Second row onwards
for (int a = 1; a < grdMass.RowCount; a++)
{
    _MSISDN.Append(",'");
    _MSISDN.Append(grdMass.Rows[a].Cells[0].Value);
    _MSISDN.AppendLine("'");
}

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:

for (int a = 0; a < grdMass.RowCount; a++)
{
    if (a == 0)
    {
        _MSISDN.Append("'");
    }
    else
    {
        _MSISDN.Append(",'");
    }
    _MSISDN.Append(grdMass.Rows[a].Cells[0].Value);
    _MSISDN.AppendLine("'");
}

Edit: You could also clean up the if statement (although I highly doubt it's having a noticable effect) like so:

//First row
if (grdMass.RowCount > 0)
{
    _MSISDN.Append("'");
    _MSISDN.Append(grdMass.Rows[0].Cells[0].Value);
    _MSISDN.AppendLine("'");
}
//Second row onwards
for (int a = 1; a < grdMass.RowCount; a++)
{
    _MSISDN.Append(",'");
    _MSISDN.Append(grdMass.Rows[a].Cells[0].Value);
    _MSISDN.AppendLine("'");
}
黑寡妇 2024-08-22 17:14:13

我怀疑不是字符串构建需要很长时间,也许是访问网格元素很慢。

您可以像这样重写代码:

var cellValues = grdMass.Rows
    .Select(r => "'" + r.Cells[0].Value.ToString() + "'")
    .ToArray();

return String.Join(",", cellValues);

现在您可以验证哪一部分花费最多时间。是构建 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:

var cellValues = grdMass.Rows
    .Select(r => "'" + r.Cells[0].Value.ToString() + "'")
    .ToArray();

return String.Join(",", cellValues);

Now you can verify which part takes the most time. Is it building the cellValues array, or is it the String.Join call?

旧竹 2024-08-22 17:14:13

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.

明媚如初 2024-08-22 17:14:13

这样就更好了......

 if (grdMass.RowCount > 0)
 {
      _MSISDN.AppendLine("'" + grdMass.Rows[0].Cells[0].Value.ToString() + "'");

      for (int a = 1; a < grdMass.RowCount; a++)
      {
            _MSISDN.AppendLine(",'" + grdMass.Rows[a].Cells[0].Value.ToString() + "'");
      }     
 }

This would be better....

 if (grdMass.RowCount > 0)
 {
      _MSISDN.AppendLine("'" + grdMass.Rows[0].Cells[0].Value.ToString() + "'");

      for (int a = 1; a < grdMass.RowCount; a++)
      {
            _MSISDN.AppendLine(",'" + grdMass.Rows[a].Cells[0].Value.ToString() + "'");
      }     
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文