Solved

Better way of calculating speeds?

Posted on 2005-05-05
Medium Priority
287 Views
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?

0
Question by:indy500fan
• 5
• 3

LVL 24

Expert Comment

ID: 13938569
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

LVL 24

Accepted Solution

Jeff Certain earned 1600 total points
ID: 13938599
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

LVL 24

Expert Comment

ID: 13938753
Oops... in the "clean" code, move the database update ;)
0

LVL 13

Assisted Solution

Corey Scheich earned 400 total points
ID: 13938851
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

LVL 24

Expert Comment

ID: 13938961
Corey2,

Yep... could do that. But I guarantee the bottleneck is the code to connect to the database and update the record....
0

LVL 13

Expert Comment

ID: 13938995
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

LVL 24

Expert Comment

ID: 13939025
Fair enough... just didn't want to send him chasing his own tail. Database connections are almost always the culprit...
0

Author Comment

ID: 13939083
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

LVL 13

Expert Comment

ID: 13939137
give Chaosian more points if you give me any at all
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and lâ€¦
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recentâ€¦
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signatâ€¦
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery procedâ€¦
Suggested Courses
Course of the Month16 days, 4 hours left to enroll