//以下差不多精确到22微秒
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Math, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
bGoOn:boolean;
MMSecLong:integer;
lastS:string;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function cyclecount:int64;
asm
db $0f
db $31
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f
ouble;
s:string;
a,c,d,iStart,iDiv:int64;
year,Month,Day,Hour,Min,Sec,Msec,MMsec:word;
begin
c := cyclecount;
sleep(100);
d := cyclecount;
//平均每微秒的周期,奇怪的是用10毫秒、100毫秒和1000毫秒得到的结果差很大
MMSecLong:=(d-c) div (100*1000);
f:= now;
iStart:= cyclecount;
DecodeDate(f,year,Month,Day);
DecodeTime(f,hour,min,sec,msec);
MMsec:=0;
a:=0;
bGoOn:=true;
while bGoOn do
begin
iDiv:= cyclecount ;
MMSec:=MMsec+(iDiv-iStart) div MMSecLong;
iStart:=iDiv;
MSec:= MSec + MMSec div 1000;
MMsec:=MMsec mod 1000;
Sec:= Sec + MSec div 1000;
MSec:=Msec Mod 1000;
Min:=Min+(Sec div 60);
Sec:= sec mod 60;
Hour:=Hour+(Min div 60);
Min:= Min mod 60;
if Hour > 24 then
begin
//重新调用比计算天数简单吧
Button1Click(nil);
exit;
end;
s:=Format('%d-%d-%d %d:%d:%d %d %d',[year,month,day,hour,min,sec,Msec,MMsec]);
if s <> lastS then
begin
ListBox1.Items.Add(s);
lastS:=s;
end;
a:=a+1;
if a > 2000 then
begin
Application.ProcessMessages;
a:=0;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
bGoOn:=false;
end;
end.