|
本帖最后由 lee1892 于 2014-12-19 10:39 编辑
51 楼代码改进后的效果,由2.1秒加速到1.6秒不到,20%+- ========================================
- Chose 13 From 27 Test #1:
- Original From 香川: 2.125s 20058300
- Change Inner DO to FOR: 1.953s 20058300
- Remove One IF: 1.656s 20058300
- Remove Outer DO: 1.578s 20058300
- Final Work by Lee1892: 1.547s 20058300
- ========================================
- Chose 13 From 27 Test #2:
- Original From 香川: 2.109s 20058300
- Change Inner DO to FOR: 1.969s 20058300
- Remove One IF: 1.656s 20058300
- Remove Outer DO: 1.578s 20058300
- Final Work by Lee1892: 1.547s 20058300
- ========================================
- Chose 13 From 27 Test #3:
- Original From 香川: 2.109s 20058300
- Change Inner DO to FOR: 1.953s 20058300
- Remove One IF: 1.641s 20058300
- Remove Outer DO: 1.563s 20058300
- Final Work by Lee1892: 1.563s 20058300
- ========================================
- Chose 13 From 27 Test #4:
- Original From 香川: 2.156s 20058300
- Change Inner DO to FOR: 2.031s 20058300
- Remove One IF: 1.641s 20058300
- Remove Outer DO: 1.609s 20058300
- Final Work by Lee1892: 1.594s 20058300
- ========================================
- Chose 13 From 27 Test #5:
- Original From 香川: 2.141s 20058300
- Change Inner DO to FOR: 2.000s 20058300
- Remove One IF: 1.656s 20058300
- Remove Outer DO: 1.609s 20058300
- Final Work by Lee1892: 1.563s 20058300
复制代码 代码改进的过程如下:- Private Sub TestSpeed()
- Dim i%, m&, n&
- m = 27
- n = 13
- For i = 1 To 5
- Debug.Print String(40, "=")
- Debug.Print "Chose " & n & " From " & m & " Test #" & i & ":"
- Debug.Print " Original From 香川: "; Combin_1(m, n)
- Debug.Print "Change Inner DO to FOR: "; Combin_2(m, n)
- Debug.Print " Remove One IF: "; Combin_3(m, n)
- Debug.Print " Remove Outer DO: "; Combin_4(m, n)
- Debug.Print " Final Work by Lee1892: "; Combin_Final(m, n)
- Next
- End Sub
- Function Combin_1(ByVal m&, ByVal n&)
- Dim i&, j&, k&, tms!
- tms = Timer
- ReDim a&(1 To n)
- For j = 1 To n - 1
- a(j) = j
- Next
- k = 0: i = n - 1 ': j = n
- Do
- For i = i + 1 To m
- 'a(j) = i
- k = k + 1
- Next
- For j = j - 1 To 1 Step -1
- i = a(j) + 1: a(j) = i
- If i = m - n + j Then
- k = k + 1
- Else
- j = j + 1
- Do Until j = n
- i = i + 1: a(j) = i: j = j + 1
- Loop
- If i = m Then Exit Do Else Exit For
- End If
- Next
- Loop Until j = 0
- Erase a
- Combin_1 = Format(Timer - tms, "0.000s ") & k
- End Function
- Function Combin_2(ByVal m&, ByVal n&)
- Dim i&, j&, k&, tms!, p&
- tms = Timer
- ReDim a&(1 To n)
- For j = 1 To n ' - 1
- a(j) = j
- Next
- k = 0: i = n - 1: j = n
- Do
- For i = i + 1 To m
- 'a(n) = i
- k = k + 1
- Next
- For j = j - 1 To 1 Step -1
- i = a(j) + 1: a(j) = i
- If i = m - n + j Then
- k = k + 1
- Else
- 'j = j + 1
- 'Do Until j = n
- For p = j + 1 To n - 1
- i = i + 1: a(p) = i ': j = j + 1
- Next
- 'Loop
- j = n
- If i = m Then Exit Do Else Exit For
- End If
- Next
- Loop Until j = 0
- Erase a
- Combin_2 = Format(Timer - tms, "0.000s ") & k
- End Function
- Function Combin_3(ByVal m&, ByVal n&)
- Dim i&, j&, k&, tms!, p&
- tms = Timer
- ReDim a&(1 To n)
- For j = 1 To n
- a(j) = j
- Next
- 'k = 0: i = n - 1: j = n
- If m = n Then
- k = 1
- GoTo EXIT_FUNC
- End If
- i = n - 1
- Do
- For i = i + 1 To m
- 'a(n) = i
- k = k + 1
- Next
- 'For j = j - 1 To 1 Step -1
- For j = n - 1 To 1 Step -1
- i = a(j) + 1: a(j) = i
- If i = m - n + j Then
- k = k + 1
- Else
- 'j = j + 1
- 'Do Until j = n
- For p = j + 1 To n - 1
- i = i + 1: a(p) = i ': j = j + 1
- Next
- 'Loop
- 'If i = m Then Exit Do Else Exit For
- Exit For
- End If
- Next
- Loop Until j = 0
- EXIT_FUNC:
- Erase a
- Combin_3 = Format(Timer - tms, "0.000s ") & k
- End Function
- Function Combin_4(ByVal m&, ByVal n&)
- Dim i&, j&, k&, tms!, p&
- tms = Timer
- ReDim a&(1 To n)
- For j = 1 To n
- a(j) = j
- Next
- If m = n Then
- k = 1
- GoTo EXIT_FUNC
- End If
- 'k = 0: i = n - 1: j = n
- 'Do
- 'For i = i + 1 To m
- For i = n To m
- 'a(n) = i
- k = k + 1
- Next
- 'For j = j - 1 To 1 Step -1
- For j = n - 1 To 1 Step -1
- i = a(j) + 1: a(j) = i
- If i = m - n + j Then
- k = k + 1
- Else
- 'j = j + 1
- 'Do Until j = n
- For p = j + 1 To n - 1
- i = i + 1: a(p) = i ': j = j + 1
- Next
- 'Loop
- For i = i + 1 To m
- 'a(n) = i
- k = k + 1
- Next
- j = n
- 'If i = m Then Exit Do Else Exit For
- End If
- Next
- 'Loop Until j = 0
- EXIT_FUNC:
- Erase a
- Combin_4 = Format(Timer - tms, "0.000s ") & k
- End Function
- Function Combin_Final(ItemCount As Long, Number As Long)
- Dim i&, j&, nInd&, nCombCnt&, aComb&(), rTime!
- rTime = Timer
- If ItemCount < 0 Then Err.Raise 30001
- If Number < 0 Then Err.Raise 30002
- If Number > ItemCount Then Err.Raise 30003
- If Number = 0 Then
- nCombCnt = 1
- GoTo EXIT_FUNC
- End If
- ReDim aComb(1 To Number)
- For i = 1 To Number
- aComb(i) = i
- Next
- If ItemCount = Number Then
- nCombCnt = 1
- GoTo EXIT_FUNC
- End If
- For i = Number To ItemCount
- 'aComb(Number) = i
- nCombCnt = nCombCnt + 1
- 'Output combination here
- Next
- For i = Number - 1 To 1 Step -1
- nInd = aComb(i) + 1
- aComb(i) = nInd
- If nInd = ItemCount - Number + i Then
- nCombCnt = nCombCnt + 1
- 'Output combination here
- Else
- For j = i + 1 To Number - 1
- nInd = nInd + 1
- aComb(j) = nInd
- Next
- For nInd = nInd + 1 To ItemCount
- 'aComb(Number) = nInd
- nCombCnt = nCombCnt + 1
- 'Output combination here
- Next
- i = Number
- End If
- Next
- EXIT_FUNC:
- Erase aComb
- Combin_Final = Format(Timer - rTime, "0.000s ") & nCombCnt
- End Function
复制代码 |
|