加一timer控件,interval属性设为1,enabled设为false.设一全局变量L as long(如果不行就改为double)
在程序开始的时候加一句timer1.enabled=true,在timer1事件中写上L=L+1.
在程序结束时print L,
L的值就是你这段代码运行所花费的时间(单位:毫秒)。
Private Sub Command1_Click()
Dim a As Currency, i As Long
a = Timer '开始计时
For i = 1 To 1000000
DoEvents
Next
a = Timer - a '计时结束
Print a
End Sub
精度1毫秒若还需要更高的精度
就需要用API我有个delphi的例程你不妨参考一下
//取系统级时间精度:
var
c1:int64;
t1,t2:int64;
r1:double;
begin
queryperformancefrequency(c1);//windows api 返回计数频率(intel86:1193180)(获得系统的高性能频率计数器在一毫秒内的震动次数)
queryperformancecounter(t1);//windows api 获取开始计数值
sleep(1000);{do...}//执行要计时的代码
queryperformancecounter(t2);//获取结束计数值
r1:=(t2-t1)/c1;//取得计时时间,单位秒(s)
r1:=(t2-t1)/c1*1000;//取得计时时间,单位毫秒(ms)
r1:=(t2-t1)/c1*1000000;//取得计时时间,单位微秒
showmessage(floattostr(r1));
end;