# Measuring the time of an algorithm in seconds

Hi all,

I need to measure the time it takes for my algorithm to make some computation. So I do:

begin
t1 := GetSystemTime;
DoALotOfMath;
t2 := GetSystemTime;
writeln('My program took ', t2 - t1, 'seconds to run');
end.

How exactly do I get the time ?
Are variables t1 and t2 integers or longints ?

Thanks
Leonel

• : Hi all,
:
: I need to measure the time it takes for my algorithm to make some computation. So I do:
:
: begin
: t1 := GetSystemTime;
: DoALotOfMath;
: t2 := GetSystemTime;
: writeln('My program took ', t2 - t1, 'seconds to run');
: end.
:
: How exactly do I get the time ?
: Are variables t1 and t2 integers or longints ?

Neither, if you go look it up on MSDN you'll see GetSystemTime takes a pointer to a SYSTEMTIME structure. Replace the calls to GetSystemTime with GetTickCount and then t2 - t1 will be the number of milliseconds the algorithm took to run. Divide by 1000 and you'll have the number of seconds.
• : : Hi all,
: :
: : I need to measure the time it takes for my algorithm to make some computation. So I do:
: :
: : begin
: : t1 := GetSystemTime;
: : DoALotOfMath;
: : t2 := GetSystemTime;
: : writeln('My program took ', t2 - t1, 'seconds to run');
: : end.
: :
: : How exactly do I get the time ?
: : Are variables t1 and t2 integers or longints ?
:
: Neither, if you go look it up on MSDN you'll see GetSystemTime takes a pointer to a SYSTEMTIME structure. Replace the calls to GetSystemTime with GetTickCount and then t2 - t1 will be the number of milliseconds the algorithm took to run. Divide by 1000 and you'll have the number of seconds.
:

Well, that will help a lot. Please, I would like to know more. I am using the Free Pascal compiler, and I use Pascal in a programming course.
I did not know there was a procedure called 'GetSystemTime', I only wrote it as an example, just like I do not know whether there is a DoALotOfMath procedure. These two, GetSystemTime and GetTickCount, are they available in every compiler ? Which compiler do you use ?
One last question: does GetTickCount really count milliseconds or does it count CPU cycles instead ?

Thank you once again

• : Well, that will help a lot. Please, I would like to know more. I am using the Free Pascal compiler, and I use Pascal in a programming course.
: I did not know there was a procedure called 'GetSystemTime', I only wrote it as an example, just like I do not know whether there is a DoALotOfMath procedure. These two, GetSystemTime and GetTickCount, are they available in every compiler ? Which compiler do you use ?
: One last question: does GetTickCount really count milliseconds or does it count CPU cycles instead ?

GetSystemTime and GetTickCount are both Windows API functions, so they are (or should be) available to any Windows compiler. So if you are using the Windows version of FreePascal, you should be able to use them by making sure "Windows" is a part of your uses clause (so it might look like this for example: uses Crt, Windows). If you are using the DOS version then neither function will be available.

I use quite a few different compilers. I prefer Delphi because it has the nicest IDE, but I also use Virtual Pascal and Free Pascal for compiling under different platforms, like DOS/OS2/*nix.

And GetTickCount returns the number of milliseconds that have elapsed since Windows was started (so it's like an uptime counter).