473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

now.ticks does not work

place this behind a button that fills a listbox.
as you will see the time is now and then 0 or filled in????????????
by hitting the button.

is there a way to determine the real elapsed time?

thanks, Willie

Dim T As Double

T = Now.Ticks

System.Threading.Thread.Sleep(3)

T = Now.Ticks - T

ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
Nov 21 '05 #1
21 4346
"Willie jan" <un*****@unkown.com> schrieb
place this behind a button that fills a listbox.
as you will see the time is now and then 0 or filled in????????????
by hitting the button.

is there a way to determine the real elapsed time?

thanks, Willie

Dim T As Double
Dim t as long

Ticks is long, not double.
T = Now.Ticks

System.Threading.Thread.Sleep(3)

T = Now.Ticks - T

ListBox1.Items.Insert(0, T.ToString("0000000000000000"))

The *unit* of Now.Ticks is 100 nano seconds.
The *resolution* is AFAIR 0.01 seconds.

Sleep longer than 0.01 seconds and you'll see a difference.

Higher resolution: Environment.Tickcount.
Even higher resolution: High performance counter. (see
QueryPerformanceCounter and QueryPerformanceFrequency API functions)


Armin

Nov 21 '05 #2
On Wed, 17 Aug 2005 15:41:31 +0200, Willie jan wrote:
place this behind a button that fills a listbox.
as you will see the time is now and then 0 or filled in????????????
by hitting the button.

is there a way to determine the real elapsed time?

thanks, Willie

Dim T As Double

T = Now.Ticks

System.Threading.Thread.Sleep(3)

T = Now.Ticks - T

ListBox1.Items.Insert(0, T.ToString("0000000000000000"))


Not sure what's wrong with your code, but here's what I would have done:

Dim T as DateTime

T = Now

System.Threading.Thread.Sleep(3)

Dim D as TimeSpan

D = Now.Subtract(T)

ListBox1.Items.Insert(0, D.ToString())
Nov 21 '05 #3
Willie,
As the others suggest, you should use Long or DateTime to store T.

DateTime has a "resolution" of 100-nanoseconds, its precession can vary, in
that although it is based on units of 100-nanosecond its resolution or
precision is based on the system timer (which may be as course as .01
seconds)

QueryPerformanceCounter has a resolution of nanoseconds or smaller (its
precession is the QueryPerformanceFrequency value). Its based on a
"high-resolution performance counter", which I understand is a special hard
ware counter on some CPUs...

Tickcount has a resolution of microseconds.

There is an MSDN Magazine article that discusses timing blocks of code,
however I am having trouble finding it right now, it may be the third link
below, however it doesn't feel right...

Use QueryPerformanceCounter to Time code in VB.NET:
http://support.microsoft.com/default...b;en-us;306978

Not sure if the following will help or not:

Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Implement a Continuously Updating, High_Resolution Time Provider for Windows
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
I normally use QueryPerformanceCounter as its in the units used by
Performance Counters:

Something like:

Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef counter
As Long) As Boolean
Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
frequency As Long) As Boolean
' set some time var
Dim start, finish, frequency As Long
QueryPerformanceFrequency(frequency)
QueryPerformanceCounter(start)

' work

QueryPerformanceCounter(finish)
Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
frequency)

Alternatively you can use DateTime:

' set some time var
Dim start, finish As DateTime
start = DateTime.Now

' work

finish = DateTime.Now
Dim time As TimeSpan = finish.Subtract(start)

A third alternative would be to use "Ticks"

' set some time var
Dim start, finish As Integer
start = Environment.TickCount

' work

' set second time var and comapre to get result
finish = Environment.TickCount
Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish - start)

My understanding is that QueryPerformanceCounter will normally be a higher
resolution then Environment.TickCount, however QueryPerformanceCounter may
not be available.

VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice by
providing a System.Diagnostics.Stopwatch class that will automatically
choose between QueryPerformanceCounter & Environment.TickCount...

http://lab.msdn.microsoft.com/vs2005/

http://msdn2.microsoft.com/library/ebf7z0sw.aspx

Hope this helps
Jay
"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| place this behind a button that fills a listbox.
| as you will see the time is now and then 0 or filled in????????????
| by hitting the button.
|
| is there a way to determine the real elapsed time?
|
| thanks, Willie
|
| Dim T As Double
|
| T = Now.Ticks
|
| System.Threading.Thread.Sleep(3)
|
| T = Now.Ticks - T
|
| ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
|
|
Nov 21 '05 #4
the problem i have is that the now.ticks is not relayable.
the first click returns 0, the second returns 2.6ms
i will check out your QueryPerformanceCounter

thanks.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in bericht
news:O6**************@TK2MSFTNGP09.phx.gbl...
Willie,
As the others suggest, you should use Long or DateTime to store T.

DateTime has a "resolution" of 100-nanoseconds, its precession can vary,
in
that although it is based on units of 100-nanosecond its resolution or
precision is based on the system timer (which may be as course as .01
seconds)

QueryPerformanceCounter has a resolution of nanoseconds or smaller (its
precession is the QueryPerformanceFrequency value). Its based on a
"high-resolution performance counter", which I understand is a special
hard
ware counter on some CPUs...

Tickcount has a resolution of microseconds.

There is an MSDN Magazine article that discusses timing blocks of code,
however I am having trouble finding it right now, it may be the third link
below, however it doesn't feel right...

Use QueryPerformanceCounter to Time code in VB.NET:
http://support.microsoft.com/default...b;en-us;306978

Not sure if the following will help or not:

Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Implement a Continuously Updating, High_Resolution Time Provider for
Windows
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
I normally use QueryPerformanceCounter as its in the units used by
Performance Counters:

Something like:

Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef counter
As Long) As Boolean
Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
frequency As Long) As Boolean
' set some time var
Dim start, finish, frequency As Long
QueryPerformanceFrequency(frequency)
QueryPerformanceCounter(start)

' work

QueryPerformanceCounter(finish)
Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
frequency)

Alternatively you can use DateTime:

' set some time var
Dim start, finish As DateTime
start = DateTime.Now

' work

finish = DateTime.Now
Dim time As TimeSpan = finish.Subtract(start)

A third alternative would be to use "Ticks"

' set some time var
Dim start, finish As Integer
start = Environment.TickCount

' work

' set second time var and comapre to get result
finish = Environment.TickCount
Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish - start)

