By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

HOWTO - measure a function execution

P: n/a
I need to measure how long a function take to execute... or a sequence of
code within

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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

Crirus wrote:
I need to measure how long a function take to execute... or a
sequence of code within

Nov 20 '05 #2

P: n/a
Cor
Hi Crirus,

This one I copied once from Nick (Nak)

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

Cor
Nov 20 '05 #3

P: n/a
"Crirus" <Cr****@datagroup.ro> schrieb
I need to measure how long a function take to execute... or a
sequence of code within


You can use
Environment.Tickcount
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4

P: n/a
Where I got 0 as the time elapsed what does that mean.. is lightning fast?:)

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Cor" <no*@non.com> wrote in message
news:ej**************@TK2MSFTNGP12.phx.gbl...
Hi Crirus,

This one I copied once from Nick (Nak)

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

Cor

Nov 20 '05 #5

P: n/a
Cor
Yes most things are not to measure and you have to put them in a loop
Nov 20 '05 #6

P: n/a
Well, I'm concerning with the ones that give me 100 ticks :)

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Cor" <no*@non.com> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
Yes most things are not to measure and you have to put them in a loop

Nov 20 '05 #7

P: n/a
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

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Cor" <no*@non.com> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
Yes most things are not to measure and you have to put them in a loop

Nov 20 '05 #8

P: n/a
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
Crirus wrote:
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
"Cor" <no*@non.com> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
Yes most things are not to measure and you have to put them in a loop

Nov 20 '05 #9

P: n/a

"One Handed Man" <Bo****@Duck.net> wrote in message
news:bp**********@titan.btinternet.com...
<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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
Nov 20 '05 #10

P: n/a
Suffel,

Thank You!

OHM

---------------------------------------------------
Sueffel wrote:
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bp**********@titan.btinternet.com...
<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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003

Nov 20 '05 #11

P: n/a
* "Crirus" <Cr****@datagroup.ro> scripsit:
I need to measure how long a function take to execute... or a sequence of
code within


<http://www.mentalis.org/soft/class.qpx?id=8>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #12

P: n/a
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

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Cor" <no*@non.com> wrote in message
news:eh**************@TK2MSFTNGP11.phx.gbl...
Yes most things are not to measure and you have to put them in a loop


Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.