计数较大时,循环会持续很长时间

发布于 2024-09-12 20:50:46 字数 513 浏览 10 评论 0原文

这个循环需要永远运行,因为循环中的项目数量接近或超过 1,000,接近 10 分钟。这需要快速运行,金额高达 30-40,000。

'Add all Loan Record Lines
Dim loans As List(Of String) = lar.CreateLoanLines()
Dim last As Integer = loans.Count - 1
For i = 0 To last
    If i = last Then
        s.Append(loans(i))
    Else
        s.AppendLine(loans(i))
    End If
Next

s 是一个 StringBuilder。 即使有数千条记录,第一行也

Dim loans As List(Of String) = lar.CreateLoanLines()

只需几秒钟即可运行。这是实际的循环,需要一段时间。

这该如何优化???

This loop takes forever to run as the amount of items in the loop approach anything close to and over 1,000, close to like 10 minutes. This needs to run fast for amounts all the way up to like 30-40 thousand.

'Add all Loan Record Lines
Dim loans As List(Of String) = lar.CreateLoanLines()
Dim last As Integer = loans.Count - 1
For i = 0 To last
    If i = last Then
        s.Append(loans(i))
    Else
        s.AppendLine(loans(i))
    End If
Next

s is a StringBuilder. The first line there

Dim loans As List(Of String) = lar.CreateLoanLines()

Runs in only a few seconds even with thousands of records. It's the actual loop that's taking a while.

How can this be optimized???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

浅笑轻吟梦一曲 2024-09-19 20:50:46

将 StringBuilder 的初始容量设置为一个较大的值。 (理想情况下,足够大以包含整个最终字符串。)就像这样:

s = new StringBuilder(loans.Count * averageExpectedStringSize)

如果您不指定容量,构建器可能最终会进行大量的内部重新分配,这会降低性能。

Set the initial capacity of your StringBuilder to a large value. (Ideally, large enough to contain the entire final string.) Like so:

s = new StringBuilder(loans.Count * averageExpectedStringSize)

If you don't specify a capacity, the builder will likely end up doing a large amount of internal reallocations, and this will kill performance.

浅浅 2024-09-19 20:50:46

您可以将特殊情况从循环中取出,这样您就不需要在循环内检查它。不过,我预计这对性能几乎没有影响。

For i = 0 To last - 1
    s.AppendLine(loans(i))
Next
s.Append(loans(last))

You could take the special case out of the loop, so you wouldn't need to be checking it inside the loop. I would expect this to have almost no impact on performance, however.

For i = 0 To last - 1
    s.AppendLine(loans(i))
Next
s.Append(loans(last))
指尖上的星空 2024-09-19 20:50:46

不过,在内部,代码非常相似,如果您使用 .NET 4,我会考虑通过一次调用来替换您的方法 String.Join

Dim result as String = String.Join(Envionment.NewLine, lar.CreateLoanLines())

Though, internally, the code is very similar, if you're using .NET 4, I'd consider replacing your method with a single call to String.Join:

Dim result as String = String.Join(Envionment.NewLine, lar.CreateLoanLines())
海螺姑娘 2024-09-19 20:50:46

我看不出您指出的代码可能会很慢,除非:

  • 您正在处理的字符串非常大(例如,如果生成的字符串是 1 GB)。
  • 您的机器上正在运行另一个进程,该进程消耗了所有时钟周期。
  • 您的机器内存不足。

尝试逐行单步执行代码并检查字符串是否包含您期望的数据,然后检查任务管理器以查看您的应用程序正在使用多少内存以及您有多少可用内存。

I can't see how the code you have pointed out could be slow unless:

  • The strings you are dealing with are huggggge (e.g. if the resulting string is 1 gigabyte).
  • You have another process running on your machine consuming all your clock cycles.
  • You haven't got enough memory in your machine.

Try stepping through the code line by line and check that the strings contain the data that you expect, and check Task Manager to see how much memory your application is using and how much free memory you have.

月下客 2024-09-19 20:50:46

我的猜测是,每次使用追加时,它都会创建一个新字符串。您似乎知道需要多少内存,如果您首先分配所有内存,然后将其复制到内存中,它应该运行得更快。虽然我可能对 vb.net 的工作原理感到困惑。

My guess would be that every time you're using append it's creating a new string. You seem to know how much memory you'll need, if you allocate all of the memory first and then just copy it into memory it should run much faster. Although I may be confused as to how vb.net works.

¢蛋碎的人ぎ生 2024-09-19 20:50:46

你可以考虑用另一种方式来做这件事。

Dim str As String = String.Join(Environment.NewLine, loans.ToArray)

You could look at doing this another way.

Dim str As String = String.Join(Environment.NewLine, loans.ToArray)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文