My understanding is that QueryPerformanceCounter will normally be a higher
resolution then Environment.TickCount, however QueryPerformanceCounter may
not be available.

VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice by
providing a System.Diagnostics.Stopwatch class that will automatically
choose between QueryPerformanceCounter & Environment.TickCount...

http://lab.msdn.microsoft.com/vs2005/

http://msdn2.microsoft.com/library/ebf7z0sw.aspx

Hope this helps
Jay
"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| place this behind a button that fills a listbox.
| as you will see the time is now and then 0 or filled in????????????
| by hitting the button.
|
| is there a way to determine the real elapsed time?
|
| thanks, Willie
|
| Dim T As Double
|
| T = Now.Ticks
|
| System.Threading.Thread.Sleep(3)
|
| T = Now.Ticks - T
|
| ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
|
|

Nov 21 '05 #5
my sleep was 3 ms, so it should show it.
the strange thing is, sometimes you get a value >0, but not always.

I use this to determine the database call time used.

"Armin Zingler" <az*******@freenet.de> schreef in bericht
news:u3**************@TK2MSFTNGP12.phx.gbl...
"Willie jan" <un*****@unkown.com> schrieb
place this behind a button that fills a listbox.
as you will see the time is now and then 0 or filled in????????????
by hitting the button.

is there a way to determine the real elapsed time?

thanks, Willie

Dim T As Double


Dim t as long

Ticks is long, not double.
T = Now.Ticks

System.Threading.Thread.Sleep(3)

T = Now.Ticks - T

ListBox1.Items.Insert(0, T.ToString("0000000000000000"))

The *unit* of Now.Ticks is 100 nano seconds.
The *resolution* is AFAIR 0.01 seconds.

Sleep longer than 0.01 seconds and you'll see a difference.

Higher resolution: Environment.Tickcount.
Even higher resolution: High performance counter. (see
QueryPerformanceCounter and QueryPerformanceFrequency API functions)


Armin

Nov 21 '05 #6
when i run your coude i get this when hitting the button sereval times:

00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00

"Ross Presser" <rp******@NOSPAMgmail.com.invalid> schreef in bericht
news:1o***************@rosspresser.dyndns.org...
On Wed, 17 Aug 2005 15:41:31 +0200, Willie jan wrote:
place this behind a button that fills a listbox.
as you will see the time is now and then 0 or filled in????????????
by hitting the button.

is there a way to determine the real elapsed time?

thanks, Willie

Dim T As Double

T = Now.Ticks

System.Threading.Thread.Sleep(3)

T = Now.Ticks - T

ListBox1.Items.Insert(0, T.ToString("0000000000000000"))


Not sure what's wrong with your code, but here's what I would have done:

Dim T as DateTime

T = Now

System.Threading.Thread.Sleep(3)

Dim D as TimeSpan

D = Now.Subtract(T)

ListBox1.Items.Insert(0, D.ToString())

Nov 21 '05 #7
when i use your code it get:

00:00:00
00:00:00.0156250
00:00:00
00:00:00
00:00:00
00:00:00.0156250
00:00:00.0156250
00:00:00
00:00:00

So it's basically the same problem. Now and then the result is >0, but often
it returns 0 ?????
"Ross Presser" <rp******@NOSPAMgmail.com.invalid> schreef in bericht
news:1o***************@rosspresser.dyndns.org...
On Wed, 17 Aug 2005 15:41:31 +0200, Willie jan wrote:
place this behind a button that fills a listbox.
as you will see the time is now and then 0 or filled in????????????
by hitting the button.

is there a way to determine the real elapsed time?

thanks, Willie

Dim T As Double

T = Now.Ticks

System.Threading.Thread.Sleep(3)

T = Now.Ticks - T

ListBox1.Items.Insert(0, T.ToString("0000000000000000"))


Not sure what's wrong with your code, but here's what I would have done:

Dim T as DateTime

T = Now

System.Threading.Thread.Sleep(3)

Dim D as TimeSpan

D = Now.Subtract(T)

ListBox1.Items.Insert(0, D.ToString())

Nov 21 '05 #8
Willie,

This method or any timing method on a moderen multi processing environment
will never be reliable just because you don't know what process is started
or done while your application is sleeping and takes the resources.

I hope this helps,

Cor
Nov 21 '05 #9
"Willie jan" <un*****@unkown.com> schrieb
my sleep was 3 ms, so it should show it.
As I wrote, the resolution is 0.01s, and 3 ms < 0.01s
the strange thing is, sometimes you get a value >0, but not always.


If the timer happend to tick just between the first and 2nd call to 'Now',
you get a difference.

Have a look @ a time scale: (Use a fixed font for viewing!)

0.03 seconds:

[---------|---------|---------]
^ ^

The "|" represent the ticks. They indicate at which point 'Now' returns a
new value. As the resolution is 0.01 seconds, this happens every 0.01
seconds. 'Now' returns 0.00 til the first tick. 'Now' returns 0.01 between
the first and 2nd tick. It returns 0.02 after the 2nd tick.

The "^" indicate the points where the measurements take place. If something
takes 3ms, you get 0.00 and 0.00 twice because there was no tick inbetween.
Difference = 0.

[---------|---------|---------]
^ ^

If one measurement takes place before, the other one after the tick, the
action took still 0.003 seconds, but you get a difference of 0.01 s, as you
see.

That's why you can get different results. As I've suggested already, use a
timer with a higher resolution.

Even if an action took 0.9999 seconds, you might get 0 as the difference as
shown here:

[---------|---------|---------]
^ ^

You see?
Armin

Nov 21 '05 #10
Willie,
| the problem i have is that the now.ticks is not relayable.
I agree: Using it to time code that is faster then the quantum that is used
to update it is not very "reliable". However! I consider Now.Ticks to be a
very reliable number!

Read my & Armin's message again, it (Now.Ticks) is very reliable as it will
be updated every quantum based on the OS, where quantum is OS based (see
first link below). However it is not very precise, because the quantum used
to update it is relatively course.

In other works Now.Ticks is reliably updated approximately every .01 seconds
as Armin suggests (as its based on the OS "clock"), however you are trying
to time something that only lasts .003 seconds. Simple math suggests that
..003 goes into .01 3 & 1/3 times. If you were timing something that took
seconds or minutes, then Now.Ticks would be very reliable!

For more details on the Precision & Resolution of Now.Ticks, see:

