|
-
Jul 9th, 2009, 02:17 PM
#1
Thread Starter
Frenzied Member
A DateTime question (Average time)
Hey guys,
I run a "scan" through the rows of a DataGridView and at the end of the scan, I have stored how long the scan took.
What I want to do is be able to get the average scan time of all completed scans, but I'm not sure how to do math operations with the Time. (format I have is 00:00:00)
let's say I do 1000 scans, does that mean I have to store all 1000 scan durations to be able to get a current average? any easier way to keep a running average of duration of completed scans?
another thing, How can I test if the Date is Friday?
-
Jul 9th, 2009, 02:25 PM
#2
Re: A DateTime question (Average time)
The duration of each scan is a TimeSpan, not a DateTime value. You can perform an average by keeping track of how many items completed and for each completed item you add the duration to the overall timespan. Get the totalseconds (or miliseconds or minutes... it's up to you to decide how accurate you want to get) from the overall timespan, divide it by the completed count and you have the average scan time of each item.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 9th, 2009, 02:27 PM
#3
Hyperactive Member
Re: A DateTime question (Average time)
Use the DateTime and TimeSpan variables:
Code:
Dim start As DateTime
Dim elapsed As TimeSpan
start = Now
Delay(1.23)
elapsed = Now - start
Debug.Print("Process started at " & start.ToString("HH:mm:ss") & " on " & start.DayOfWeek.ToString)
Debug.Print("Process took " & elapsed.TotalSeconds & " seconds")
-
Jul 9th, 2009, 02:40 PM
#4
Thread Starter
Frenzied Member
Re: A DateTime question (Average time)
Thought I should add the code I'm using to do a scan time. It uses a StopWatch object.
vb Code:
'In a Timer's tick event for 1000milli
scanTime.Text = _
mSwatch.Elapsed.Hours.ToString("00") & ":" & _
mSwatch.Elapsed.Minutes.ToString("00") & ":" & _
mSwatch.Elapsed.Seconds.ToString("00")
now looking at your code there Jacques, that looks pretty nifty.
let's say I implement your code above to replace mine to keep track of scan durations. to get an average, I would just add the previos elapsed time to 'elapsed' then divide by number of scans completed?
-
Jul 9th, 2009, 02:56 PM
#5
Re: A DateTime question (Average time)
Do something like this:
Code:
Dim totalDuration As New TimeSpan(0)
Dim startTime As DateTime
Dim completedCount As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
startTime = Date.Now
'Do you scan here with this row
'
'
'When done, add 1 to completedCount
completedCount += 1
'Add the elapsed time to totalDuration
totalDuration += Date.Now.Subtract(startTime)
'Calculate the average
LabelAverage.Text = String.Format("Average scan time per item is: {0} seconds", (totalDuration.TotalSeconds / completedCount).ToString("F3"))
Next
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 9th, 2009, 03:04 PM
#6
Thread Starter
Frenzied Member
Re: A DateTime question (Average time)
thanks stan, but mine stores the duration of all rows, which is roughly 17 minutes for a full scan to complete. I don't really worry about each row individually.
-
Jul 9th, 2009, 03:23 PM
#7
Hyperactive Member
Re: A DateTime question (Average time)
Here is a way to get your average:
Code:
Dim start As DateTime
Dim elapsed As TimeSpan
Dim loops As Integer = 3
start = Now
For Attempts As Integer = 1 To loops
Delay(1.23)
Next
elapsed = Now - start
Debug.Print("Process started at " & start.ToString("HH:mm:ss") & " on " & start.DayOfWeek.ToString)
Debug.Print("Process took " & elapsed.TotalSeconds & " seconds")
Debug.Print("Average is " & (elapsed.TotalSeconds / loops) & " seconds")
-
Jul 9th, 2009, 03:29 PM
#8
Re: A DateTime question (Average time)
In your original post, you saked:
 Originally Posted by stateofidleness
Hey guys,
let's say I do 1000 scans, does that mean I have to store all 1000 scan durations to be able to get a current average? any easier way to keep a running average of duration of completed scans?
And my code showed you how to do just that. It calculates and updates the average every time a item scan completes and you don't have to wait 17 minutes to get the average.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 9th, 2009, 03:34 PM
#9
Thread Starter
Frenzied Member
Re: A DateTime question (Average time)
but i'm not really doing an "item" scan, i'm doing a "list" scan. after it goes through all the rows (17 minutes later), it increments my "scanCount" variable by 1.
Jacques' last post looks like it should work, I just need to convert the seconds to minutes with a simple "/60".
-
Jul 9th, 2009, 03:43 PM
#10
Hyperactive Member
Re: A DateTime question (Average time)
Code:
Debug.Print("Average is " & (elapsed.TotalMinutes / loops) & " minutes")
-
Jul 9th, 2009, 03:45 PM
#11
Re: A DateTime question (Average time)
When I use the term "item", it means whatever object that your scan performs on. And in you original post again, you said:
Code:
I run a "scan" through the rows of a DataGridView and at the end of the scan, I have stored how long the scan took.
So in the code I posted, I used a for each loop to loop thru a datagridview.rows collection, which is exactly what you wanted in the beginning. But anyway, it's your program and you can choose to change it anytime when need to, right
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 9th, 2009, 04:01 PM
#12
Thread Starter
Frenzied Member
Re: A DateTime question (Average time)
I hear ya, translation error. 'ppreciate the help from you both!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|