Need to know how long it takes to run these 3 lines...

  • Thread starter Thread starter Bruce D
  • Start date Start date
B

Bruce D

My program in connecting to a web site and I want to know how long it takes
to connect to this site. I'm assuming I need to use some sort of timer.
This is a console application. Here are the lines of code.

' set some time var
Dim lWebRequest As System.Net.WebRequest =
System.Net.WebRequest.Create(lURL)
lWebRequest.Timeout = lTimeOut
Dim lWebResponse As System.Net.WebResponse = lWebRequest.GetResponse()
' set second time var and comapre to get result

Any help is appreciated!
-bruce duncan
 
Bruce,

The most simple one is in my opinion the environment.ticks
\\\
dim start as integer = environment.ticks
dim ends as integer = environment.ticks - start
///

Know that timing with a PC is never correct.

Cor
 
Bruce,

you could setup a timer control with interval 10 (hundredth of a
second) and use the following code

\\\
Private clicked As Double = 0.0

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
clicked += 0.01
End Sub

' your sub/event
Me.clicked = 0.0
Timer1.Start
Dim lWebRequest As System.Net.WebRequest =
System.Net.WebRequest.Create(lURL)
lWebRequest.Timeout = lTimeOut
Dim lWebResponse As System.Net.WebResponse =
lWebRequest.GetResponse()
Timer1.Stop
Msgbox(Me.clicked)

///

Don't know if that works or how accurate it is, just an idea that might
help you along.

hth
 
Bruce,
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

' set second time var and comapre to get result
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

' set second time var and comapre to get result
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
 
Jay,
I was using the datetime, but I was looking for something a little ?better?.
I went with tickcount(). All these methods are great. Do you know of a
reason to use one over another? I'm not familiar with Performance
Counters...so I'm going to look into it now...
-bruce
 
Hi *Not* Aaron
Don't know if that works or how accurate it is, just an idea that might
help you along.

Unfortunately it is *very* inaccurate, the only way to get *real*
accuracy is to use QueryHighPerformanceCounter. This brings back some very
accurate results!

Nick.
 
Bruce,
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.

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.aspx?scid=kb;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/issues/04/02/TimersinNET/default.aspx

Implement a Continuously Updating, High_Resolution Time Provider for Windows
http://msdn.microsoft.com/msdnmag/issues/04/03/HighResolutionTimer/default.aspx

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top