locked
Time between time-stamps... with running average RRS feed

  • Question

  • I need to figure out the time between timestamps and then average that time by the number of timestamps. Not sure how to go about this... 
    • Changed type GWBurt Thursday, December 8, 2011 6:13 PM
    Thursday, December 8, 2011 4:55 PM

Answers

  • I thought you were looking for more accuracy which is why I used  .GetTimestamp.  It should still work.

     

    Public Class Form1

        Dim foo As New List(Of Long)
        Dim last As Nullable(Of Long)

        Private Sub Button1_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button1.Click
            'record time between clicks
            Dim temp As Long = Stopwatch.GetTimestamp()
            If last.HasValue Then
                foo.Add(temp - last.Value)
            End If
            last = temp
        End Sub

        Private Sub Button2_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button2.Click
            'show average
            If foo.Count > 0 Then
                ListBox1.Items.Clear()
                Dim temp As Integer = CInt((foo.Sum / Stopwatch.Frequency) / foo.Count) 'the average
                Label1.Text = New TimeSpan(0, 0, temp).ToString
                For Each fooVal As Long In foo
                    temp = CInt(fooVal / Stopwatch.Frequency) 'convert to seconds
                    ListBox1.Items.Add(New TimeSpan(0, 0, temp).ToString)
                Next
                foo.Clear()
                last = Nothing
            Else
                Debug.WriteLine("nothing")
            End If
        End Sub
    End Class


    Serial Port      Random       Check Internet Connection      Microsoft® Community Contributor 2011
    • Marked as answer by GWBurt Friday, December 9, 2011 3:53 PM
    Thursday, December 8, 2011 11:09 PM

All replies

  • The Date and TimeSpan ojects have a Ticks property you should be able to use that for your average
    Thursday, December 8, 2011 5:04 PM
  • If you store your time stamps as a DateTime, you can subtract them to get a TimeSpan.  These can then be added together as needed.

     

    Dim timeStamps As List(Of DateTime) = GetTimeStamps()
    
    Dim numberOfIntervals As Integer = timeStamps.Count - 1
    
    Dim totalLengths As New TimeSpan(0)
    
    For i As Integer = 1 To timeStamps.Count - 1
    	totalLengths = totalLengths.Add(timeStamps(i) - timeStamps(i - 1))
    Next
    
    Dim average As TimeSpan = TimeSpan.FromTicks(totalLengths.Ticks / numberOfIntervals)
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 8, 2011 5:04 PM
  • Thanks for the reply. How would you suggest I store the timestamo...maybe in sql? I'm starting form scratch... right now I just have a button on the form... a new project
    Thursday, December 8, 2011 6:16 PM
  • Thanks for the reply. How would you suggest I store the timestamo...maybe in sql? I'm starting form scratch... right now I just have a button on the form... a new project

    If this is just for keeping track within your application, I'd just save the DateTime using DateTime.Now into a List(Of DateTime) as you go.  When it's time to get the average, the code above should work fine...

     

    If you need to persist these between application runs, then saving them into a database makes sense.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 8, 2011 6:28 PM
  • Thank you..could you please give an example of how to do what you just described ... for now I will use a button to initiate the datetime stamp - i will keep tracjk in the application and not use sql
    • Edited by GWBurt Thursday, December 8, 2011 6:44 PM added info
    Thursday, December 8, 2011 6:39 PM
  • Say you have a form - just add a local variable for your list of  datetime, and on the button click, add to it:

     

     

    ' Your form class...
    Public Class YourForm
    	Inherits Form
    	Public Sub New()
    		InitializeComponent()
    	End Sub
    
    	' Add a list:
    	Private timeStamps As New List(Of DateTime)
    
    	' On button click, add a timestamp:
    	Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
    		' Add "now" in as a new time stamp
    		timeStamps.Add(DateTime.Now)
    	End Sub
    End Class


     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, December 8, 2011 6:41 PM
  • You are active in the Visual Basic forum

     

    Thanks - get confused sometimes ;)  (Fixed now)
    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 8, 2011 6:56 PM
  • This shows average time between button1 clicks

    Public Class Form1

        Dim foo As New List(Of Long)
        Dim last As Nullable(Of Long)

        Private Sub Button1_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button1.Click
            'record time between clicks
            Dim temp As Long = Stopwatch.GetTimestamp()
            If last.HasValue Then
                foo.Add(temp - last.Value)
            End If
            last = temp
        End Sub

        Private Sub Button2_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button2.Click
            'show average
            If foo.Count > 0 Then
                Dim theAvg As Double = (foo.Sum / Stopwatch.Frequency) / foo.Count
                Debug.WriteLine(theAvg)
                foo.Clear()
                last = Nothing
            Else
                Debug.WriteLine("nothing")
            End If
        End Sub
    End Class


    Serial Port      Random       Check Internet Connection      Microsoft® Community Contributor 2011
    • Edited by dbasnett Thursday, December 8, 2011 8:02 PM
    Thursday, December 8, 2011 8:02 PM
  • Thanks for this last bit... how can I view what is in list "foo"?
    Thursday, December 8, 2011 9:29 PM
  •  

    Note that you will be looking at Stopwatch.GetTimestamp ticks.

            For Each fooVal As Long In foo
                Debug.WriteLine(fooVal.ToString)
            Next

     

    What are you looking for?


    Serial Port      Random       Check Internet Connection      Microsoft® Community Contributor 2011
    • Edited by dbasnett Thursday, December 8, 2011 9:36 PM
    Thursday, December 8, 2011 9:34 PM
  • What I'm trying to do is list the times .. probably in a listbox. Also, this works great for seconds but how do I show the time in hours/min/seconds?

    My form needs to have a listbox or something similar showing the datetimestamp and then another display showing the average time in hour min sec between the button1 clicks... 

    Right now it works fine but I cant see the list of datetimestamps and the average is in seconds

    Thanks a ton for your help

    Thursday, December 8, 2011 9:51 PM
  • I thought you were looking for more accuracy which is why I used  .GetTimestamp.  It should still work.

     

    Public Class Form1

        Dim foo As New List(Of Long)
        Dim last As Nullable(Of Long)

        Private Sub Button1_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button1.Click
            'record time between clicks
            Dim temp As Long = Stopwatch.GetTimestamp()
            If last.HasValue Then
                foo.Add(temp - last.Value)
            End If
            last = temp
        End Sub

        Private Sub Button2_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button2.Click
            'show average
            If foo.Count > 0 Then
                ListBox1.Items.Clear()
                Dim temp As Integer = CInt((foo.Sum / Stopwatch.Frequency) / foo.Count) 'the average
                Label1.Text = New TimeSpan(0, 0, temp).ToString
                For Each fooVal As Long In foo
                    temp = CInt(fooVal / Stopwatch.Frequency) 'convert to seconds
                    ListBox1.Items.Add(New TimeSpan(0, 0, temp).ToString)
                Next
                foo.Clear()
                last = Nothing
            Else
                Debug.WriteLine("nothing")
            End If
        End Sub
    End Class


    Serial Port      Random       Check Internet Connection      Microsoft® Community Contributor 2011
    • Marked as answer by GWBurt Friday, December 9, 2011 3:53 PM
    Thursday, December 8, 2011 11:09 PM