如何使VB.NET中的嵌套for循环在强大的计算机上运行更快?

huangapple go评论67阅读模式
英文:

how to make Nested for-loops in Vb.net run faster on a powerful computer?

问题

我试图在使用Vb.net编写的代码中嵌套6-7个for循环,然后尝试在一台拥有32GB内存的i9-9900电脑上进行编译,但运行时间太长了!我运行了任务管理器,意识到应用程序仅使用了CPU的10%以及少于20MB的内存!那么是否有办法通过更多的CPU计算能力来加速for循环呢?

这是我编写的示例代码:

Dim maxNo As Integer
maxNo = 200

For i = 0 To maxNo
 For k = i To maxNo
  For j = k To maxNo
   For l = j To maxNo
    For m = l To maxNo
     For n = m To maxNo
      For q = n To maxNo
          '......
      Next q
     Next n
    Next m
   Next l
  Next j
 Next k
Next i

提前感谢您,
程序员

在高性能电脑上尝试了这段代码,但仍然需要太长时间才能完成循环。我想充分利用强大的PC来更快地运行代码。

英文:

I was trying to code 6-7 nested for-loops using Vb.net and tried to compile it on a i9-9900 computer with 32 GB of memory, but the run time takes too long! I ran the task manager and realized that the application is using only 10% if the CPU and less than 20 mb of memory! so is there a way to speed up the for-loops using more CPU power?

here's the sample of what I was coding:

Dim maxNo As Integer
maxNo = 200

For i = 0 To maxNo
 For k = i To maxNo
  For j = k To maxNo
   For l = j To maxNo
    For m = l To maxNo
     For n = m To maxNo
      For q = n To maxNo
          '......
      Next q
     Next n
    Next m
   Next l
  Next j
 Next k
Next i

Thank you in advance,
The Programmer

tried the code on a high spec. computer, but it still take too much time to complete the loops. I want to take advantage of the powerful PC to run the code faster.

答案1

得分: 0

以下是您要翻译的部分:

"the simple matter is that your code will be single threaded, and your code show shows n^n type of problem."

"Been a while since I done combinations and permutations (high school). But this code is WORSE then a n! (factorial) problem."

"With just my lowly little laptop, you can in advanced compile, turn off "remove integer overflow checks - that will REALLY speed things up."

"So, with your code like this:

Dim maxNo As Integer
maxNo = 150

Dim startTime As DateTime = DateTime.Now
Dim MyTimeSpan As TimeSpan
Dim mycount As Long

For i = 1 To maxNo
For k = i To maxNo
For j = k To maxNo
For l = j To maxNo
For m = l To maxNo
For n = m To maxNo
For q = n To maxNo
mycount += 1
Next q
Next n
Next m
Next l
Next j
Next k
Next i

MyTimeSpan = DateTime.Now.Subtract(startTime)
TextBox1.Text = $"Time to run loop = { (MyTimeSpan.TotalMilliseconds) / 1000.0}"
TextBox2.Text = mycount"

"To JUST a value of 150?

You are going to run a whopping and "astounding" 389 BILLION (yes, that is correct BILLION loops!!)."

"I also found that running as x86 (forced x32 bit project) seems to run a good deal faster (no need to force the CPU to work with larger x64 bit side codes)."

"So, your problem?

You have to consider a smaller problem, or maybe perhaps adopt some type of parallal processing. but even with such a approach, your numbers are rather large, and thus you dealing with large amounts of processing."

英文:

the simple matter is that your code will be single threaded, and your code show shows n^n type of problem.

Been a while since I done combinations and permutations (high school). But this code is WORSE then a n! (factorial) problem.

With just my lowly little laptop, you can in advanced compile, turn off "remove integer overflow checks - that will REALLY speed things up.

So, with your code like this:

    Dim maxNo As Integer
    maxNo = 150

    Dim startTime As DateTime = DateTime.Now
    Dim MyTimeSpan As TimeSpan
    Dim mycount As Long

    For i = 1 To maxNo
        For k = i To maxNo
            For j = k To maxNo
                For l = j To maxNo
                    For m = l To maxNo
                        For n = m To maxNo
                            For q = n To maxNo
                                mycount += 1
                            Next q
                        Next n
                    Next m
                Next l
            Next j
        Next k
    Next i

    MyTimeSpan = DateTime.Now.Subtract(startTime)
    TextBox1.Text = $"Time to run loop = { (MyTimeSpan.TotalMilliseconds) / 1000.0}"
    TextBox2.Text = mycount

To JUST a value of 150?

You are going to run a whopping and "astounding" 389 BILLION (yes, that is correct BILLION loops!!).

I get this:

如何使VB.NET中的嵌套for循环在强大的计算机上运行更快?

I also found that running as x86 (forced x32 bit project) seems to run a good deal faster (no need to force the CPU to work with larger x64 bit side codes).

So, your problem?

You have to consider a smaller problem, or maybe perhaps adopt some type of parallal processing. but even with such a approach, your numbers are rather large, and thus you dealing with large amounts of processing.

huangapple
  • 本文由 发表于 2023年4月11日 02:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定