"Precision" of DateTime (100-nanoseconds):
http://msdn.microsoft.com/library/de...classtopic.asp

"Resolution" of DateTime.Now (.01 seconds):
http://msdn.microsoft.com/library/de...ssNowTopic.asp

Where "Precision" is how many digits of "time" a DateTime contains, while
"Resolution" is how accurate those digits are when using DateTime.Now.

Hope this helps
Jay

"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| the problem i have is that the now.ticks is not relayable.
| the first click returns 0, the second returns 2.6ms
| i will check out your QueryPerformanceCounter
|
| thanks.
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
| news:O6**************@TK2MSFTNGP09.phx.gbl...
| > Willie,
| > As the others suggest, you should use Long or DateTime to store T.
| >
| > DateTime has a "resolution" of 100-nanoseconds, its precession can vary,
| > in
| > that although it is based on units of 100-nanosecond its resolution or
| > precision is based on the system timer (which may be as course as .01
| > seconds)
| >
| > QueryPerformanceCounter has a resolution of nanoseconds or smaller (its
| > precession is the QueryPerformanceFrequency value). Its based on a
| > "high-resolution performance counter", which I understand is a special
| > hard
| > ware counter on some CPUs...
| >
| > Tickcount has a resolution of microseconds.
| >
| > There is an MSDN Magazine article that discusses timing blocks of code,
| > however I am having trouble finding it right now, it may be the third
link
| > below, however it doesn't feel right...
| >
| > Use QueryPerformanceCounter to Time code in VB.NET:
| > http://support.microsoft.com/default...b;en-us;306978
| >
| > Not sure if the following will help or not:
| >
| > Comparing the Timer Classes in the .NET Framework Class Library
| > http://msdn.microsoft.com/msdnmag/is...T/default.aspx
| >
| > Implement a Continuously Updating, High_Resolution Time Provider for
| > Windows
| >
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
| >
| >
| > I normally use QueryPerformanceCounter as its in the units used by
| > Performance Counters:
| >
| > Something like:
| >
| > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
counter
| > As Long) As Boolean
| > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| > frequency As Long) As Boolean
| >
| >
| > ' set some time var
| > Dim start, finish, frequency As Long
| > QueryPerformanceFrequency(frequency)
| > QueryPerformanceCounter(start)
| >
| > ' work
| >
| > QueryPerformanceCounter(finish)
| > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
| > frequency)
| >
| > Alternatively you can use DateTime:
| >
| > ' set some time var
| > Dim start, finish As DateTime
| > start = DateTime.Now
| >
| > ' work
| >
| > finish = DateTime.Now
| > Dim time As TimeSpan = finish.Subtract(start)
| >
| > A third alternative would be to use "Ticks"
| >
| > ' set some time var
| > Dim start, finish As Integer
| > start = Environment.TickCount
| >
| > ' work
| >
| > ' set second time var and comapre to get result
| > finish = Environment.TickCount
| > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish - start)
| >
| > My understanding is that QueryPerformanceCounter will normally be a
higher
| > resolution then Environment.TickCount, however QueryPerformanceCounter
may
| > not be available.
| >
| > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice
by
| > providing a System.Diagnostics.Stopwatch class that will automatically
| > choose between QueryPerformanceCounter & Environment.TickCount...
| >
| > http://lab.msdn.microsoft.com/vs2005/
| >
| > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| >
| > Hope this helps
| > Jay
| >
| >
| > "Willie jan" <un*****@unkown.com> wrote in message
| > news:43***********************@news.euronet.nl...
| > | place this behind a button that fills a listbox.
| > | as you will see the time is now and then 0 or filled in????????????
| > | by hitting the button.
| > |
| > | is there a way to determine the real elapsed time?
| > |
| > | thanks, Willie
| > |
| > | Dim T As Double
| > |
| > | T = Now.Ticks
| > |
| > | System.Threading.Thread.Sleep(3)
| > |
| > | T = Now.Ticks - T
| > |
| > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| > |
| > |
| >
| >
|
|
Nov 21 '05 #11
On Thu, 18 Aug 2005 11:26:51 +0200, Armin Zingler wrote:
Even if an action took 0.9999 seconds, you might get 0 as the difference as
shown here:

[---------|---------|---------]


Typo; I think you meant 0.09999 seconds.
Nov 21 '05 #12
Willie,
I should add: as Cor mentions.

Using Now.Ticks or QueryPerformanceCounter or any other timing method in
Windows is not going to be reliable, as there are other processes that are
running, that can change the outcome of the timing. I believe one or more of
the articles I gave discuss this "interference" of other processes.
Normally I run my test 5 or more times & average the results.

Hope this helps
Jay

