Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there a way to benchmark some code or the app 2

Status
Not open for further replies.

DelphiAaron

Programmer
Jul 4, 2002
826
AU
has anyone seen a way to test the speed of some code or the whole app ?.

i want to try 2 different pieces of code that gives the same result and see wich one is faster.

Aaron Taylor
John Mutch Electronics
 
A simple way to measure the speed of a piece of code is to measure the time just before your code starts and just after it finishes and then find the difference between the times.

You can use Now() function which gives you the systems time and date at the point of execution. Also have a look at the help for DateTime routines defined in DateUtils unit.


I hope the following helps.


uses
SysUtils, //Now() is defined in SysUtils
DateUtils;//Date time routines are defined in DateUtils

...

var
StartTime:TDateTime;
EndTime:TDateTime;
nMilliseconds:integer;
begin
StartTime:=Now();

//your code here

EndTime:=Now();
nMilliseconds:=MilliSecondsBetween(EndTime,StartTime);
end;

...
 
If it helps, here's what I'm using - it's in a unit and tested so it should be easy enough to snapshot a time through code:

unit ptimer;
{ provides all code for timing of a program using the system time
Delphi 3.0/FPC compatible. }

{ Copyrighted by Glenn9999 at tek-tips.com
Free for use with the last two lines included in the source.
}

interface
uses sysutils;
var
time1, time2: TTimeStamp;

procedure ptimer_start;
function ptimer_finish: longint;

implementation
procedure ptimer_start;
begin
time1 := DateTimeToTimeStamp(Now);
end;

function ptimer_finish: longint;
{ returns program time in MilliSeconds }
begin
time2 := DateTimeToTimeStamp(Now);
ptimer_finish := trunc(TimeStampToMSecs(time2) - TimeStampToMSecs(time1));
end;
end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top