HighTechTalks DotNet Forums  

Computing Elapsed Time

Visual Studio.net (General) microsoft.public.vsnet.general


Discuss Computing Elapsed Time in the Visual Studio.net (General) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #11  
Old   
Hans Kesting
 
Posts: n/a

Default Re: Computing Elapsed Time - 06-18-2009 , 10:23 AM






JungleJim74 was thinking very hard :
Quote:
Hans, I believe that my problem is caused by the fact the you MVPs put in a
snippet of code that will do the job but you don't tell me what I have to
include in my code to get the snippet to compile and run successfully. You
assume that everybody knows how to include all entries that will make your
snippet compile and run but that is just not so. I say this respectfully: You
MVPs are so very advanced that you have forgotten what it is like to be a
beginner. Have a good day. Respectfully, JungleJim74


True, it has been a while since I was a beginner (and I am not an MVP,
that is a sort of official title - but never mind that).

On the other hand it is difficult to answer at the precise level: If I
answer at a very basic level, then someone with already some experience
might also feel offended (plus it would take a lot more time to answer
a question). It is not always clear what level the poster has.
Also I usually want to point the asker in the right direction, so
he/she can get more information and learn how things work, rather than
just give "finished" code.

You are right: sometimes a code snippet can illustrate the point
better. But then a further complication pops in: I am a C# programmer
and apparently you are using VB.Net. For framework issues (such as how
the TimeSpan and DateTime struct work) that is not a problem (C# and VB
use the same framework), but for code snippets it is: I have to guess
at the VB syntax.

But let's try:
I assume in the snippets that there is a method 'PerformCalculation'
that performs the calculations you want to measure. Also you will need
to place this code in a method where you can execute it.


' Calculate a time using DateTime
Dim start as DateTime
Din end as DateTime
start = DateTime.Now
PerformCalculation
end = DateTime.Now
Dim elapsed as TimeSpan
elapsed = end - start
Console.WriteLine(elapsed.TotalMilliseconds)


' Calculate using Stopwatch
Dim sw as new System.Diagnostics.Stopwatch
sw.Start
PerformCalculation
sw.Stop
Console.WriteLine(sw.ElapsedMilliseconds)



As for a second remark: if you have in you code something like:
result=((2.154*33)-161.376 / (3.541*7))
try replacing that with
result = 64.571
to find out if that makes any difference in speed (it shouldn't)

Hans Kesting

Quote:
"Hans Kesting" wrote:

JungleJim74 formulated the question :
I am using Visual Studio 2003 and am trying to compute and display the
elapsed time that my computer takes to calculate ((2.154*33)-161.376 /
(3.541*7)). I have the interface prepared and the program makes this
calculation 2,000,000,000 in 5 seconds. I display the start time and the
end time with start = Now and end = Now and this works OK for the display
purposes. But I do not know how to compute the elapsed time. I have
declared the variables "start"and ënd" as strings and this declaration may
be the source of my trouble. Thanks in advance for any help on this.
JungleJim74

Just a note: if that formula is a copy from your test-program, then you
might end up testing the wrong thing! The compiler already performs
calculations with fixed numbers, so runtime the formula would be
replaced by a single value.
A formula "a=2*3" is compiled as "a=6".

Hans Kesting



Reply With Quote
  #12  
Old   
eBob.com
 
Posts: n/a

Default Re: Computing Elapsed Time - 06-18-2009 , 12:00 PM






Hans,

Here is some sample code which produces an elapsed time value. Note that I
am not an expert at this stuff. So my sample code might not be the best,
but I don't think that it will lead you astray.

I used the VS IDE/designer but did not place any controls on the form.

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

Option Explicit On
Option Strict On

' always use the two options above because they allow the compiler
' to catch things which can cause run time errors

Imports System.Threading

' System.Threading needed for the Sleep function


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim StartTime As DateTime = DateTime.Now 'for calc of elapsed time

Dim ElapsedTime As TimeSpan 'for calc of elapsed time

Dim ElapsedTimeText As String ' for displaying elapsed time

Thread.Sleep(1000) 'sleep for 1 second

ElapsedTime = DateTime.op_Subtraction(DateTime.Now, StartTime)

ElapsedTimeText = String.Format("{0,2:#0'h'} {1,3:#0'm'}" & _
" {2,3:#0's'}{3,7:##0'ms'}", _
ElapsedTime.Hours, ElapsedTime.Minutes, _
ElapsedTime.Seconds, ElapsedTime.Milliseconds)

' although this is my code (I can't blame it on anyone else), I
' don't understand how I get away with the above call to
' String.Format as the overloaded String.Format method doesn't,
' so far as I can see, support this number of arguments