"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| the problem i have is that the now.ticks is not relayable.
| the first click returns 0, the second returns 2.6ms
| i will check out your QueryPerformanceCounter
|
| thanks.
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
| news:O6**************@TK2MSFTNGP09.phx.gbl...
| > Willie,
| > As the others suggest, you should use Long or DateTime to store T.
| >
| > DateTime has a "resolution" of 100-nanoseconds, its precession can vary,
| > in
| > that although it is based on units of 100-nanosecond its resolution or
| > precision is based on the system timer (which may be as course as .01
| > seconds)
| >
| > QueryPerformanceCounter has a resolution of nanoseconds or smaller (its
| > precession is the QueryPerformanceFrequency value). Its based on a
| > "high-resolution performance counter", which I understand is a special
| > hard
| > ware counter on some CPUs...
| >
| > Tickcount has a resolution of microseconds.
| >
| > There is an MSDN Magazine article that discusses timing blocks of code,
| > however I am having trouble finding it right now, it may be the third
link
| > below, however it doesn't feel right...
| >
| > Use QueryPerformanceCounter to Time code in VB.NET:
| > http://support.microsoft.com/default...b;en-us;306978
| >
| > Not sure if the following will help or not:
| >
| > Comparing the Timer Classes in the .NET Framework Class Library
| > http://msdn.microsoft.com/msdnmag/is...T/default.aspx
| >
| > Implement a Continuously Updating, High_Resolution Time Provider for
| > Windows
| >
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
| >
| >
| > I normally use QueryPerformanceCounter as its in the units used by
| > Performance Counters:
| >
| > Something like:
| >
| > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
counter
| > As Long) As Boolean
| > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| > frequency As Long) As Boolean
| >
| >
| > ' set some time var
| > Dim start, finish, frequency As Long
| > QueryPerformanceFrequency(frequency)
| > QueryPerformanceCounter(start)
| >
| > ' work
| >
| > QueryPerformanceCounter(finish)
| > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
| > frequency)
| >
| > Alternatively you can use DateTime:
| >
| > ' set some time var
| > Dim start, finish As DateTime
| > start = DateTime.Now
| >
| > ' work
| >
| > finish = DateTime.Now
| > Dim time As TimeSpan = finish.Subtract(start)
| >
| > A third alternative would be to use "Ticks"
| >
| > ' set some time var
| > Dim start, finish As Integer
| > start = Environment.TickCount
| >
| > ' work
| >
| > ' set second time var and comapre to get result
| > finish = Environment.TickCount
| > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish - start)
| >
| > My understanding is that QueryPerformanceCounter will normally be a
higher
| > resolution then Environment.TickCount, however QueryPerformanceCounter
may
| > not be available.
| >
| > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice
by
| > providing a System.Diagnostics.Stopwatch class that will automatically
| > choose between QueryPerformanceCounter & Environment.TickCount...
| >
| > http://lab.msdn.microsoft.com/vs2005/
| >
| > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| >
| > Hope this helps
| > Jay
| >
| >
| > "Willie jan" <un*****@unkown.com> wrote in message
| > news:43***********************@news.euronet.nl...
| > | place this behind a button that fills a listbox.
| > | as you will see the time is now and then 0 or filled in????????????
| > | by hitting the button.
| > |
| > | is there a way to determine the real elapsed time?
| > |
| > | thanks, Willie
| > |
| > | Dim T As Double
| > |
| > | T = Now.Ticks
| > |
| > | System.Threading.Thread.Sleep(3)
| > |
| > | T = Now.Ticks - T
| > |
| > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| > |
| > |
| >
| >
|
|
Nov 21 '05 #13
"Ross Presser" <rp******@NOSPAMgmail.com.invalid> schrieb
On Thu, 18 Aug 2005 11:26:51 +0200, Armin Zingler wrote:
Even if an action took 0.9999 seconds, you might get 0 as the
difference as shown here:

[---------|---------|---------]


Typo; I think you meant 0.09999 seconds.

Yes! You're right. Thanks for correction.
Armin
Nov 21 '05 #14
aaahhhh now i see the light!

thanks for your clear answer.

Willie.

"Armin Zingler" <az*******@freenet.de> schreef in bericht
news:e1**************@tk2msftngp13.phx.gbl...
"Willie jan" <un*****@unkown.com> schrieb
my sleep was 3 ms, so it should show it.


As I wrote, the resolution is 0.01s, and 3 ms < 0.01s
the strange thing is, sometimes you get a value >0, but not always.


If the timer happend to tick just between the first and 2nd call to 'Now',
you get a difference.

Have a look @ a time scale: (Use a fixed font for viewing!)

0.03 seconds:

[---------|---------|---------]
^ ^

The "|" represent the ticks. They indicate at which point 'Now' returns a
new value. As the resolution is 0.01 seconds, this happens every 0.01
seconds. 'Now' returns 0.00 til the first tick. 'Now' returns 0.01 between
the first and 2nd tick. It returns 0.02 after the 2nd tick.

The "^" indicate the points where the measurements take place. If
something
takes 3ms, you get 0.00 and 0.00 twice because there was no tick
inbetween.
Difference = 0.

[---------|---------|---------]
^ ^

If one measurement takes place before, the other one after the tick, the
action took still 0.003 seconds, but you get a difference of 0.01 s, as
you
see.

That's why you can get different results. As I've suggested already, use a
timer with a higher resolution.

Even if an action took 0.9999 seconds, you might get 0 as the difference
as shown here:

[---------|---------|---------]
^ ^

You see?
Armin

Nov 21 '05 #15
the point is that my application logs the time used to run a certain
database call, and no average can me calculated. Now something they get a
value, and sometimes a 0. That's my problem right now.
When the call is for instance 30-60 ms, it's no problem with the ticks not
showing the right amount because the longer the calls takes, the less offset
in % is there.
But when a call that is made is 5ms than 1 less or more is 20%...

Willie.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in bericht
news:Op**************@TK2MSFTNGP14.phx.gbl...
Willie,
I should add: as Cor mentions.

Using Now.Ticks or QueryPerformanceCounter or any other timing method in
Windows is not going to be reliable, as there are other processes that are
running, that can change the outcome of the timing. I believe one or more
of
the articles I gave discuss this "interference" of other processes.
Normally I run my test 5 or more times & average the results.

Hope this helps
Jay

