Lazarus
September 02, 2010, 03:26:01 pm
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: Welcome back to Lazarus forum
Home
Forum
Help
Search
Login
Register
Downloads
Daily Snapshots
Wiki
Bugtracker
Developer Blog
Mailing List
IRC channel
Follow us on Twitter
Free pascal
Other languages
Useful Wiki Links
FAQ
Project Roadmap
Getting the Source
Screenshots
About donations (wiki)
Search
Advanced search
Articles
PeaZip 3.0 file and archive manager
Other languages
RC1 of FacturLinEx 2.0
Kelime Ezberim v1.2 (Word Memorizing NOT:Turkish program )
sQueryDNS a tool for Query DNS Servers
Lazarus
>
Forum
>
Using the Lazarus IDE
>
General
>
Measuring time
Pages: [
1
]
« previous
next »
Print
Author
Topic: Measuring time (Read 2699 times)
byronarn
Newbie
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!!!
Logged
theo
Global Moderator
Hero Member
Posts: 1336
Re: Measuring time
«
Reply #1 on:
January 02, 2009, 08:24:15 pm »
GetTickCount (unit LCLIntf). Difference from A to B is in Milliseconds.
Logged
Phil
Hero Member
Posts: 979
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
Logged
byronarn
Newbie
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.
Logged
byronarn
Newbie
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?
Logged
Phil
Hero Member
Posts: 979
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
Logged
theo
Global Moderator
Hero Member
Posts: 1336
Re: Measuring time
«
Reply #6 on:
January 02, 2009, 10:15:08 pm »
Quote from: byronarn on January 02, 2009, 09:43:49 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
»
Logged
byronarn
Newbie
Posts: 9
Re: Measuring time
«
Reply #7 on:
January 02, 2009, 11:49:19 pm »
Quote from: theo on January 02, 2009, 10:15:08 pm
Quote from: byronarn on January 02, 2009, 09:43:49 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
»
Logged
theo
Global Moderator
Hero Member
Posts: 1336
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.
Logged
byronarn
Newbie
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.
Logged
theo
Global Moderator
Hero Member
Posts: 1336
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.
Logged
byronarn
Newbie
Posts: 9
Re: Measuring time
«
Reply #11 on:
January 03, 2009, 03:16:45 am »
Quote from: theo on January 03, 2009, 02:42:08 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
»
Logged
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Announcements
-----------------------------
=> Team
=> Third party
-----------------------------
Using the Lazarus IDE
-----------------------------
=> Editor
=> Designer
=> Debugger
=> Options
=> General
-----------------------------
Programming
-----------------------------
=> General
=> LCL
=> Databases
=> Graphics
=> Networking
=> Widgetset
===> Carbon
===> Cocoa
===> GTK
===> QT
===> Win32/64
===> WinCE
===> Other
=> OS
===> Linux
=====> arm
===> OSX
===> Windows
===> Windows CE
===> Other
=> Packages and Libraries
===> Ported from Delphi/Kylix
===> General
===> KOL
-----------------------------
Installation
-----------------------------
=> Linux
=> Windows (32/64)
=> Mac OS X
=> PDAs and Smartphones
=> General
-----------------------------
Suggestions
-----------------------------
=> IDE/CodeTools
=> LCL
-----------------------------
Miscellaneous
-----------------------------
=> Translations
=> Jobs
=> Other
Recent
Write a SOAP service for ...
by
jim99
[
Today
at 02:50:54 pm]
lazarus crashes on file o...
by
TCase
[
Today
at 02:33:34 pm]
StringGrid Strangeness
by
faber
[
Today
at 02:05:58 pm]
لماذا حجم الملفات الناتجة...
by
skalogryz
[
Today
at 12:59:00 pm]
Stretch picture from TIma...
by
faber
[
Today
at 11:29:28 am]
MOVED: Sending file (WM5)
by
felipemdc
[
Today
at 11:24:32 am]
Path to application
by
kamischi
[
Today
at 08:17:00 am]
Runtime dependencies, X11...
by
kamischi
[
Today
at 07:27:52 am]
Sending file (WM5)
by
fredycc
[
Today
at 04:19:34 am]
Transparent PNG showed in...
by
JoshyFun
[September 01, 2010, 11:22:23 pm]
TinyPortal v1.0 beta 4 ©
Bloc
Loading...