' the formatting specification is not intuitive and documentation
can
' be difficult to find. But if you look at the doc for String
Members
' and then the Format method and then follow the link to
' Composite Formatting I think you'll find pretty decent
documentation.

MsgBox("My formatting yields: " & ElapsedTimeText)

MsgBox("TimeSpan.ToString yields: " & ElapsedTime.ToString)

End Sub
End Class

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

Good Luck, Bob

"JungleJim74" <JungleJim74 (AT) discussions (DOT) microsoft.com> wrote

Quote:
Thank you Hans but I am beginning to believe that I am in over my head and
that I need to study VB.NET a lot more BEFORE I try to write and compile
this
tyoe of program. I have tried to follow all of the responses posted to my
request for help but no matter which one I try all I get is compiler
errors.
If the code does not compile you sure cannot run an executable. But I
thank
you both again for trying to help me. JungleJim74 (I'm 81 years old and I
guess I just don't have it anymore)

"Hans Kesting" wrote:

JungleJim74 formulated the question :
I am using Visual Studio 2003 and am trying to compute and display the
elapsed time that my computer takes to calculate ((2.154*33)-161.376 /
(3.541*7)). I have the interface prepared and the program makes this
calculation 2,000,000,000 in 5 seconds. I display the start time and
the end
time with start = Now and end = Now and this works OK for the display
purposes. But I do not know how to compute the elapsed time. I have
declared
the variables "start"and ënd" as strings and this declaration may be
the
source of my trouble. Thanks in advance for any help on this.
JungleJim74

Just a note: if that formula is a copy from your test-program, then you
might end up testing the wrong thing! The compiler already performs
calculations with fixed numbers, so runtime the formula would be
replaced by a single value.
A formula "a=2*3" is compiled as "a=6".

Hans Kesting



Reply With Quote
  #13  
Old   
eBob.com
 
Posts: n/a

Default Re: Computing Elapsed Time (Whoops; for JungleJim74 not Hans) - 06-18-2009 , 12:05 PM



Meant for JungleJim74 for Hans. Sorry.

"eBob.com" <eBob.com (AT) totallybogus (DOT) com> wrote

Quote:
Hans,

Here is some sample code which produces an elapsed time value. Note that
I am not an expert at this stuff. So my sample code might not be the
best, but I don't think that it will lead you astray.

I used the VS IDE/designer but did not place any controls on the form.

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

Option Explicit On
Option Strict On

' always use the two options above because they allow the compiler
' to catch things which can cause run time errors

Imports System.Threading

' System.Threading needed for the Sleep function


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim StartTime As DateTime = DateTime.Now 'for calc of elapsed
time

Dim ElapsedTime As TimeSpan 'for calc of elapsed time

Dim ElapsedTimeText As String ' for displaying elapsed time

Thread.Sleep(1000) 'sleep for 1 second

ElapsedTime = DateTime.op_Subtraction(DateTime.Now, StartTime)

ElapsedTimeText = String.Format("{0,2:#0'h'} {1,3:#0'm'}" & _
" {2,3:#0's'}{3,7:##0'ms'}", _
ElapsedTime.Hours, ElapsedTime.Minutes, _
ElapsedTime.Seconds, ElapsedTime.Milliseconds)

' although this is my code (I can't blame it on anyone else), I
' don't understand how I get away with the above call to
' String.Format as the overloaded String.Format method doesn't,
' so far as I can see, support this number of arguments

' the formatting specification is not intuitive and documentation
can
' be difficult to find. But if you look at the doc for String
Members
' and then the Format method and then follow the link to
' Composite Formatting I think you'll find pretty decent
documentation.

MsgBox("My formatting yields: " & ElapsedTimeText)

MsgBox("TimeSpan.ToString yields: " & ElapsedTime.ToString)

End Sub
End Class

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

Good Luck, Bob

