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.

how to create a timer to time a piece of code in pascal

muaz_farooqmuaz_farooq Posts: 4Member
hi everybody,

i want to create a timer so that i could time my program to calculate it's time efficiency. So how would i create it in pascal.

Comments

  • zibadianzibadian Posts: 6,349Member
    : hi everybody,
    :
    : i want to create a timer so that i could time my program to calculate it's time efficiency. So how would i create it in pascal.
    :
    You can use Now or GetTime, depending on your compiler. The general pseudo-code looks like this:
    [code]
    Get current system clock
    Perform lengthy process
    Get current system clock
    Calculate run time.
    [/code]
  • dolev9dolev9 Posts: 147Member
    its pritty simple.

    check the gettime and settime procedures.
    you need to use dos unit to use them.
    these procedures exist in the tp help for more info.


    gettime(var houre,minute,second,milisecond);
    (gives you the current time)

    settime(var houre,minute,second,milisecond);
    (set time to (changes the clock))

    getdate(var year,month,day,dayofweek);
    (gives you the current date)

    setdate(var year,month,day,dayofweek);
    (set the current date)



    to check how long takes program to do something do

    gettime(hr1,mn1,sc1,ms1);
    a := ms1+sc1*100+ms1*6000+hr1*360000;
    ...
    ...
    ...
    gettime(hr2,mn2,sc2,ms2);
    b := ms2+sc2*100+ms2*6000+hr2*360000;

    c := a-b {milliseconds defferance}


    when hour is 23:59:59:999
    it wont work.
    to make it work between days you should use also the getdate/setdate procedures.


    hopefully i helped

    dolev



  • Phat NatPhat Nat Posts: 757Member
    [b][red]This message was edited by Phat Nat at 2005-5-2 20:45:6[/red][/b][hr]
    : hi everybody,
    :
    : i want to create a timer so that i could time my program to calculate it's time efficiency. So how would i create it in pascal.
    :


    You can also get a running clock (18.2 times/second) from memory. I'm not sure if it changes location, but you can just do a display of your lower memory ($0000:$0000 -> $0100:0000) to your screen and find the part that's counting. On my machine it's at Mem[$0046:$000C]; It's really quick returning the memory, so there is no "added" time to your testing due to interrupt calls, etc.

    [code]
    USES Crt, Dos;
    VAR
    X : Word;
    L : LongInt;
    Begin
    Repeat
    GotoXY(1,1);
    Move(Mem[$0046:$000C],L,4);
    WriteLn(L);
    { This displays the lower memory }
    { For X := 0 to 2500 Do
    Mem[$B800:X*2] := Mem[$0000:$0000+X];
    }

    Until Keypressed;
    End.
    [/code]

    If you just save the memory location to a variable such as [b]L[/b] above, you can save it again to another variable and suctract. The second will always be larger (except in the very rare occasion that the clock actually fills the whole LongInt) so when you subtract the 2 numbers you can get the # of seconds it took by dividing by 18.2

    [code]
    USES Crt, Dos;
    VAR
    TStart : LongInt;
    TEnd : LongInt;
    Begin
    ClrScr;
    Move(Mem[$0046:$000C],TStart,4);
    Repeat
    GotoXY(1,1); WriteLn('Random # = ',Random(240));
    Until Keypressed;
    Move(Mem[$0046:$000C],TEnd,4);
    WriteLn('It looped for ',TEnd-TStart,' clock ticks.');
    WriteLn('It looped for ',(TEnd-TStart)/18.2:1:1,' seconds.');
    End.
    [/code]


    Used this many times. Comes in very handy ;)

    Phat Nat



  • est69dogest69dog Posts: 5Member
    just add one "if"

    gettime(h,m,s,ms);
    if h>12 then h:=h-12;

    make sure that you add it both times
    it will if you dont use that program for 12h+
  • acetechcnacetechcn Posts: 28Member
    This post has been deleted.
  • emily2010001emily2010001 Posts: 6Member
    This post has been deleted.
Sign In or Register to comment.