Results 1 to 12 of 12

Thread: A DateTime question (Average time)

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  3. #3
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    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")

  4. #4

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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:
    1. 'In a Timer's tick event for 1000milli
    2. scanTime.Text = _
    3.             mSwatch.Elapsed.Hours.ToString("00") & ":" & _
    4.             mSwatch.Elapsed.Minutes.ToString("00") & ":" & _
    5.             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?

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  6. #6

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    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")

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: A DateTime question (Average time)

    In your original post, you saked:
    Quote Originally Posted by stateofidleness View Post
    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 -

  9. #9

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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".

  10. #10
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    Re: A DateTime question (Average time)

    Code:
    Debug.Print("Average is " & (elapsed.TotalMinutes / loops) & " minutes")

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  12. #12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width