Recent

Author Topic: Measuring time  (Read 43534 times)

byronarn

  • New member
  • *
  • Posts: 9
Measuring time
« on: January 02, 2009, 07:55:06 pm »
Lets say I want to measure how much time passes between two events, and then have my application choose from different options depending upon  much time has passed.  How would i do this?

I assume it requires storing the current time in a variable at the time of the first event, then storing the current time in another variable at the second event, then storing the difference of time in a third variable, then using a set of if-then statements to perform the action.  But what time functions does free pascal have to help me out?

Thanks in advance for any help!!!

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Measuring time
« Reply #1 on: January 02, 2009, 08:24:15 pm »
GetTickCount (unit LCLIntf). Difference from A to B is in Milliseconds.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Measuring time
« Reply #2 on: January 02, 2009, 08:44:26 pm »
Here's a wrapper function I use to handle platform differences with GetTickCount:

function GetTickCount : DWORD;
 {On Windows, this is number of milliseconds since Windows was
   started. On non-Windows platforms, LCL returns number of
   milliseconds since Dec. 30, 1899, wrapped by size of DWORD.
   This value can overflow LongInt variable when checks turned on,
   so "wrap" value here so it fits within LongInt.
  Also, since same thing could happen with Windows that has been
   running for at least approx. 25 days, override it too.}
begin
{$IFDEF MSWINDOWS}
  Result := Windows.GetTickCount mod High(LongInt);
{$ELSE}
  Result := LclIntf.GetTickCount mod High(LongInt);
{$ENDIF} 
end;

(from OrphPort package)

Thanks.

-Phil

byronarn

  • New member
  • *
  • Posts: 9
Re: Measuring time
« Reply #3 on: January 02, 2009, 09:33:46 pm »
Is there no functions that will tell you the time difference in minutes or hours, other than dividing the number by 60,000 or 3,600,000 (respectively)?  Because what I have in mind requires lengths of time in minutes and hours.

byronarn

  • New member
  • *
  • Posts: 9
Re: Measuring time
« Reply #4 on: January 02, 2009, 09:43:49 pm »
I use ubuntu Linux so I guess I would use
Quote
Result := LclIntf.GetTickCount mod High(LongInt);

I hate to sound like a n00b, but I am.  What does LclIntf and "mod High" mean?

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Measuring time
« Reply #5 on: January 02, 2009, 10:11:27 pm »
LclIntf is the cross-platform unit that implements many common Win API functions. Use it instead of Windows if you want your apps to be compilable on OS X and Linux.

mod is a Pascal operator. High is a RTL function. See the FPC documentation.

Thanks.

-Phil

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Measuring time
« Reply #6 on: January 02, 2009, 10:15:08 pm »
I hate to sound like a n00b

It's not very efficient to learn a programming language by asking in a forum. ;-)
For the basics, have a look at:
http://delphibasics.co.uk/
In your case:
http://delphibasics.co.uk/RTL.asp?Name=Mod
http://delphibasics.co.uk/RTL.asp?Name=High
http://delphibasics.co.uk/ByFunction.asp?Main=DatesAndTimes

It's for Delphi, but Lazarus is compatible in this basic stuff.
« Last Edit: January 02, 2009, 10:34:27 pm by theo »

byronarn

  • New member
  • *
  • Posts: 9
Re: Measuring time
« Reply #7 on: January 02, 2009, 11:49:19 pm »
I hate to sound like a n00b

It's not very efficient to learn a programming language by asking in a forum. ;-)
For the basics, have a look at:
http://delphibasics.co.uk/
In your case:
http://delphibasics.co.uk/RTL.asp?Name=Mod
http://delphibasics.co.uk/RTL.asp?Name=High
http://delphibasics.co.uk/ByFunction.asp?Main=DatesAndTimes

It's for Delphi, but Lazarus is compatible in this basic stuff.

Thanks for the resources!

Edit:  I get the following errors about LclIntf:

Quote
unit1.pas(44,16) Error: Identifier not found "LCLIntf"
unit1.pas(44,34) Error: Type mismatch
unit1.pas(54) Fatal: There were 2 errors compiling module, stopping

Here is my full code, with my code in bold:
Quote
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  var press1, press2: tdatetime;
  var diff: integer;
  var strDiff: string;

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  press1:=now;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  press2:=now;
  diff:=LclIntf.GetTickCount mod High(press2-press1);
  str(diff,strDiff);
  label1.caption:=strDiff;

end;

initialization
  {$I unit1.lrs}

end.
« Last Edit: January 03, 2009, 12:37:38 am by byronarn »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Measuring time
« Reply #8 on: January 03, 2009, 01:56:39 am »
You should learn the very basics of Lazarus / Delphi Programming.

LclIntf.GetTickCount is a very special case, where you need to specify in which unit the function is defined.
What Phil said is misleading for newbies.
The important thing is, that you ADD the unit to the uses clause:
 
uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, LclIntf;

You don't have to specify the "Namespace" if you don't use the windows unit as well.

So GetTickCount is enough, and LclIntf.GetTickCount is not necessary.

byronarn

  • New member
  • *
  • Posts: 9
Re: Measuring time
« Reply #9 on: January 03, 2009, 02:23:57 am »
Now I get the error:
Quote
unit1.pas(44,26) Error: Type mismatch

My code:

Quote
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, LclIntf;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  var press1, press2: tdatetime;
  var diff: longint;
  var strDiff: string;

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  press1:=now;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  press2:=now;
  diff:=GetTickCount mod High(press2-press1);
  str(diff,strDiff);
  label1.caption:=strDiff;
end;

initialization
  {$I unit1.lrs}

end.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Measuring time
« Reply #10 on: January 03, 2009, 02:42:08 am »
How long will you play this game? ;-)
Please read a good book about pascal programming.
Your code is complete nonsense.

If you want to measure the time between two events in ms. then do

starttime:=getTickCount;
//wait for something
endtime:=getTickCount-starttime;

endtime is the difference in milliseconds.

byronarn

  • New member
  • *
  • Posts: 9
Re: Measuring time
« Reply #11 on: January 03, 2009, 03:16:45 am »
How long will you play this game? ;-)
Until I get it right. :-) I learn best by trial and error.  Tutorials just put me to sleep.  :-)

Quote from: theo
If you want to measure the time between two events in ms. then do

starttime:=getTickCount;
//wait for something
endtime:=getTickCount-starttime;

endtime is the difference in milliseconds.
Aw, thanks, That fixed everything!  I appreciate it so much!!!
« Last Edit: January 03, 2009, 06:54:21 am by byronarn »

 

TinyPortal © 2005-2018