Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21238

How can I make a very time-consuming loop short?

$
0
0
Hi.
There are some loops in my code, that take very long time (a few hours) to complete. This is one of them, that also is the longest.

Code:

    For x = 0 To cal_it(5)
        If z_it(x) = 0 And kh(x) <> 0 Then
        z_it(x) = 1
            For xprime = 0 To cal_it(5)
                If z_it(xprime) = 0 And kh(xprime) <> 0 Then
                z_it(xprime) = 1
                    For w = 0 To cal_it(5)
                        If z_it(w) = 0 And kh(w) <> 0 Then
                        z_it(w) = 1
                            For wprime = 0 To cal_it(5)
                                If z_it(wprime) = 0 And kh(wprime) <> 0 Then
                                z_it(wprime) = 1
                                    For u = 0 To cal_it(5)
                                        If z_it(u) = 0 And kh(u) <> 0 Then
                                            p5 = p5 + (probability(x)) * (probability(xprime) * probability(w) * probability(wprime) * probability(u))
                                        End If
                                    Next u
                                End If
                                z_it(wprime) = 0
                            Next wprime
                        End If
                        z_it(w) = 0
                    Next w
                End If
                z_it(xprime) = 0
            Next xprime
        End If
        z_it(x) = 0
    Next x

The value of cal_it(5) is 119. It means the commands in loop should be repeated about 180,000,000 times (because it considers all the combinations of 119 x, that includes five values). Is there any other way to write these kinds of loops that make them efficient?
Thank you so much for your helps.

Viewing all articles
Browse latest Browse all 21238

Trending Articles