Gather the runtime of a process

  • Thread starter Federico G. Babelis
  • Start date
F

Federico G. Babelis

Hi All:

I need to gather the runtime in format "HH:mm:ss" of certain processes from
its start to its end, any ideas ?

thanks,
Federico
 
J

Jay B. Harlow [MVP - Outlook]

Federico,
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
 
F

Federico G. Babelis

Impressive !

Thanks for your help !

PS: How can i transform the TimeSpan results into format HH:mm:ss ?

Regards,
Federico
 
L

Larry Lard

Federico said:
PS: How can i transform the TimeSpan results into format HH:mm:ss ?

You are lucky in that "hh:mm:ss" is the default string conversion of a
TimeSpan, so just MyTimeSpan.ToString() will give you what you want.
 
J

Jay B. Harlow [MVP - Outlook]

Federico,
As Larry suggests, calling TimeSpan.ToString() will return the results in
the format: [-][d.]hh:mm:ss[.ff]

http://msdn.microsoft.com/library/d...tml/frlrfSystemTimeSpanClassToStringTopic.asp

Remember the ToString method of "formattable" objects in .NET is normally
overridden to provide formatting for that object.

If you don't want the fractional seconds or days on the formatted string, I
normally convert the TimeSpan to a DateTime & then use custom DateTime
formatting.

Note TimeSpan itself only supports a fixed format, I will convert a TimeSpan
into a DateTime if I need custom formatting.

Dim ts As TimeSpan
Dim dt As DateTime = DateTime.MinValue.Add(ts)
Dim s As String

s = ts.ToString() ' default TimeSpan formatting
s = dt.ToString("H:mm:ss") ' custom DateTime formatting


For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp


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

Top