"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| the problem i have is that the now.ticks is not relayable.
| the first click returns 0, the second returns 2.6ms
| i will check out your QueryPerformanceCounter
|
| thanks.
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
| news:O6**************@TK2MSFTNGP09.phx.gbl...
| > Willie,
| > As the others suggest, you should use Long or DateTime to store T.
| >
| > DateTime has a "resolution" of 100-nanoseconds, its precession can
vary,
| > in
| > that although it is based on units of 100-nanosecond its resolution or
| > precision is based on the system timer (which may be as course as .01
| > seconds)
| >
| > QueryPerformanceCounter has a resolution of nanoseconds or smaller
(its
| > precession is the QueryPerformanceFrequency value). Its based on a
| > "high-resolution performance counter", which I understand is a special
| > hard
| > ware counter on some CPUs...
| >
| > Tickcount has a resolution of microseconds.
| >
| > There is an MSDN Magazine article that discusses timing blocks of
code,
| > however I am having trouble finding it right now, it may be the third
link
| > below, however it doesn't feel right...
| >
| > Use QueryPerformanceCounter to Time code in VB.NET:
| > http://support.microsoft.com/default...b;en-us;306978
| >
| > Not sure if the following will help or not:
| >
| > Comparing the Timer Classes in the .NET Framework Class Library
| >
http://msdn.microsoft.com/msdnmag/is...T/default.aspx
| >
| > Implement a Continuously Updating, High_Resolution Time Provider for
| > Windows
| >
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
| >
| >
| > I normally use QueryPerformanceCounter as its in the units used by
| > Performance Counters:
| >
| > Something like:
| >
| > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
counter
| > As Long) As Boolean
| > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| > frequency As Long) As Boolean
| >
| >
| > ' set some time var
| > Dim start, finish, frequency As Long
| > QueryPerformanceFrequency(frequency)
| > QueryPerformanceCounter(start)
| >
| > ' work
| >
| > QueryPerformanceCounter(finish)
| > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
| > frequency)
| >
| > Alternatively you can use DateTime:
| >
| > ' set some time var
| > Dim start, finish As DateTime
| > start = DateTime.Now
| >
| > ' work
| >
| > finish = DateTime.Now
| > Dim time As TimeSpan = finish.Subtract(start)
| >
| > A third alternative would be to use "Ticks"
| >
| > ' set some time var
| > Dim start, finish As Integer
| > start = Environment.TickCount
| >
| > ' work
| >
| > ' set second time var and comapre to get result
| > finish = Environment.TickCount
| > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish -
start)
| >
| > My understanding is that QueryPerformanceCounter will normally be a
higher
| > resolution then Environment.TickCount, however QueryPerformanceCounter
may
| > not be available.
| >
| > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice
by
| > providing a System.Diagnostics.Stopwatch class that will automatically
| > choose between QueryPerformanceCounter & Environment.TickCount...
| >
| > http://lab.msdn.microsoft.com/vs2005/
| >
| > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| >
| > Hope this helps
| > Jay
| >
| >
| > "Willie jan" <un*****@unkown.com> wrote in message
| > news:43***********************@news.euronet.nl...
| > | place this behind a button that fills a listbox.
| > | as you will see the time is now and then 0 or filled in????????????
| > | by hitting the button.
| > |
| > | is there a way to determine the real elapsed time?
| > |
| > | thanks, Willie
| > |
| > | Dim T As Double
| > |
| > | T = Now.Ticks
| > |
| > | System.Threading.Thread.Sleep(3)
| > |
| > | T = Now.Ticks - T
| > |
| > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| > |
| > |
| >
| >
|
|

Nov 21 '05 #16
Willie,
| the point is that my application logs the time used to run a certain
| database call, and no average can me calculated.
OK. I believe my comment still stands.

My first point is you need to use QueryPerformanceCounter or another higher
resolution timer (the multiple media timer for example) to time the database
call. I believe one or more of the articles I gave discuss the various high
resolution timers available.

My second point is that an individual database call may be "off" (because of
other processes running & possibly the network itself), but when you average
the times of 100 or 1000 or more database calls, it will be increasingly
more "reliable"...

I would recommend QueryPerformanceCounter as it has the same resolution as
System.Diagnostics.PerformanceCounter. In addition to (instead of?) logging
the time of a certain database call, I would define a number of
PerformanceCounters (aka Instrumentation) that tracked items such as:
- database calls per second
- seconds per database call
- number of database calls
- total seconds of all database calls

- number of "transactions" (unit of work)
- number of database calls per "transaction"
- seconds per "transaction"
- "transactions" per second
- total seconds of all "transactions"

Where an individual "transaction" makes one or more database calls. For
example saving a shopping cart is the transaction. The act of saving the
shopping cart will call the database for the shopping cart shipping/billing
info (the header), plus call the database once for each item in the shopping
cart (the detail).

Generally you only need one of "calls per second" & "seconds per call",
depending on the what the average length of the item being measured is.
Sometimes I include both as its not obvious which one will be more useful.

By defining PerformanceCounters you are able to use tools such as PerfMon
(Start - Control Panel - Administrative Tools - Performance') to see how
your app is performing overall. Having the log may help identify specific
problems once PerfMon has identified a problem...

Post if you would like links on Performance Counters & Instrumentation in
..NET.

Hope this helps
Jay

"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| the point is that my application logs the time used to run a certain
| database call, and no average can me calculated. Now something they get a
| value, and sometimes a 0. That's my problem right now.
| When the call is for instance 30-60 ms, it's no problem with the ticks not
| showing the right amount because the longer the calls takes, the less
offset
| in % is there.
| But when a call that is made is 5ms than 1 less or more is 20%...
|
| Willie.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
| news:Op**************@TK2MSFTNGP14.phx.gbl...
| > Willie,
| > I should add: as Cor mentions.
| >
| > Using Now.Ticks or QueryPerformanceCounter or any other timing method in
| > Windows is not going to be reliable, as there are other processes that
are
| > running, that can change the outcome of the timing. I believe one or
more
| > of
| > the articles I gave discuss this "interference" of other processes.
| >
| >
| > Normally I run my test 5 or more times & average the results.
| >
| > Hope this helps
| > Jay
| >
| > "Willie jan" <un*****@unkown.com> wrote in message
| > news:43***********************@news.euronet.nl...
| > | the problem i have is that the now.ticks is not relayable.
| > | the first click returns 0, the second returns 2.6ms
| > | i will check out your QueryPerformanceCounter
| > |
| > | thanks.
| > |
| > | "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
| > bericht
| > | news:O6**************@TK2MSFTNGP09.phx.gbl...
| > | > Willie,
| > | > As the others suggest, you should use Long or DateTime to store T.
| > | >
| > | > DateTime has a "resolution" of 100-nanoseconds, its precession can
| > vary,
| > | > in
| > | > that although it is based on units of 100-nanosecond its resolution
or
| > | > precision is based on the system timer (which may be as course as
..01
| > | > seconds)
| > | >
| > | > QueryPerformanceCounter has a resolution of nanoseconds or smaller
| > (its
| > | > precession is the QueryPerformanceFrequency value). Its based on a
| > | > "high-resolution performance counter", which I understand is a
special
| > | > hard
| > | > ware counter on some CPUs...
| > | >
| > | > Tickcount has a resolution of microseconds.
| > | >
| > | > There is an MSDN Magazine article that discusses timing blocks of
| > code,
| > | > however I am having trouble finding it right now, it may be the
third
| > link
| > | > below, however it doesn't feel right...
| > | >
| > | > Use QueryPerformanceCounter to Time code in VB.NET:
| > | > http://support.microsoft.com/default...b;en-us;306978
| > | >
| > | > Not sure if the following will help or not:
| > | >
| > | > Comparing the Timer Classes in the .NET Framework Class Library
| > | >
| > http://msdn.microsoft.com/msdnmag/is...T/default.aspx
| > | >
| > | > Implement a Continuously Updating, High_Resolution Time Provider for
| > | > Windows
| > | >
| >
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
| > | >
| > | >
| > | > I normally use QueryPerformanceCounter as its in the units used by
| > | > Performance Counters:
| > | >
| > | > Something like:
| > | >
| > | > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
| > counter
| > | > As Long) As Boolean
| > | > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| > | > frequency As Long) As Boolean
| > | >
| > | >
| > | > ' set some time var
| > | > Dim start, finish, frequency As Long
| > | > QueryPerformanceFrequency(frequency)
| > | > QueryPerformanceCounter(start)
| > | >
| > | > ' work
| > | >
| > | > QueryPerformanceCounter(finish)
| > | > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start)
/
| > | > frequency)
| > | >
| > | > Alternatively you can use DateTime:
| > | >
| > | > ' set some time var
| > | > Dim start, finish As DateTime
| > | > start = DateTime.Now
| > | >
| > | > ' work
| > | >
| > | > finish = DateTime.Now
| > | > Dim time As TimeSpan = finish.Subtract(start)
| > | >
| > | > A third alternative would be to use "Ticks"
| > | >
| > | > ' set some time var
| > | > Dim start, finish As Integer
| > | > start = Environment.TickCount
| > | >
| > | > ' work
| > | >
| > | > ' set second time var and comapre to get result
| > | > finish = Environment.TickCount
| > | > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish -
| > start)
| > | >
| > | > My understanding is that QueryPerformanceCounter will normally be a
| > higher
| > | > resolution then Environment.TickCount, however
QueryPerformanceCounter
| > may
| > | > not be available.
| > | >
| > | > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the
choice
| > by
| > | > providing a System.Diagnostics.Stopwatch class that will
automatically
| > | > choose between QueryPerformanceCounter & Environment.TickCount...
| > | >
| > | > http://lab.msdn.microsoft.com/vs2005/
| > | >
| > | > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| > | >
| > | > Hope this helps
| > | > Jay
| > | >
| > | >
| > | > "Willie jan" <un*****@unkown.com> wrote in message
| > | > news:43***********************@news.euronet.nl...
| > | > | place this behind a button that fills a listbox.
| > | > | as you will see the time is now and then 0 or filled
in????????????
| > | > | by hitting the button.
| > | > |
| > | > | is there a way to determine the real elapsed time?
| > | > |
| > | > | thanks, Willie
| > | > |
| > | > | Dim T As Double
| > | > |
| > | > | T = Now.Ticks
| > | > |
| > | > | System.Threading.Thread.Sleep(3)
| > | > |
| > | > | T = Now.Ticks - T
| > | > |
| > | > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| > | > |
| > | > |
| > | >
| > | >
| > |
| > |
| >
| >
|
|
Nov 21 '05 #17
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
My second point is that an individual database call may be "off"
(because of other processes running & possibly the network itself),

