我对编程很新,我很想知道;定时和优化代码的最佳方法是什么?我目前想要的代码是VBA for MS-Access中的一系列查询,但我也想在VB.NET和ASP.NET中计算代码。
重申一下,为每种语言编码和优化它的最佳方法是什么?
请说明理由和解释,以帮助我了解如何操作。
提前致谢。
答案 0 :(得分:2)
此计时器可能有所帮助:http://support.microsoft.com/kb/233275
利用索引(sargable)的查询运行得更快。如果没有必要,还有其他要点,例如避免订购。通常最好发布似乎花费太长时间的SQL,并且您经常会收到一些提高速度的建议。
答案 1 :(得分:0)
除非你有一个非常大的应用程序,我不会担心工具。您通常可以通过一些debug.print
语句和Timer()
来电来解决任何瓶颈问题。
下面的代码我从一个非常pedantic answer的阵列调整大小:)
尝试注释掉msgbox(),你会在代码窗口的底部看到一个漂亮的小调试信息。
Option Explicit
Sub RedimTest()
Dim tA, tB As Single
tA = RedimTestA(1000000)
tB = RedimTestB(1000000)
MsgBox "Test A takes : " & tA & ", and Test B takes : " & tB
End Sub
Function RedimTestA(iterations As Long) As Single
Dim t As Single
Dim i As Long
Dim aryString() As String
Dim myString As String
t = Timer
Do While i <= iterations
ReDim Preserve aryString(i) As String
aryString(i) = "ABCEFG123"
i = i + 1
Loop
RedimTestA = Timer - t
Debug.Print "RedimTestA: " & Format(RedimTestA, "#0.0000ms")
End Function
Function RedimTestB(iterations As Long) As Single
Dim t As Single
Dim i As Long
Dim aryString() As String
Dim myString As String
t = Timer
ReDim aryString(0) As String
Do While i <= iterations
If i >= UBound(aryString) Then
ReDim Preserve aryString(i * 2) As String
End If
aryString(i) = "ABCEFG123"
i = i + 1
Loop
ReDim Preserve aryString(i - 1) As String ' i - 1 becuase of the final i = i + 1
RedimTestB = Timer - t
Debug.Print "RedimTestB: " & Format(RedimTestB, "#0.0000ms")
End Function