Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

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!
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops... in the "clean" code, move the database update ;)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Corey2,

Yep... could do that. But I guarantee the bottleneck is the code to connect to the database and update the record....
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.
Fair enough... just didn't want to send him chasing his own tail. Database connections are almost always the culprit...
Avatar of indy500fan
indy500fan

ASKER

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
give Chaosian more points if you give me any at all