Unless you set the priority *very* high. ;-) But the user probably won't
like this. (and it's not good design to do it just for more accurate
measuring (apart from testing purposes)).
Armin
Nov 21 '05 #18
There are 2 more options that may help you out.

1. System.Environment.TickCount give milliseconds

Dim _t As Integer = Environment.TickCount

...

Console.Writeline(Environment.TickCount - _t1)

You will still get 0 if the start aend end occur in the same millisecond.

2. TimeSpan.TotalMilliseconds

Dim _t As DateTime = DateTime.Now

...

Console.Writeline("DateTime.Now.Subtract(_t).Total Milliseconds)

This will give milliseconds including fractions of milliseconds.
"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
the point is that my application logs the time used to run a certain
database call, and no average can me calculated. Now something they get a
value, and sometimes a 0. That's my problem right now.
When the call is for instance 30-60 ms, it's no problem with the ticks not
showing the right amount because the longer the calls takes, the less
offset in % is there.
But when a call that is made is 5ms than 1 less or more is 20%...

Willie.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht news:Op**************@TK2MSFTNGP14.phx.gbl...
Willie,
I should add: as Cor mentions.

Using Now.Ticks or QueryPerformanceCounter or any other timing method in
Windows is not going to be reliable, as there are other processes that
are
running, that can change the outcome of the timing. I believe one or more
of
the articles I gave discuss this "interference" of other processes.
Normally I run my test 5 or more times & average the results.

Hope this helps
Jay

"Willie jan" <un*****@unkown.com> wrote in message
news:43***********************@news.euronet.nl...
| the problem i have is that the now.ticks is not relayable.
| the first click returns 0, the second returns 2.6ms
| i will check out your QueryPerformanceCounter
|
| thanks.
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
bericht
| news:O6**************@TK2MSFTNGP09.phx.gbl...
| > Willie,
| > As the others suggest, you should use Long or DateTime to store T.
| >
| > DateTime has a "resolution" of 100-nanoseconds, its precession can
vary,
| > in
| > that although it is based on units of 100-nanosecond its resolution
or
| > precision is based on the system timer (which may be as course as .01
| > seconds)
| >
| > QueryPerformanceCounter has a resolution of nanoseconds or smaller
(its
| > precession is the QueryPerformanceFrequency value). Its based on a
| > "high-resolution performance counter", which I understand is a
special
| > hard
| > ware counter on some CPUs...
| >
| > Tickcount has a resolution of microseconds.
| >
| > There is an MSDN Magazine article that discusses timing blocks of
code,
| > however I am having trouble finding it right now, it may be the third
link
| > below, however it doesn't feel right...
| >
| > Use QueryPerformanceCounter to Time code in VB.NET:
| > http://support.microsoft.com/default...b;en-us;306978
| >
| > Not sure if the following will help or not:
| >
| > Comparing the Timer Classes in the .NET Framework Class Library
| >
http://msdn.microsoft.com/msdnmag/is...T/default.aspx
| >
| > Implement a Continuously Updating, High_Resolution Time Provider for
| > Windows
| >
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
| >
| >
| > I normally use QueryPerformanceCounter as its in the units used by
| > Performance Counters:
| >
| > Something like:
| >
| > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
counter
| > As Long) As Boolean
| > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| > frequency As Long) As Boolean
| >
| >
| > ' set some time var
| > Dim start, finish, frequency As Long
| > QueryPerformanceFrequency(frequency)
| > QueryPerformanceCounter(start)
| >
| > ' work
| >
| > QueryPerformanceCounter(finish)
| > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
| > frequency)
| >
| > Alternatively you can use DateTime:
| >
| > ' set some time var
| > Dim start, finish As DateTime
| > start = DateTime.Now
| >
| > ' work
| >
| > finish = DateTime.Now
| > Dim time As TimeSpan = finish.Subtract(start)
| >
| > A third alternative would be to use "Ticks"
| >
| > ' set some time var
| > Dim start, finish As Integer
| > start = Environment.TickCount
| >
| > ' work
| >
| > ' set second time var and comapre to get result
| > finish = Environment.TickCount
| > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish -
start)
| >
| > My understanding is that QueryPerformanceCounter will normally be a
higher
| > resolution then Environment.TickCount, however
QueryPerformanceCounter
may
| > not be available.
| >
| > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the
choice
by
| > providing a System.Diagnostics.Stopwatch class that will
automatically
| > choose between QueryPerformanceCounter & Environment.TickCount...
| >
| > http://lab.msdn.microsoft.com/vs2005/
| >
| > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| >
| > Hope this helps
| > Jay
| >
| >
| > "Willie jan" <un*****@unkown.com> wrote in message
| > news:43***********************@news.euronet.nl...
| > | place this behind a button that fills a listbox.
| > | as you will see the time is now and then 0 or filled in????????????
| > | by hitting the button.
| > |
| > | is there a way to determine the real elapsed time?
| > |
| > | thanks, Willie
| > |
| > | Dim T As Double
| > |
| > | T = Now.Ticks
| > |
| > | System.Threading.Thread.Sleep(3)
| > |
| > | T = Now.Ticks - T
| > |
| > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| > |
| > |
| >
| >
|
|