"JungleJim74" <JungleJim74 (AT) discussions (DOT) microsoft.com> wrote in message
news:F55DE265-6E07-4F1F-9496-08314D182002 (AT) microsoft (DOT) com...
Thank you Hans but I am beginning to believe that I am in over my head
and
that I need to study VB.NET a lot more BEFORE I try to write and compile
this
tyoe of program. I have tried to follow all of the responses posted to my
request for help but no matter which one I try all I get is compiler
errors.
If the code does not compile you sure cannot run an executable. But I
thank
you both again for trying to help me. JungleJim74 (I'm 81 years old and I
guess I just don't have it anymore)

"Hans Kesting" wrote:

JungleJim74 formulated the question :
I am using Visual Studio 2003 and am trying to compute and display the
elapsed time that my computer takes to calculate ((2.154*33)-161.376 /
(3.541*7)). I have the interface prepared and the program makes this
calculation 2,000,000,000 in 5 seconds. I display the start time and
the end
time with start = Now and end = Now and this works OK for the display
purposes. But I do not know how to compute the elapsed time. I have
declared
the variables "start"and ënd" as strings and this declaration may be
the
source of my trouble. Thanks in advance for any help on this.
JungleJim74

Just a note: if that formula is a copy from your test-program, then you
might end up testing the wrong thing! The compiler already performs
calculations with fixed numbers, so runtime the formula would be
replaced by a single value.
A formula "a=2*3" is compiled as "a=6".

Hans Kesting




Reply With Quote
  #14  
Old   
JungleJim74
 
Posts: n/a

Default Re: Computing Elapsed Time (Whoops; for JungleJim74 not Hans) - 06-19-2009 , 06:19 AM



Thank you Bob. You have gone out of your way to help me and I want you to
know that I surely do appreciate it. Your post is so complete that I believe
that even I will be able to solve my problem using your post as guidance.
Have a great day, you surely have made my day for me. JungleJim74

"eBob.com" wrote:

Quote:
Meant for JungleJim74 for Hans. Sorry.

"eBob.com" <eBob.com (AT) totallybogus (DOT) com> wrote in message
news:uhxDFZD8JHA.4632 (AT) TK2MSFTNGP02 (DOT) phx.gbl...
Hans,

Here is some sample code which produces an elapsed time value. Note that
I am not an expert at this stuff. So my sample code might not be the
best, but I don't think that it will lead you astray.

I used the VS IDE/designer but did not place any controls on the form.

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

Option Explicit On
Option Strict On

' always use the two options above because they allow the compiler
' to catch things which can cause run time errors

Imports System.Threading

' System.Threading needed for the Sleep function


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim StartTime As DateTime = DateTime.Now 'for calc of elapsed
time

Dim ElapsedTime As TimeSpan 'for calc of elapsed time

Dim ElapsedTimeText As String ' for displaying elapsed time

Thread.Sleep(1000) 'sleep for 1 second

ElapsedTime = DateTime.op_Subtraction(DateTime.Now, StartTime)

ElapsedTimeText = String.Format("{0,2:#0'h'} {1,3:#0'm'}" & _
" {2,3:#0's'}{3,7:##0'ms'}", _
ElapsedTime.Hours, ElapsedTime.Minutes, _
ElapsedTime.Seconds, ElapsedTime.Milliseconds)

' although this is my code (I can't blame it on anyone else), I
' don't understand how I get away with the above call to
' String.Format as the overloaded String.Format method doesn't,
' so far as I can see, support this number of arguments

' the formatting specification is not intuitive and documentation
can
' be difficult to find. But if you look at the doc for String
Members
' and then the Format method and then follow the link to
' Composite Formatting I think you'll find pretty decent
documentation.

MsgBox("My formatting yields: " & ElapsedTimeText)

MsgBox("TimeSpan.ToString yields: " & ElapsedTime.ToString)

End Sub
End Class

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

Good Luck, Bob

