clock, timing? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

clock, timing?

fosseyfossey Posts: 6Member
Hi, i need some help in code.
how can i time the duration of a simple text based program, im using turbo pascal v7, using 'crt' and maybe 'dos' if i need to, i used the gettime command like this; '

h, m, s, hund : Word;
h2,m2,s2,hund2 :word;

function time(w : Word) : String;
var
s : String;
begin

Str(w:0,s);
if Length(s) = 1 then
s := '0' + s;
time := s;
end;

begin
GetTime(h,m,s,hund);
Writeln('your time is ',time(h),':',time(m),':',time(s),'.',time(hund));
end;
'
so i can read/write the time at start and finish, but you cant subtract time, you get some werid number, if i do this sort of command twice how can i work out the differance? is there an easyier way to workout the time from when the program starts and finishes? thanx for anyones help

Comments

  • slicer69slicer69 Posts: 272Member
    Instead of subtracting raw, time, try doing this:

    uses crt, dos;
    var
    h, m, s, hund : Word;
    h2,m2,s2,hund2 :word;
    delta_h, delta_m, delta_s, delta_hund: integer;

    begin
    gettime(h, m, s, hund); { get starting time }
    { do something here }
    gettime(h2, m2, s2, hund2); { get ending time }

    { find difference in time }
    delta_h := h2 - h;
    delta_m := m2 - m;
    delta_s := s2 - s;
    delta_hund := hund2 - hund;

    writeln('Time passed: ', delta_h, ':', delta_m, ':', delta_s, '.', delta_hund);

    end.

  • fosseyfossey Posts: 6Member
    that wont work, u see, if the end time (m2)is less than the star time (m) then it would go below 0; eg 5 min :32 sec
    6 min 10 sec
    time2-time1 =
    6 - 5;
    10 - 30;

    which equals 1 min and some werid number,
    the actual answer is 42 sec
    thanx anyway...

  • ManningManning Posts: 1,621Member
    : Hi, i need some help in code.
    : how can i time the duration of a simple text based program, im using turbo pascal v7, using 'crt' and maybe 'dos' if i need to, i used the gettime command like this; '

    What I would use is this:

    [code]
    { Calculate the number of seconds since midnight
    when the program loads }
    GetTime(H, M, S, Hund);
    StartTime := (H * 3600) + (M * 60) + S;

    { Do stuff here }
    blah;
    blah;
    blah;

    { Calculate the number of seconds since midnight
    when your program is about to end }
    GetTime(H, M, S, Hund);
    EndTime := (H * 3600) + (M * 60) + S;

    { Handle midnight rollover }
    if (EndTime < StartTime) then
    Inc(EndTime, 86400);

    { Calculate the amount of time the program ran for }
    Duration := EndTime - StartTime;

    { Calculate the number of hours/minutes/seconds
    the program ran for }
    Hours := Trunc(Duration / 3600);
    Dec(Duration, Hour * 3600);

    Minutes := Trunc(Duration / 60);
    Dec(Duration, Minute * 60);

    Seconds := Duration;
    [/code]

    That was done off the top of my head so there probably is a better way, but that should work. If you think your program will run for more than a 24 hour period this wont work of course. You'll have to also use GetDate and do some comparison between the day/month/year values.
  • fosseyfossey Posts: 6Member
    that will do me, thats all i need, the prog probably wont run over 5 minutes anyway, thanx heaps!!!
  • fzapperfzapper Posts: 7Member
    Use the BIOS clock count at absolute address $0040:$006C. It increments every 65536/3600 times per second and contains the ticks since midnight (4 bytes). The word at $0040:$006E is the hours since midnight.

    Do something like:

    PROGRAM Timer;
    VAR
    LOWord : Word Absolute $0040:$006C; {Ticks since begining of hour}
    HiWord : Word Absolute $0040:$006E; {Hours cince midnight}
    OldLO, Diff, X : Word;
    R : Real; {For fractional seconds}

    BEGIN
    OldLO := LOWord;
    Writeln('Ticks since beginning of the hour: ',OldLO);
    Writeln('Dont just do something! Stand there!');
    Writeln('Timing loop....');
    For X := 1 to 65535 do
    R := R;
    Diff := LOWord;
    Writeln('Done timing. Ticks are now ',Diff);
    Diff := Diff - OldLO;
    Writeln('Difference is ',Diff,' ticks');
    R := Diff * 3600/65536;
    Writeln('Difference is ',R:10:2,' seconds');
    END.

    If you're unning under NT/W2k youll have to use Int 1Ah to get the ticks from the real time clock :

    13.2.12 INT 1Ah - Real Time Clock

    Instruction: int 1ah
    BIOS Operation: Real time clock services
    Parameters: ax, cx, dx
    There are two services provided by this BIOS routine- read the clock and set the clock. The PC's real time clock maintains a counter that counts the number of 1/18ths of a second that have transpired since midnight. When you read the clock, you get the number of "ticks" which have occurred since then. When you set the clock, you specify the number of "ticks" which have occurred since midnight. As usual, the particular service is selected via the value in the ah register.

    13.2.12.1 AH=0: Read the Real Time Clock

    If ah = 0, then int 1ah returns a 33-bit value in al:cx:dx as follows:

    Reg Value Returned
    dx L.O. word of clock count
    cx H.O. word of clock count
    al Zero if timer has not run for more than 24 hours
    Non-zero otherwise.

    If you *really* want to get fancy read the 8254 timer chip register 0 for the 1/1193180 seconds count that increments the BIOS:

    http://www.techedge.com.au/tech/8253tec.htm


    : Hi, i need some help in code.
    : how can i time the duration of a simple text based program, im using turbo pascal v7, using 'crt' and maybe 'dos' if i need to, i used the gettime command like this; '
    :
    : h, m, s, hund : Word;
    : h2,m2,s2,hund2 :word;
    :
    : function time(w : Word) : String;
    : var
    : s : String;
    : begin
    :
    : Str(w:0,s);
    : if Length(s) = 1 then
    : s := '0' + s;
    : time := s;
    : end;
    :
    : begin
    : GetTime(h,m,s,hund);
    : Writeln('your time is ',time(h),':',time(m),':',time(s),'.',time(hund));
    : end;
    : '
    : so i can read/write the time at start and finish, but you cant subtract time, you get some werid number, if i do this sort of command twice how can i work out the differance? is there an easyier way to workout the time from when the program starts and finishes? thanx for anyones help
    :
    :

Sign In or Register to comment.