请问怎样观察一段程序运行的时间呢?(100分)

  • 主题发起人 主题发起人 小羽
  • 开始时间 开始时间

小羽

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个程序,里面用了差分运算,我想知道它到底用了多少时间,应该怎么做呢?
 
开始时取时间赋给一个变量,结束时再取一次时间,两者相减。不知可否。
 
当然可以了,精确到毫秒哦。
 
请问取时间的函数有哪些,谢谢!
 
GetTickCount;可以取得时间;
var t1,t2,t3:Cardinal;

t1:=GetTickCount;
while true do
begin
//....
end;
t2:=GetTickCount;
t3:=2-t1;///
 
青侠的算法有一点小问题。 gettickcount的值每47.5天左右会重置为从0开始。 所以有可能第二次gettickcount取到的值比第一次的小[:D]
取时间可以用now函数
var
d: TDateTime;
begin
d := now;
...... 你的耗时的程序代码
showmessage('程序运行了'+formatdatetime('d"天"h"小时"n"分钟"s"秒"ms"毫秒"', now-d));
end;
 
这个好像只能精确到18ms左右

-------------------
http://www.8421.org
 
function TForm1.gettime: double;
var int1,int2:int64;
begin
QueryperformanceCounter(int1);
Queryperformancefrequency(int2) ;
result:=int1/int2;
end;
以上这个函数计算开机以来的时间,可以精触到纳秒!

我用它来观察,一个表格的数据进行排序所花的时间!
程序如下:
procedure TForm1.ToolButton2Click(Sender: TObject);

var maxnum,i,j,eee:integer;
tempstr:string;
ddd:double;
const b:boolean=false;
begin
ddd:=gettime;
eee:=2;
case eee of
1:begin
for i:=1 to stringgrid1.RowCount-2 do
for j:=1 to stringgrid1.Rowcount-2 do
begin //冒泡算法,还可以用插入排序法。
if (strtoint(stringgrid1.cells[2,j])<
strtoint(stringgrid1.cells[2,j+1]))xor b then
begin
tempstr:=stringgrid1.cells[2,j];
stringgrid1.cells[2,j]:=stringgrid1.cells[2,j+1];
stringgrid1.cells[2,j+1]:=tempstr;

tempstr:=stringgrid1.cells[1,j];
stringgrid1.cells[1,j]:=stringgrid1.cells[1,j+1];
stringgrid1.cells[1,j+1]:=tempstr;
end;
end;
tempstr:='冒泡';
end;
2:begin
for i:=2 to stringgrid1.RowCount -1 do
for j:=i downto 2 do
begin
if (strtoint(stringgrid1.Cells[2,j])
>strtoint(stringgrid1.Cells[2,j-1]))xor b
then
begin
tempstr:=stringgrid1.cells[2,j];
stringgrid1.cells[2,j]:=stringgrid1.cells[2,j-1];
stringgrid1.cells[2,j-1]:=tempstr;

tempstr:=stringgrid1.cells[1,j];
stringgrid1.cells[1,j]:=stringgrid1.cells[1,j-1];
stringgrid1.cells[1,j-1]:=tempstr;
end
else
break;
end;
tempstr:='插入';
end;
end;
b:=not b;
ddd:=gettime-ddd;
caption:='耗时 '+floattostr(ddd)+'毫秒';
toolbar1.Buttons[1].Caption :=tempstr+'排序';
end;
这只是一份薄礼啦!请问:可爱的girl,如果一个中专生,他连中专毕业证书
都给弄丢了,那他能不能考研呢?哪怕是一个毕业证书!
 
后退
顶部