vb 代码 执行 速度 满意再加分

来源:百度知道 编辑:UC知道 时间:2024/06/18 05:59:11
如何计算一段代码执行完所消耗的时间?比如代码如下 怎么加个代码计算FORM这段代码用时。
Private Sub Form_Load()
Print "时间"
End Sub
老是看到 某某代码比某某代码快20倍。快N陪 都不知道怎么计算出来了。有什么更准确的根据?

这个函数我怎么在API里没找到。。timeGetTime
测试过了~GetTickCount 这个函数更稳定点。偶尔还能更快许多
timeGetTime 经常波动 而且和gettickcount 时间差不多 代码如下
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub Command1_Click()
e = GetTickCount
For i = 1 To 1000
w = w & i
Next i
Text1.Text = Text1.Text & w
Text1.Tag = Text1.Text
Text1.Text = Text2.Text
Text2.Text = Text1.Tag
s = GetTickCount
y = s - e
Print y
End Sub

刚才写错了.gettickcount 只能精确到10MS. 用 timegettime 能精确到 1MS.

给你写了个源码..

Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Private Sub Command1_Click()
Dim b As Long, e As Long, s As Long
b = timeGetTime
For i = 1 To 10
Cls
Print i
Next i
e = timeGetTime
s = e - b
Print "代码执行时间 " & s & " 毫秒"
End Sub

用Timer控件不知道能否实现,跟ASP里面的原理差不多,就是用Timer()来计算一头一尾的执行时间...

可以用Timer函数来返回时间,精确到一秒的小数部分。
比如下面两个程序,添加两个按钮,一个文本框
同样是把文本框里填上从1到10000
一个用控件属性做,一个用变量做,同样的结果
前者需用10多秒,后者只用0.2秒

Option Explicit

Private Sub Command1_Click()
Dim i As Integer, t As Single
t = Timer
For i = 1 To 10000
Text1.Text = Text1.Text & i
Next i
t = Timer - t
Print "用时" & t & "秒"
End Sub

Private Sub Command2_Click()
Dim i As Integer, s As String, t As Single
t = Timer
For i = 1