代码我试验了,确实比VB慢一点.Delphi是1.9秒.VB是1.6秒.这个是没法子的事情Delphi的编译器对浮点运算完全没有优化,怎么写就怎么编译.从Delphi1到现在都是不优化浮点的. 而VB6和VC6是共享编译后端的.在浮点方面有优化. 如果你用高版本的VC,优化全部开这个简单循环更夸张,VC编译器会直接帮你把简单循环的结果计算出来.这样就一行指令赋值结果给G.虽然Delphi的编译浮点一直是不优化的,但是Delphi64位编译器是重写的,后端使用了LLVM优化.LLVM可以说是目前最强的编译器优化. 这个算法我同样试了一下 Delphi32 VB6 Delphi64 1.9s 1.6s 0.3s Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Command1_Click()Dim G As Double Dim TT As Long Dim i As LongTT = GetTickCount() G = 1 For i = 1 To 10000000 Step 1 G = G * 0.999 NextMe.Caption = "G=" + Str(G) + " 耗时:" + Str(GetTickCount - TT) End Subvar G: double; TT: Longint; i: Integer; begin TT := GetTickCount; G := 1; for i := 1 to 10000000 do begin G := G * 0.999; end; Caption := 'G='+ FloatToStr(G) + ' 耗时: '+ Inttostr(GetTickCount - TT);end;
另外整数运算VB则完全不是Delphi的对手. Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Command1_Click()Dim G As Long Dim TT As Long Dim i As LongTT = GetTickCount() G = 1 For i = 1 To 10000000 Step 1 If G < &HFFFFFF Then G = G * 2 Else G = G / 2 End If NextMe.Caption = "G=" + Str(G) + " 耗时:" + Str(GetTickCount - TT) End Sub var G: Integer; TT: Longint; i: Integer; begin TT := GetTickCount; G := 1; for i := 1 to 10000000 do begin if G< $FFFFFF then G := G * 2 else G := G div 2; end; Caption := 'G='+ FloatToStr(G) + ' 耗时: '+ Inttostr(GetTickCount - TT);end;但是Delphi64反而比Delphi32慢一些.... Delphi32 Delphi64 VB6 15 45 171
hi ~ 除以 1000 这个我知道edit2.Text 显示的值 = 2016 我写在这里时, 我已经把它除以 1000 了 也就是我上面写ˇ的 2.016 秒 这部分是我不好, 让你误解以为没有除以 1000 我写的 2秒, 2.016秒 已经除以过1000 了
2. 不要在IDE里运行,编译成exe后单独运行这个exe.。2秒得多慢哪,你的什么电脑?不会是中毒了吧。我用农企的廉价APU电脑测试,0.015秒,显然是GetTickCount的精度不够了,改用QueryPerformancePfrquency、QueryPerformanceCounter计时间,0.005秒。没有VB,所以无法对比。
而VB6和VC6是共享编译后端的.在浮点方面有优化.
如果你用高版本的VC,优化全部开这个简单循环更夸张,VC编译器会直接帮你把简单循环的结果计算出来.这样就一行指令赋值结果给G.虽然Delphi的编译浮点一直是不优化的,但是Delphi64位编译器是重写的,后端使用了LLVM优化.LLVM可以说是目前最强的编译器优化.
这个算法我同样试了一下
Delphi32 VB6 Delphi64
1.9s 1.6s 0.3s
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()Dim G As Double
Dim TT As Long
Dim i As LongTT = GetTickCount()
G = 1
For i = 1 To 10000000 Step 1
G = G * 0.999
NextMe.Caption = "G=" + Str(G) + " 耗时:" + Str(GetTickCount - TT)
End Subvar
G: double;
TT: Longint;
i: Integer;
begin TT := GetTickCount;
G := 1;
for i := 1 to 10000000 do
begin
G := G * 0.999;
end; Caption := 'G='+ FloatToStr(G) + ' 耗时: '+ Inttostr(GetTickCount - TT);end;
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()Dim G As Long
Dim TT As Long
Dim i As LongTT = GetTickCount()
G = 1
For i = 1 To 10000000 Step 1
If G < &HFFFFFF Then
G = G * 2
Else
G = G / 2
End If
NextMe.Caption = "G=" + Str(G) + " 耗时:" + Str(GetTickCount - TT)
End Sub
var
G: Integer;
TT: Longint;
i: Integer;
begin TT := GetTickCount;
G := 1;
for i := 1 to 10000000 do
begin
if G< $FFFFFF then
G := G * 2
else
G := G div 2;
end; Caption := 'G='+ FloatToStr(G) + ' 耗时: '+ Inttostr(GetTickCount - TT);end;但是Delphi64反而比Delphi32慢一些....
Delphi32 Delphi64 VB6
15 45 171
迟了一步
我结帖了, 无法分给你分数
感谢您的测试整数先前我有测试过, 输 delphi 很多
内存 Array 读写, Call Sub vb 也输 delphi 很多
迟了一步
我结帖了, 无法分给你分数
感谢您的测试整数先前我有测试过, 输 delphi 很多
内存 Array 读写, Call Sub vb 也输 delphi 很多