DELPHI进行浮点运算效率的问题,如何改进呢?
一段简单的代码就是循环和计算,在同一机器上delphi7运行时间7秒,VC运行时间0.2秒。听说DELPHI进行浮点运算效率较低,有没有改进的方式?
如果屏蔽这句lat:=((pi/4-arctan(power(b/a,1/n)))*2)*180/pi;运算时间差不多可以减少一半。
示意代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,math;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure xy_to_jw(const x,y,c,k:integer;const n,zxjd,a,zxy,xs:double);
end;
var
Form1: TForm1;
lon,lat:double;
implementation
{$R *.dfm}
procedure TForm1.xy_to_jw(const x,y,c,k:integer;const n,zxjd,a,zxy,xs:double);
var
b:double;
begin
if y+zxy-k/2<>0 then
lon:=arctan((x-c/2)/(y+zxy-k/2))*180/pi/n+zxjd
else
lon:=90/n+zxjd;
b:=(x-c/2)*(x-c/2)+(y+zxy-k/2)*(y+zxy-k/2);
b:=xs*sqrt(b);
lat:=((pi/4-arctan(power(b/a,1/n)))*2)*180/pi;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,j :integer;
t1,t2,t3:int64;
lon,lat:double;
begin
queryperformancecounter(t1);
for j:=0 to 4000-1 do
begin
for i:=0 to 6000-1 do
begin
xy_to_jw(i,j,6000,4800,0.72,115,11423.8,7529,0.922);
end;
end;
queryperformancecounter(t2);
queryperformancefrequency(t3);
self.Caption :=format('%.1f',[(t2-t1)/t3])+'秒';
end;
end.