"JungleJim74" <JungleJim74 (AT) discussions (DOT) microsoft.com> wrote in message
news:F55DE265-6E07-4F1F-9496-08314D182002 (AT) microsoft (DOT) com...
Thank you Hans but I am beginning to believe that I am in over my head
and
that I need to study VB.NET a lot more BEFORE I try to write and compile
this
tyoe of program. I have tried to follow all of the responses posted to my
request for help but no matter which one I try all I get is compiler
errors.
If the code does not compile you sure cannot run an executable. But I
thank
you both again for trying to help me. JungleJim74 (I'm 81 years old and I
guess I just don't have it anymore)

"Hans Kesting" wrote:

JungleJim74 formulated the question :
I am using Visual Studio 2003 and am trying to compute and display the
elapsed time that my computer takes to calculate ((2.154*33)-161.376 /
(3.541*7)). I have the interface prepared and the program makes this
calculation 2,000,000,000 in 5 seconds. I display the start time and
the end
time with start = Now and end = Now and this works OK for the display
purposes. But I do not know how to compute the elapsed time. I have
declared
the variables "start"and ënd" as strings and this declaration may be
the
source of my trouble. Thanks in advance for any help on this.
JungleJim74

Just a note: if that formula is a copy from your test-program, then you
might end up testing the wrong thing! The compiler already performs
calculations with fixed numbers, so runtime the formula would be
replaced by a single value.
A formula "a=2*3" is compiled as "a=6".

Hans Kesting






Reply With Quote
  #15  
Old   
JungleJim74
 
Posts: n/a

Default Re: Computing Elapsed Time - 06-19-2009 , 06:26 AM



Thanks Hans. You surely are doing your best to help me and I appreciate it
greatly.
I started studying programming in 1992 and I started with thee C Programming
Language. Then I started using C++ and finally Visual Basic 6. Now I am
trying to use VB.NET and it is a bit too much for me. Also, I do not have the
large library of books for .NET that I have for VB6. Thanks again.
JungleJim74

"Hans Kesting" wrote:

Quote:
JungleJim74 was thinking very hard :
Hans, I believe that my problem is caused by the fact the you MVPs put in a
snippet of code that will do the job but you don't tell me what I have to
include in my code to get the snippet to compile and run successfully. You
assume that everybody knows how to include all entries that will make your
snippet compile and run but that is just not so. I say this respectfully: You
MVPs are so very advanced that you have forgotten what it is like to be a
beginner. Have a good day. Respectfully, JungleJim74



True, it has been a while since I was a beginner (and I am not an MVP,
that is a sort of official title - but never mind that).

On the other hand it is difficult to answer at the precise level: If I
answer at a very basic level, then someone with already some experience
might also feel offended (plus it would take a lot more time to answer
a question). It is not always clear what level the poster has.
Also I usually want to point the asker in the right direction, so
he/she can get more information and learn how things work, rather than
just give "finished" code.

You are right: sometimes a code snippet can illustrate the point
better. But then a further complication pops in: I am a C# programmer
and apparently you are using VB.Net. For framework issues (such as how
the TimeSpan and DateTime struct work) that is not a problem (C# and VB
use the same framework), but for code snippets it is: I have to guess
at the VB syntax.

But let's try:
I assume in the snippets that there is a method 'PerformCalculation'
that performs the calculations you want to measure. Also you will need
to place this code in a method where you can execute it.


' Calculate a time using DateTime
Dim start as DateTime
Din end as DateTime
start = DateTime.Now
PerformCalculation
end = DateTime.Now
Dim elapsed as TimeSpan
elapsed = end - start
Console.WriteLine(elapsed.TotalMilliseconds)


' Calculate using Stopwatch
Dim sw as new System.Diagnostics.Stopwatch
sw.Start
PerformCalculation
sw.Stop
Console.WriteLine(sw.ElapsedMilliseconds)



As for a second remark: if you have in you code something like:
result=((2.154*33)-161.376 / (3.541*7))
try replacing that with
result = 64.571
to find out if that makes any difference in speed (it shouldn't)

Hans Kesting

"Hans Kesting" wrote:

JungleJim74 formulated the question :
I am using Visual Studio 2003 and am trying to compute and display the
elapsed time that my computer takes to calculate ((2.154*33)-161.376 /
(3.541*7)). I have the interface prepared and the program makes this
calculation 2,000,000,000 in 5 seconds. I display the start time and the
end time with start = Now and end = Now and this works OK for the display
purposes. But I do not know how to compute the elapsed time. I have
declared the variables "start"and ënd" as strings and this declaration may
be the source of my trouble. Thanks in advance for any help on this.
JungleJim74

Just a note: if that formula is a copy from your test-program, then you
might end up testing the wrong thing! The compiler already performs
calculations with fixed numbers, so runtime the formula would be
replaced by a single value.
A formula "a=2*3" is compiled as "a=6".

Hans Kesting






Reply With Quote
  #16  
Old   
JungleJim74
 
Posts: n/a

Default Re: Computing Elapsed Time (Whoops; for JungleJim74 not Hans) - 06-19-2009 , 10:58 AM



Bob, I wanted you to know that your code compiled and ran just fine AFTER I
reduced the call to String.Format using only two arguments instead of the
four. But seconds and milliseconds is really all I need. I will check MSDN to
see if I can include three arguments as minutes, seconds, and milliseconds
would be better than just seconds and milliseconds.
Thanks again for your help. JungleJim74

"eBob.com" wrote:

Quote:
Meant for JungleJim74 for Hans. Sorry.

"eBob.com" <eBob.com (AT) totallybogus (DOT) com> wrote in message
news:uhxDFZD8JHA.4632 (AT) TK2MSFTNGP02 (DOT) phx.gbl...
Hans,

Here is some sample code which produces an elapsed time value. Note that
I am not an expert at this stuff. So my sample code might not be the
best, but I don't think that it will lead you astray.

I used the VS IDE/designer but did not place any controls on the form.

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

Option Explicit On
Option Strict On

' always use the two options above because they allow the compiler
' to catch things which can cause run time errors

Imports System.Threading

' System.Threading needed for the Sleep function


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim StartTime As DateTime = DateTime.Now 'for calc of elapsed
time

Dim ElapsedTime As TimeSpan 'for calc of elapsed time

Dim ElapsedTimeText As String ' for displaying elapsed time

Thread.Sleep(1000) 'sleep for 1 second

ElapsedTime = DateTime.op_Subtraction(DateTime.Now, StartTime)

ElapsedTimeText = String.Format("{0,2:#0'h'} {1,3:#0'm'}" & _
" {2,3:#0's'}{3,7:##0'ms'}", _
ElapsedTime.Hours, ElapsedTime.Minutes, _
ElapsedTime.Seconds, ElapsedTime.Milliseconds)

' although this is my code (I can't blame it on anyone else), I
' don't understand how I get away with the above call to
' String.Format as the overloaded String.Format method doesn't,
' so far as I can see, support this number of arguments

' the formatting specification is not intuitive and documentation
can
' be difficult to find. But if you look at the doc for String
Members
' and then the Format method and then follow the link to
' Composite Formatting I think you'll find pretty decent
documentation.

MsgBox("My formatting yields: " & ElapsedTimeText)

MsgBox("TimeSpan.ToString yields: " & ElapsedTime.ToString)

End Sub
End Class

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

Good Luck, Bob

"JungleJim74" <JungleJim74 (AT) discussions (DOT) microsoft.com> wrote in message
news:F55DE265-6E07-4F1F-9496-08314D182002 (AT) microsoft (DOT) com...
Thank you Hans but I am beginning to believe that I am in over my head
and
that I need to study VB.NET a lot more BEFORE I try to write and compile
this
tyoe of program. I have tried to follow all of the responses posted to my
request for help but no matter which one I try all I get is compiler
errors.
If the code does not compile you sure cannot run an executable. But I
thank
you both again for trying to help me. JungleJim74 (I'm 81 years old and I
guess I just don't have it anymore)

"Hans Kesting" wrote:

JungleJim74 formulated the question :
I am using Visual Studio 2003 and am trying to compute and display the
elapsed time that my computer takes to calculate ((2.154*33)-161.376 /
(3.541*7)). I have the interface prepared and the program makes this
calculation 2,000,000,000 in 5 seconds. I display the start time and
the end
time with start = Now and end = Now and this works OK for the display
purposes. But I do not know how to compute the elapsed time. I have
declared
the variables "start"and ënd" as strings and this declaration may be
the
source of my trouble. Thanks in advance for any help on this.
JungleJim74

Just a note: if that formula is a copy from your test-program, then you
might end up testing the wrong thing! The compiler already performs
calculations with fixed numbers, so runtime the formula would be
replaced by a single value.
A formula "a=2*3" is compiled as "a=6".

Hans Kesting






Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.