Recent

Author Topic: get time since program runs  (Read 10383 times)

Flaze07

  • New Member
  • *
  • Posts: 36
get time since program runs
« on: July 05, 2017, 07:45:46 am »
hi is there any equivalent to ctime ??(I know that there's DateUtils and sysutils for time but those are...kinda hard   :-[, (I can use them tho (I have made a class for it too) )  )
this code in C
Code: [Select]
int main()
{
    while (true)
   {
        time_t cooldown = clock() + 100;
        if (clock() >= cooldown)
       {
            std::cout << clock() << std::endl;
            cooldown = clock() + 100;
        }
     }
}
« Last Edit: July 05, 2017, 08:11:44 am by Flaze07 »
pascal is good for learning programming language

Flaze07

  • New Member
  • *
  • Posts: 36
Re: get time since program runs
« Reply #1 on: July 05, 2017, 08:15:14 am »
so..here's a simple class for a similiar thing

Code: Pascal  [Select][+][-]
  1. type
  2.   TClock = class
  3.   private
  4.     first : TDateTime;
  5.     last : TDateTime;
  6.   public
  7.     constructor Create;
  8.     function getElapsed : Int64;
  9.     function restart : Int64;
  10.   end;
  11.  
  12. implementation
  13.  
  14. constructor TClock.Create;
  15. begin
  16.   first := Now;
  17. end;
  18.  
  19. function TClock.getElapsed : Int64;
  20. var
  21.   DiffTime : Int64;
  22. begin
  23.   last := Now;
  24.   DiffTime := MilliSecondsBetween(first, last);
  25. end;
  26.  
  27. function TClock.restart : Int64;
  28. begin
  29.   first := Now;
  30.   exit(getElapsed);
  31. end;
  32.  
  33. end.    
  34.  

and here's how you use it
Code: Pascal  [Select][+][-]
  1. var
  2.   clock : TClock;
  3.   cooldown : Int64 = 100;
  4. begin
  5.  clock := TClock.Create;
  6.  while (true) do
  7.  begin
  8.    if (clock.getElapsed >= cooldown) then
  9.    begin
  10.      writeln('lel');
  11.      clock.restart;
  12.    end;
  13.  end;
  14. end.
  15.  

is this acceptable ?
pascal is good for learning programming language

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: get time since program runs
« Reply #2 on: July 05, 2017, 09:00:42 am »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: get time since program runs
« Reply #3 on: July 05, 2017, 10:18:49 am »
Nope.
Try this:
Code: Pascal  [Select][+][-]
  1. program runtimefun;
  2. {$mode objfpc}
  3. uses sysutils;
  4. type
  5.   TTimeSinceProgramRunClass = class
  6.   strict private
  7.   class var StartTime:TdateTime;
  8.   public
  9.   class constructor create;
  10.   class function Since:TDateTime;
  11.   class function TimeRunning:TDateTime;
  12.  end;
  13.  
  14. class constructor TTimeSinceProgramRunClass.Create;
  15. begin
  16.   StartTime :=Now;
  17. end;
  18. class function TTimeSinceProgramRunClass.Since:TDateTime;
  19. begin
  20.   Result  := StartTime;
  21. end;
  22.  
  23. class function TTimeSinceProgramRunClass.TimeRunning:TDateTime;
  24. begin
  25.   Result := Now-StartTime;
  26. end;
  27.  
  28. begin
  29.   writeln(FormatDateTime('yyyy-mm-dd hh:nn:sss',TTimeSinceProgramRunClass.Since));
  30.   Sleep(1000);
  31.   writeln(FormatDateTime('hh:nn:sss',TTimeSinceProgramRunClass.TimeRunning));
  32. end.

The class constructor is only called at program startup.
« Last Edit: July 05, 2017, 10:20:29 am by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: get time since program runs
« Reply #4 on: July 05, 2017, 10:22:59 am »
so..here's a simple class for a similiar thing
Yes.... but not yours, but mine  8-) Good effort, though...Add the routines from your class to mine as class methods and I am happy.

Or (your code adapted:
Code: Pascal  [Select][+][-]
  1. type
  2.   TClock = class
  3.   private
  4.     class var first : TDateTime;
  5.     class var last : TDateTime;
  6.   public
  7.     class constructor Create;
  8.     class function getElapsed : Int64;
  9.     class function restart : Int64;
  10.   end;
  11.  
  12. implementation
  13.  
  14. class constructor TClock.Create;
  15. begin
  16.   first := Now;
  17. end;
  18.  
  19. class function TClock.getElapsed : Int64;
  20. begin
  21.   last := Now;
  22.   Result := MilliSecondsBetween(first, last);
  23. end;
  24.  
  25. class function TClock.restart : Int64;
  26. begin
  27.   first := Now;
  28.   exit(getElapsed);
  29. end;
  30.  
  31. end.    
  32.  
« Last Edit: July 05, 2017, 10:42:55 am by Thaddy »
Specialize a type, not a var.

Flaze07

  • New Member
  • *
  • Posts: 36
Re: get time since program runs
« Reply #5 on: July 05, 2017, 10:25:18 am »
thank you :D
pascal is good for learning programming language

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: get time since program runs
« Reply #6 on: July 05, 2017, 10:28:01 am »
Post crossed. See above I adapted your code...
Specialize a type, not a var.

Flaze07

  • New Member
  • *
  • Posts: 36
Re: get time since program runs
« Reply #7 on: July 05, 2017, 10:30:46 am »
yeah..
also
I just realized the function getElapsed return nothing :-[
pascal is good for learning programming language

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: get time since program runs
« Reply #8 on: July 05, 2017, 10:36:45 am »
It returned nothing because you have not set the result. It should be:

Code: Pascal  [Select][+][-]
  1. function TClock.getElapsed : Int64;
  2. begin
  3.   last := Now;
  4.   result := MilliSecondsBetween(first, last);
  5. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: get time since program runs
« Reply #9 on: July 05, 2017, 10:37:03 am »
Yes. I corrected that...In my code adaption (2)...

Btw: this is a typical example where class vars and functions are appropriate.
« Last Edit: July 05, 2017, 10:40:47 am by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: get time since program runs
« Reply #10 on: July 05, 2017, 10:39:06 am »
This should be removed, it serves no use.

Code: Pascal  [Select][+][-]
  1. var
  2.   DiffTime : Int64;

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: get time since program runs
« Reply #11 on: July 05, 2017, 10:42:03 am »
Hmm currently, yes, [edit] corrected the code and removed.
Anyway Flaze07 came up with some very useful code  8-)
« Last Edit: July 05, 2017, 10:46:11 am by Thaddy »
Specialize a type, not a var.

Flaze07

  • New Member
  • *
  • Posts: 36
Re: get time since program runs
« Reply #12 on: July 05, 2017, 10:51:28 am »
Anyway Flaze07 came up with some very useful code  8-)

Haha yeah  8-) got the idea from sf::timer
pascal is good for learning programming language

Flaze07

  • New Member
  • *
  • Posts: 36
Re: get time since program runs
« Reply #13 on: July 05, 2017, 06:48:12 pm »
so I added
Code: Pascal  [Select][+][-]
  1. type
  2.     milliseconds = Int64;
  3.  

is this acceptable ?
because it made much more sense for a variable to be type of milliseonds rather than int
pascal is good for learning programming language

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: get time since program runs
« Reply #14 on: July 05, 2017, 07:08:31 pm »
I think it is acceptable, for readability. But I personally will create a new type only it is really needed.

 

TinyPortal © 2005-2018