HOWTO - measure a function execution

  • Thread starter Thread starter Crirus
  • Start date Start date
Here is one 'CLUNKY' but probably fairly accurate way if the code execution
time is very small.

PSEUDO CODE
------------------

Dim startTime, EndTime as Time
Dim x as long
Const NTIMES = 10,000 ( Depending on the length of time to perform the
function )

startTime= Now
For x=1 to NTIMES ( Depending on the length of time to perform the
unction )

Call Function

Next
EndTime= now

Display "Time to run function = " & endTime-StartTime( In Seconds ) /
NTIMES


OHM
 
Hi Crirus,

This one I copied once from Nick (Nak)

Dim StartTick As Integer = Environment.TickCount
.................
Dim Elapsed As Integer = Environment.TickCount - StartTick

Cor
 
I found this
sometime next statement take 0 tick and sometime take 16 ticks...why's that?

TileImage = New Bitmap(imgTranz(trans(x, i, j) - 1, x - 1))

imgTranz is an array of images 14x8
trans is an array of integers from 1 to 15
 
For all sorts of reasons, like the machine may be busy doing other things at
the time. Thats why I suggested that you call the function over a period of
time ( thousands of calls ) then you can get the average.

But no one listens to me, perhaps I should stop posting.

OHM
 
<SNIP>

OHM is on the ball. Windows is finicky, and even if you don't have
another program running, it's always doing something. And remember, a CPU
can only handle 1 function at a time, that's why cache was invented, so
doing a bench-mark twice is not going to do anything but upset you because
it may run a 0 ticks if nothing else is going on, or 16 ticks if it had to
wait for another process so it could execute a call.
Run it 100 times, each time writing the resaults to a file, call a
System.Threading.Thread.Sleep(5000) between each execution of the loop. It
will take 500,000 ms (about 8 1/2 minutes) to finish the benchmark, but you
will have a very accurate set of numbers. The simply open the file into
Excel or something similar, and get the average, and there is your ART
(Average RunTime) for the function. Use that number for stateting how fast
the function runs.

HTH
Suefell
 
In addition, timer resolution is only good for about 15 ticks anyway, so a
method that takes 8 ticks to execute may actually just return 0.

-- russ
 
Back
Top