datetime.now

mirzao

Active member
Joined
Nov 10, 2005
Messages
44
Location
Malaysia
Programming Experience
Beginner
Hi,

I used datetime function to know how much time it takes to finish the lines inside a do loop. But the problem is, it only gives me the correct value for the first 20 loops for example and then it start to give the wrong value.

Do
starttime=datetime.now
For
'do sth
next
stoptime=datetime.now

span=stoptime.subtract(starttime)

dt =
CInt(span.Seconds)
Label1.Text = dt
Loop

I convert the timespan into integer because I want to use it in calculation.Anyone can point me the problem?many thanks
 
Take a look at the help topic for the TimeSpan members. What you want is TotalSeconds, not Seconds. Seconds is just the number of whole seconds past that last full minute, while TotalSeconds is the number of seconds in the entire period. Note that Seconds is already an Integer while TotalSeconds is a Double. Read, don't assume.
 
How about this?
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] T1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = Now
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 10000
[/SIZE][SIZE=2][COLOR=#0000ff] Me[/COLOR][/SIZE][SIZE=2].Text = i
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] T2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = Now
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] span [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TimeSpan = T2.Subtract(T1)
[/SIZE][SIZE=2][COLOR=#008000]'show how long it took to generate text for the form 10000 times
[/COLOR][/SIZE][SIZE=2]MessageBox.Show(span.Seconds.ToString) [/SIZE][SIZE=2][COLOR=#008000]'1st choice
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("It took " & ((T2.Ticks - T1.Ticks) / 10000000).ToString & " secs to create text for the current form") [/SIZE][SIZE=2][COLOR=#008000]'2nd choice with more precisely result[/COLOR][/SIZE]

Regards ;)
 
There's no need to use the Ticks properties of the Date objects. TotalSeconds is a Double so it will give you the total time in seconds to the same precision. Also, the TimeSpan has its own Ticks property. The help/MSDN topic for TotalSeconds gives examples of how the Seconds and TotalSeconds properties behave, as well as Days, TotalDays, Hours, TotalHours, Minutes, TotalMinutes, Milliseconds, TotalMilliseconds and Ticks.
 
Thanks guys but I have another question here. Supposed, it only takes about 50-60 seconds to complete 1 loop but it becomes slower and slower after some times.For your info,this application will continously run unless someone pressed the stop button. How do I prevent this?Any idea guys?Thanks again.
 
Last edited:
Hmm...but it's not what i'm looking for. I want my application running without interruption. The issue is it doesn't responding after a few hours running. What causes this? Why it becomes slower with time.
 
mirzao said:
Hmm...but it's not what i'm looking for. I want my application running without interruption. The issue is it doesn't responding after a few hours running. What causes this? Why it becomes slower with time.
It could be any number of things. It would be pure speculation without having seen the code involved. You may not be disposing objects appropriately or god knows what.
 
Back
Top