Nov 21 '05 #19
Stephany,
| 1. System.Environment.TickCount give milliseconds
| You will still get 0 if the start aend end occur in the same millisecond.
Agreed as I believe Armin & I have both stated that.

| 2. TimeSpan.TotalMilliseconds
| Dim _t As DateTime = DateTime.Now
| This will give milliseconds including fractions of milliseconds.
No, it will not give "fractions of milliseconds" it will be "rounded up" to
every 10 or 55 milliseconds based on the OS.

http://msdn.microsoft.com/library/de...ssNowTopic.asp

Although DateTime.Now includes fractions of a millisecond, it is based on
the system timer, which is every 10 or 55 milliseconds.

Hope this helps
Jay
"Stephany Young" <noone@localhost> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
| There are 2 more options that may help you out.
|
| 1. System.Environment.TickCount give milliseconds
|
| Dim _t As Integer = Environment.TickCount
|
| ...
|
| Console.Writeline(Environment.TickCount - _t1)
|
| You will still get 0 if the start aend end occur in the same millisecond.
|
| 2. TimeSpan.TotalMilliseconds
|
| Dim _t As DateTime = DateTime.Now
|
| ...
|
| Console.Writeline("DateTime.Now.Subtract(_t).Total Milliseconds)
|
| This will give milliseconds including fractions of milliseconds.
|
|
| "Willie jan" <un*****@unkown.com> wrote in message
| news:43***********************@news.euronet.nl...
| > the point is that my application logs the time used to run a certain
| > database call, and no average can me calculated. Now something they get
a
| > value, and sometimes a 0. That's my problem right now.
| > When the call is for instance 30-60 ms, it's no problem with the ticks
not
| > showing the right amount because the longer the calls takes, the less
| > offset in % is there.
| > But when a call that is made is 5ms than 1 less or more is 20%...
| >
| > Willie.
| >
| >
| > "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
| > bericht news:Op**************@TK2MSFTNGP14.phx.gbl...
| >> Willie,
| >> I should add: as Cor mentions.
| >>
| >> Using Now.Ticks or QueryPerformanceCounter or any other timing method
in
| >> Windows is not going to be reliable, as there are other processes that
| >> are
| >> running, that can change the outcome of the timing. I believe one or
more
| >> of
| >> the articles I gave discuss this "interference" of other processes.
| >>
| >>
| >> Normally I run my test 5 or more times & average the results.
| >>
| >> Hope this helps
| >> Jay
| >>
| >> "Willie jan" <un*****@unkown.com> wrote in message
| >> news:43***********************@news.euronet.nl...
| >> | the problem i have is that the now.ticks is not relayable.
| >> | the first click returns 0, the second returns 2.6ms
| >> | i will check out your QueryPerformanceCounter
| >> |
| >> | thanks.
| >> |
| >> | "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schreef in
| >> bericht
| >> | news:O6**************@TK2MSFTNGP09.phx.gbl...
| >> | > Willie,
| >> | > As the others suggest, you should use Long or DateTime to store T.
| >> | >
| >> | > DateTime has a "resolution" of 100-nanoseconds, its precession can
| >> vary,
| >> | > in
| >> | > that although it is based on units of 100-nanosecond its resolution
| >> or
| >> | > precision is based on the system timer (which may be as course as
..01
| >> | > seconds)
| >> | >
| >> | > QueryPerformanceCounter has a resolution of nanoseconds or smaller
| >> (its
| >> | > precession is the QueryPerformanceFrequency value). Its based on a
| >> | > "high-resolution performance counter", which I understand is a
| >> special
| >> | > hard
| >> | > ware counter on some CPUs...
| >> | >
| >> | > Tickcount has a resolution of microseconds.
| >> | >
| >> | > There is an MSDN Magazine article that discusses timing blocks of
| >> code,
| >> | > however I am having trouble finding it right now, it may be the
third
| >> link
| >> | > below, however it doesn't feel right...
| >> | >
| >> | > Use QueryPerformanceCounter to Time code in VB.NET:
| >> | > http://support.microsoft.com/default...b;en-us;306978
| >> | >
| >> | > Not sure if the following will help or not:
| >> | >
| >> | > Comparing the Timer Classes in the .NET Framework Class Library
| >> | >
| >> http://msdn.microsoft.com/msdnmag/is...T/default.aspx
| >> | >
| >> | > Implement a Continuously Updating, High_Resolution Time Provider
for
| >> | > Windows
| >> | >
| >>
http://msdn.microsoft.com/msdnmag/is...r/default.aspx
| >> | >
| >> | >
| >> | > I normally use QueryPerformanceCounter as its in the units used by
| >> | > Performance Counters:
| >> | >
| >> | > Something like:
| >> | >
| >> | > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
| >> counter
| >> | > As Long) As Boolean
| >> | > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| >> | > frequency As Long) As Boolean
| >> | >
| >> | >
| >> | > ' set some time var
| >> | > Dim start, finish, frequency As Long
| >> | > QueryPerformanceFrequency(frequency)
| >> | > QueryPerformanceCounter(start)
| >> | >
| >> | > ' work
| >> | >
| >> | > QueryPerformanceCounter(finish)
| >> | > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start)
/
| >> | > frequency)
| >> | >
| >> | > Alternatively you can use DateTime:
| >> | >
| >> | > ' set some time var
| >> | > Dim start, finish As DateTime
| >> | > start = DateTime.Now
| >> | >
| >> | > ' work
| >> | >
| >> | > finish = DateTime.Now
| >> | > Dim time As TimeSpan = finish.Subtract(start)
| >> | >
| >> | > A third alternative would be to use "Ticks"
| >> | >
| >> | > ' set some time var
| >> | > Dim start, finish As Integer
| >> | > start = Environment.TickCount
| >> | >
| >> | > ' work
| >> | >
| >> | > ' set second time var and comapre to get result
| >> | > finish = Environment.TickCount
| >> | > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish -
| >> start)
| >> | >
| >> | > My understanding is that QueryPerformanceCounter will normally be a
| >> higher
| >> | > resolution then Environment.TickCount, however
| >> QueryPerformanceCounter
| >> may
| >> | > not be available.
| >> | >
| >> | > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the
| >> choice
| >> by
| >> | > providing a System.Diagnostics.Stopwatch class that will
| >> automatically
| >> | > choose between QueryPerformanceCounter & Environment.TickCount...
| >> | >
| >> | > http://lab.msdn.microsoft.com/vs2005/
| >> | >
| >> | > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| >> | >
| >> | > Hope this helps
| >> | > Jay
| >> | >
| >> | >
| >> | > "Willie jan" <un*****@unkown.com> wrote in message
| >> | > news:43***********************@news.euronet.nl...
| >> | > | place this behind a button that fills a listbox.
| >> | > | as you will see the time is now and then 0 or filled
in????????????
| >> | > | by hitting the button.
| >> | > |
| >> | > | is there a way to determine the real elapsed time?
| >> | > |
| >> | > | thanks, Willie
| >> | > |
| >> | > | Dim T As Double
| >> | > |
| >> | > | T = Now.Ticks
| >> | > |
| >> | > | System.Threading.Thread.Sleep(3)
| >> | > |
| >> | > | T = Now.Ticks - T
| >> | > |
| >> | > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| >> | > |
| >> | > |
| >> | >
| >> | >
| >> |
| >> |
| >>
| >>
| >
| >
|
|
Nov 21 '05 #20
Armin,
My understanding is even when a thread or process's priority is *very* high,
the OS may use "priority boosting" to raise the lesser threads up to give
them a slight chance of having some time.

