• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Better way of calculating speeds?

Friends,

I have a program that collects lap times from a serial stream, and when it collects the appropriate time, it assigns it to a variable and a text field on a form.  Now, a change in the text (specifically for Lap4) fires off the following set of instructions:

Private Sub txtLap4Time_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLap4Time.TextChanged
        Try

            FieldName = "Lap4Time"
            NewValue = txtLap4Time.Text
            LapTime = txtLap4Time.Text

            Dim QualifyingTime As Decimal

            If txtLap4Time.Text = 0 Then
                Speed = 0.0
                txtLap4Speed.Text = Speed
            ElseIf txtLap4Time.Text = Nothing Then
                Speed = Nothing
                txtWarmup1Speed.Text = Nothing
            Else
                UpdateDatabase(dsn, FieldName, NewValue)
                Speed = CalculateSpeed(LapTime)
                txtLap4Speed.Text = Speed

                QualifyingTime = CalculateQualifyingTime()
                txtQualifyingTime.Text = QualifyingTime

                SetQualifyingSpeed(QualifyingTime)

            End If

        Catch ex As Exception

        End Try
    End Sub

As you can see it first updates the database with the laptime, but then it calculates that speed for that lap, and then it goes
and calculates the qualifying time for the entire run (all 4 laps) and displays it in the txtQualifying text box, and then it goes and calculates the QualifyingSpeed.  

See the Qualifying Time and Speed functions below:


    Public Sub SetQualifyingSpeed(ByVal QualifyingTime As Decimal)
        'Dim QualifyingSpeed
        QualSpeed = CalculateQualifyingSpeed(QualifyingTime)
        txtQualifyingSpeed.Text = QualSpeed

    End Sub

     Private Function CalculateSpeed(ByVal LapTime As Decimal)

        Dim ElapsedTime As Decimal
        Dim Distance As Decimal

        ElapsedTime = LapTime / 3600
        Distance = 2.5

        Speed = (Distance / ElapsedTime)

        Return Math.Round(Speed, 3)

    End Function

    Private Function CalculateQualifyingTime()

        Dim ElapsedTime As Decimal
        'Dim Distance As Decimal

        Dim Lap1Time As Decimal
        Dim Lap2Time As Decimal
        Dim Lap3Time As Decimal
        Dim Lap4Time As Decimal


        Lap1Time = txtLap1Time.Text
        Lap2Time = txtLap2Time.Text
        Lap3Time = txtLap3Time.Text
        Lap4Time = txtLap4Time.Text

        ElapsedTime = (Lap1Time + Lap2Time + Lap3Time + Lap4Time)
        'Distance = 2.5

        'Speed = (Distance / ElapsedTime)

        Return ElapsedTime
    End Function

    Private Function CalculateQualifyingSpeed(ByVal QualifyingLapTime As Decimal)

        Dim ElapsedTime As Decimal
        Dim Distance As Decimal

        ElapsedTime = QualifyingLapTime / 3600
        Distance = 10

        QualSpeed = (Distance / ElapsedTime)

        Return Math.Round(QualSpeed, 3)

    End Function

When this runs, the txtLap4Time textbox gets updated instantly, and the calculation and display of the txtLap4Speed is very quick, but the txtQualifyingTime and txtQualifyingSpeed are sort of slow to update.  Is there anyway that you guys can see to fix that?

Thanks in advance!
0
indy500fan
Asked:
indy500fan
  • 5
  • 3
2 Solutions
 
Jeff CertainCommented:
The issue is most likely the call to update the database. Nothing else in there should take any significant amount of time. Try moving the database update.

 If txtLap4Time.Text = 0 Then
                Speed = 0.0
                txtLap4Speed.Text = Speed
            ElseIf txtLap4Time.Text = Nothing Then
                Speed = Nothing
                txtWarmup1Speed.Text = Nothing
            Else  
                Speed = CalculateSpeed(LapTime)
                txtLap4Speed.Text = Speed

                QualifyingTime = CalculateQualifyingTime()
                txtQualifyingTime.Text = QualifyingTime

                SetQualifyingSpeed(QualifyingTime)
UpdateDatabase(dsn, FieldName, NewValue)
            End If
0
 
Jeff CertainCommented:
To clean things up a bit:

            LapTime = Single.Parse(txtLap4Time.Text)

            Dim QualifyingTime As Decimal

            If LapTime = 0 Then
                Speed = 0.0
                txtLap4Speed.Text = Speed
            ElseIf IsNothing(LapTime) Then
                Speed = Nothing
                txtWarmup1Speed.Text = ""
            Else
                UpdateDatabase(dsn, "Lap4Time", LapTime)
                Speed = CalculateSpeed(LapTime)
                txtLap4Speed.Text = Speed

                QualifyingTime = CalculateQualifyingTime()
                txtQualifyingTime.Text = QualifyingTime

                SetQualifyingSpeed(QualifyingTime)

            End If
0
 
Jeff CertainCommented:
Oops... in the "clean" code, move the database update ;)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Corey ScheichDeveloperCommented:
Try stepping through the code you could log time values between operations to narrow down where the bottle neck is.
Function OneofYourSubs ()
  Dim StartTime as DateTime = now

  'do calculations


   msgbox("OneofYourSubs took: " & now.subtract(Starttime).tostring)
  Return Value
End Function
0
 
Jeff CertainCommented:
Corey2,

Yep... could do that. But I guarantee the bottleneck is the code to connect to the database and update the record....
0
 
Corey ScheichDeveloperCommented:
I was just giving him a way to test if he runs into a similar problem in the future where he doesn't know where the bottle neck is.
0
 
Jeff CertainCommented:
Fair enough... just didn't want to send him chasing his own tail. Database connections are almost always the culprit...
0
 
indy500fanAuthor Commented:
Wow,

Thanks for both of your input.

Would it be alright if I split the points for both of you?  250 a piece?  Chaosian, you confirmed my suspicions, but Corey2 gave me a neat little tool for troubleshooting.  What do each of you feel is fair?

Regards,
Eric
0
 
Corey ScheichDeveloperCommented:
give Chaosian more points if you give me any at all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now