2013-02-12 4 views
3

Delphi7에서 밀리 초 또는 나노 초 단위로 타이머를 찾고 있습니다. 순차 검색으로 세 ISAM 파일의 속도를 확인해야합니다. 첫 번째 ind 파일은 "record_0"에서 "record_50"과 같은 50 개의 문자열을 포함합니다. 두 번째 - "record_0"- "record_500"및 세 번째 - "record_0"- "record_5000". 모든 것을 구현했지만 타이머를 만드는 방법을 모르겠습니다. 각 ISAM 파일의 마지막 항목과 문자열을 비교합니다. 여기에 최초의 IND 파일에 대한 내 코드입니다 : 나는 그것을 호출 할 때 밀리 초 또는 나노초 단위로 카운트를 시작해야하며, 문자열이 발견되면 중지하도록 내가 절차/기능을 필요Delphi - 시작/정지 기능으로 밀리 초 또는 나노초 단위로 타이머를 만드는 방법

procedure TForm1.Button1Click(Sender: TObject); 
    var i:integer; 
    var content : String[20]; 
    var indexCounter:integer; 
    var keyword:string; 
begin 
    //First ISAM file 
    AssignFile(indF1, 'index1.ind'); 
    ReWrite(indF1); 
    Reset(indF1); 
    for i:=0 to 49 do begin 
    content := 'record_'; 
    content := content + IntToStr(i+1); 
    index1.index1 := content; 
    index1.position1 := FileSize(indF1); 
    Seek(indF1, FileSize(indF1)); 
    write(indF1, index1); 
    end; 
    CloseFile(indF1); 
    Label12.Caption := FileSizeStr('index1.ind'); 

    //Sequential search in first ind file 
    Reset(indF1); 
    keyword := 'record_50'; 
    indexCounter := 0; 
    //start timer 
    while not Eof(indF1) do begin 
    Seek(indF1, indexCounter); 
    Read(indF1, Index1); 
    if (keyword = Index1.index1) then begin 
     //stop timer; 
     //Label20 := milliseconds/nanoseconds; 
     //return/break while loop (result := -1; exit;) ??? 
    end; 
    indexCounter := indexCounter + 1; 
    end; 

은 (는 마지막 문자열입니다 각 ind 파일)을 표시하고 모든 파일을 탐색하는 데 소요 된 시간을 표시합니다. 또한 while 루프를 깨는 방법을 모르겠습니다. 미리 감사드립니다.

+4

왜 당신은 여전히 ​​파스칼 I/O를 사용하고 있습니다 :

lFreq: Int64; InitialF, FinalF: Int64; if QueryPerformanceFrequency(lFreq) then // hi-res timer is supported else // hi-res timer is not supported QueryPerformanceCounter(InitialF); // do something you want to time QueryPerformanceCounter(FinalF); // duration of the time of something is FinalF - InitialF in "units" // divide by lFreq to get the amount of time in seconds, // this will be an Extended type. 

답변

11

여기에 설명 된 TStopWatch 클래스는 "delphi-high-performance-timer-tstopwatch"에 필요한 모든 기능 (Delphi-7)이 있습니다.

이것은 단위 진단의 고급 레코드로 최신 델파이 버전 (Delphi-2010)에서 구현됩니다.

예 :

var 
    sw : TStopWatch; 
    elapsedMilliseconds : cardinal; 
begin 
    ... 
    sw := TStopWatch.Create() ; 
    try 
    sw.Start; 

    while not Eof(indF1) do begin 
     Seek(indF1, indexCounter); 
     Read(indF1, Index1); 
     if (keyword = Index1.index1) then begin 
     sw.Stop; 
     Label20.Caption := IntToStr(sw.ElapsedMilliseconds); 
     break; // break while loop 
     end; 
     indexCounter := indexCounter + 1; 
    end; 
    ... 
    finally 
    sw.Free; 
    end; 
end; 

는 while 루프를 중단하려면, 단지 조건부 테스트 내부 break;을한다.

+1

@iamjoosy, 링크는 대답에서 설명한대로 델파이 7에서 작동하는 클래스입니다. –

+0

@ LU RD oops, 링크를 놓쳤습니다. – iamjoosy

+0

당신이 말했듯이'TStopwatch'는 고급 기록입니다. 따라서'.Free' 메소드가 없기 때문에 파괴 할 필요가 없습니다. –

1

제다이 JCL의 JclCounter을 사용하십시오. 또는 Jedi와 함께 가고 싶지 않은 경우 Win API QueryPerformanceCounter를 사용하십시오.

9

QueryPerformanceFrequencyQueryPerformanceCounter을 사용하십시오. 첫 번째 함수는 초당 단위 수를 반환하고 두 번째 함수는 값을 반환합니다.

var 
StartTime : Cardinal; 
begin 
    StartTime := GetTickCount; 
    //code to do 
    ShowMessage(Format('Elapsed time %d ms', [GetTickCount - StartTime])); 
+0

어떻게해야할지 모르겠다. 내 코드로 구현할 수 있습니까? –

+8

@Ezio_ 혼자서 할 수있는 모든 정보가 여기 있습니다. StackOverflow는 me_ service 코드를 쓰는 것이 아니므로, 여러분의 노력을 보여주십시오. 더 궁금한 점이 있으면 여기로 돌아와서 필요한 도움을 받으십시오. – jachguate

4

이 간단한 코드를 발견?
+0

PC가 49 일 (예 : 2^32 밀리 초) 동안 계속 실행되는 경우, GetTickCount가 0으로 감에 따라 경과 시간이 마이너스가 될 위험이 있습니다. Windows Vista 이상에서는 5 억 5 천 5 백만년 동안 충분해야하는 64 비트 부호없는 정수를 사용하여 밀리 초를 계산하는 GetTickCount64 함수는 테스트 할 수 없습니다. – Flynn1179