http://msdn.microsoft.com/library/de...ity_boosts.asp

Jay

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
| > My second point is that an individual database call may be "off"
| > (because of other processes running & possibly the network itself),
|
|
| Unless you set the priority *very* high. ;-) But the user probably won't
| like this. (and it's not good design to do it just for more accurate
| measuring (apart from testing purposes)).
|
|
| Armin
Nov 21 '05 #21
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
Armin,
My understanding is even when a thread or process's priority is
*very* high, the OS may use "priority boosting" to raise the lesser
threads up to give them a slight chance of having some time.

http://msdn.microsoft.com/library/de...ity_boosts.asp


Yes, but the higher the priority the better the results when measuring the
time because the probability of being interrupted will be lower. If the
base priorty is 31, it wouldn't help other threads anyway (Willie, "don't
try this at home" - unless you know what you're doing). I didn't want to go
into details as it was not meant really seriously.

(BTW, the system's crystal is running @ 14,318,181 MHz. The value returned
by QueryPerformanceFrequency is 3,579,545. This is the first value divided
by 4. Means, every 4th base tick, the HP counter is increased.
Another divider is 12. Result=1193182 (14318181/12). (Actually 1193180 has
been used but nobody really knows why). On former systems (or still?), every
65536 (0x10000) tick, the timer interrupt has been raised: 1193182/65536 =
18.2 times a second. Every 11932st tick, the time (also returned by
DateTime.Now) is updated. This means 1193182/11932 ~ 100 times a second.)
Armin

Nov 21 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Riggio | last post by:
Does anyone know a way to get system ticks in .Net? I found DateTime.Ticks, but that is equal to 100 nanoseconds. The reason I'm asking is because we're considering passing some sort of timestamp...
4
by: Jeroen CEuppens | last post by:
Hi, I want to measure a time (in ticks) between a couple of operations, with these code: long voor = DateTime.Now.TimeOfDay.Ticks; bmp1 = new Bitmap(@"\Network\im\ImageWeftIllum.bmp"); bmp2...
16
by: Tamir Khason | last post by:
Follow the code I have to check the performance double a = r.Next(9)*1e307; DateTime d = DateTime.Now; double b = Math.Pow(a,1/(double)100); TimeSpan tm = DateTime.Now - d;
1
by: TC | last post by:
Anyone know what this means Parser Error Message: Error loading XML file E:\Inetpub\wwwroot\web.config Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name:...
3
by: Ken Dopierala Jr. | last post by:
Hi, Here is C# code: Random rdm1 = new Random(unchecked((int)DateTime.Now.Ticks)); Can I do this in VB? I've tried CType(DateTime.Now.Ticks, Integer) and CInt(DateTime.Now.Ticks) but...
3
by: sparkle | last post by:
Hi, Does anybody know how to convert date to ticks from a dateTimePicker? What I'm using now isn't working. I'm trying to use a dateTimePicker to set an appointment in Outlook with a...
1
by: Mark | last post by:
How does one determine the number of ticks from a given date as opposed to the date (Jan 1, 0001) that is used in the .Ticks method? Thanks, Mark
1
by: Ole | last post by:
Is there a way to get ticks from a the system without using the DateTime.Ticks (there isn't a watch in the hardware so it doesn't work)? The Thread.Sleep(x) works well so where does it get its...
1
by: raghu1 | last post by:
How to convert a given date to its equvalent ticks.: string d="5/15/2006 12:10:44 PM"; // string 2 date ... dt=Convert.ToDateTime(d); // Date 2 ticks ... dt2ticks=dt.Ticks; string ticks =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.