Click to See Complete Forum and Search --> : Very Urgent Help needed on .net programming...


johnny123
June 13th, 2009, 11:09 PM
Hello everybody,

I am working with .net programming. i am starter and new for .net programming. somehow i have written a code hardly. the code is working but it is taking time to calculate results. i wanted to make the calculation faster and reduce the code. its a very simple code which retrives the values from sql tables and calculates the average of 6 values. but i made it complicated. You guys please help me to reduce the code its very urgent dudes....:sick:

Here is the code....it is very big code...actually the first 20 lines repeats next 5 loops with different variables thats it..i want to make it into 1 single loop..i dont know how to do it...i will explain the code...

step1: there a 6 strings to be passed to sql database which contains stored procedures to calculate a position which passes the calculated position and probabilty to .net parameter again. i need to show the position in gui finally.

step2: Initially the 6 strings are passed and stored procedures are called to calculate position which the code is not included :)...its done already and calculation is fast....secondly i need to pass only 5 strings every time excluding one string. so i copied the same code passing only 5 by excluding 1 each and every time by FOR loop...so i am doing same process by 6 times and calculating the average of all 6 positions...

step3: what i need is... can i reduce this code by making it to single loop...to make calculation faster by using .net programming techniques...i am beginner to .net i dont know any techniques......please help me friends....if you have any questions please let me know..... please go through the code..you can understand....






Dim macs(), prob_proc() As String
Dim k, kk As Integer
Dim final_position As Integer
Dim max1(5) As Long
Dim position(5) As Long
Dim probablistic1(49) As Long

'this is the case when 1st macid is turned off...........

macs = New String() {"00112052E170", "0011218F1A60", "00112052ECC0", "00112052D840", "00112052F010", "00112052E5D0"}
prob_proc = New String() {"probs1", "probs2", "probs3", "probs4", "probs5", "probs6", "probs7", "probs8", "probs9", "probs10", "probs11", "probs12", "probs13", "probs14", "probs15", "probs16", "probs17", "probs18", "probs19", "probs20", "probs21", "probs22", "probs23", "probs24", "probs25", "probs26", "probs27", "probs28", "probs29", "probs30", "probs31", "probs32", "probs33", "probs34", "probs35", "probs36", "probs37", "probs38", "probs39", "probs40", "probs41", "probs42", "probs43", "probs44", "probs45", "probs46", "probs47", "probs48", "probs49", "probs50"}
For kk = 0 To 49
For k = 0 To 4
sqlcomm3.Connection = sqlconn
sqlcomm3.CommandType = CommandType.StoredProcedure
sqlcomm3.CommandText = prob_proc(kk)
sqlcomm3.Parameters.Clear()
q = New SqlParameter
q.ParameterName = "@macs"
q.Direction = ParameterDirection.Input
q.SqlDbType = SqlDbType.NVarChar
q.Value = macs(k)
sqlcomm3.Parameters.Add(q)
q = New SqlParameter
q.ParameterName = "@gaussian"
q.Direction = ParameterDirection.Output
q.SqlDbType = SqlDbType.Int
sqlcomm3.Parameters.Add(q)
Try
sqlcomm3.ExecuteNonQuery()
Catch ex As Exception
q.Value = 0
End Try

Try
If Convert.IsDBNull(q.Value) = True Then
q.Value = 25
End If

If q.Value < 25 Then
q.Value = 25
End If

If k = 0 Then
probablistic1(kk) = q.Value
Else
probablistic1(kk) = probablistic1(kk) * q.Value
End If
Catch ex As Exception
If k = 0 Then
probablistic1(kk) = 25
Else
probablistic1(kk) = probablistic1(kk) * 25
End If
End Try
Next
Next
max1(0) = probablistic1(0)
For kk = 0 To 49
If max1(0) < probablistic1(kk) Then
max1(0) = probablistic1(kk)
position(0) = kk
End If
Next
'__________________________________________________________________________________________________
'2)this is the case with 2nd macid is turned off.....
Dim probablistic2(49) As Long
macs = New String() {"0011218F1A60", "00112052ECC0", "00112052D840", "00112052F010", "00112052E5D0", "00112052E170"}
For kk = 0 To 49
For k = 0 To 4
sqlcomm3.Connection = sqlconn
sqlcomm3.CommandType = CommandType.StoredProcedure

sqlcomm3.CommandText = prob_proc(kk)
sqlcomm3.Parameters.Clear()
q = New SqlParameter
q.ParameterName = "@macs"
q.Direction = ParameterDirection.Input
q.SqlDbType = SqlDbType.NVarChar
q.Value = macs(k)
sqlcomm3.Parameters.Add(q)
q = New SqlParameter
q.ParameterName = "@gaussian"
q.Direction = ParameterDirection.Output
q.SqlDbType = SqlDbType.Int
sqlcomm3.Parameters.Add(q)
Try
sqlcomm3.ExecuteNonQuery()
Catch ex As Exception
q.Value = 0
End Try

Try
If Convert.IsDBNull(q.Value) = True Then
q.Value = 25
End If

If q.Value < 25 Then
q.Value = 25
End If

If k = 0 Then
probablistic2(kk) = q.Value
Else
probablistic2(kk) = probablistic2(kk) * q.Value
End If
Catch ex As Exception
If k = 0 Then
probablistic2(kk) = 25
Else
probablistic2(kk) = probablistic2(kk) * 25
End If
End Try
Next
Next
max1(1) = probablistic2(0)
For kk = 0 To 49
If max1(1) < probablistic2(kk) Then
max1(1) = probablistic2(kk)
position(1) = kk
End If
Next
'--------------------------------------------------------------------------------------------------
'this is the case for 3rd macid is off...
Dim probablistic3(49) As Long
macs = New String() {"00112052ECC0", "00112052D840", "00112052F010", "00112052E5D0", "00112052E170", "0011218F1A60"}
For kk = 0 To 49
For k = 0 To 4
sqlcomm3.Connection = sqlconn
sqlcomm3.CommandType = CommandType.StoredProcedure

sqlcomm3.CommandText = prob_proc(kk)
sqlcomm3.Parameters.Clear()
q = New SqlParameter
q.ParameterName = "@macs"
q.Direction = ParameterDirection.Input
q.SqlDbType = SqlDbType.NVarChar
q.Value = macs(k)
sqlcomm3.Parameters.Add(q)
q = New SqlParameter
q.ParameterName = "@gaussian"
q.Direction = ParameterDirection.Output
q.SqlDbType = SqlDbType.Int
sqlcomm3.Parameters.Add(q)
Try
sqlcomm3.ExecuteNonQuery()
Catch ex As Exception
q.Value = 0
End Try

Try
If Convert.IsDBNull(q.Value) = True Then
q.Value = 25
End If

If q.Value < 25 Then
q.Value = 25
End If

If k = 0 Then
probablistic3(kk) = q.Value
Else
probablistic3(kk) = probablistic3(kk) * q.Value
End If
Catch ex As Exception
If k = 0 Then
probablistic3(kk) = 25
Else
probablistic3(kk) = probablistic3(kk) * 25
End If
End Try
Next
Next
max1(2) = probablistic3(0)
For kk = 0 To 49
If max1(2) < probablistic3(kk) Then
max1(2) = probablistic3(kk)
position(2) = kk
End If
Next
'--------------------------------------------------------------------------------------------------------------
'this is the case when 4th macid is off.....
macs = New String() {"00112052D840", "00112052F010", "00112052E5D0", "00112052E170", "0011218F1A60", "00112052ECC0"}
Dim probablistic4(49) As Long
For kk = 0 To 49
For k = 0 To 4
sqlcomm3.Connection = sqlconn
sqlcomm3.CommandType = CommandType.StoredProcedure

sqlcomm3.CommandText = prob_proc(kk)
sqlcomm3.Parameters.Clear()
q = New SqlParameter
q.ParameterName = "@macs"
q.Direction = ParameterDirection.Input
q.SqlDbType = SqlDbType.NVarChar
q.Value = macs(k)
sqlcomm3.Parameters.Add(q)
q = New SqlParameter
q.ParameterName = "@gaussian"
q.Direction = ParameterDirection.Output
q.SqlDbType = SqlDbType.Int
sqlcomm3.Parameters.Add(q)
Try
sqlcomm3.ExecuteNonQuery()
Catch ex As Exception
q.Value = 0
End Try

Try
If Convert.IsDBNull(q.Value) = True Then
q.Value = 25
End If

If q.Value < 25 Then
q.Value = 25
End If

If k = 0 Then
probablistic4(kk) = q.Value
Else
probablistic4(kk) = probablistic4(kk) * q.Value
End If
Catch ex As Exception
If k = 0 Then
probablistic4(kk) = 25
Else
probablistic4(kk) = probablistic4(kk) * 25
End If
End Try
Next
Next
max1(3) = probablistic4(0)
For kk = 0 To 49
If max1(3) < probablistic4(kk) Then
max1(3) = probablistic4(kk)
position(3) = kk
End If
Next
'--------------------------------------------------------------------------------------------------
'this is the case when 5th macid is turned off......

macs = New String() {"00112052F010", "00112052E5D0", "00112052E170", "0011218F1A60", "00112052ECC0", "00112052D840"}
Dim probablistic5(49) As Long
For kk = 0 To 49
For k = 0 To 4
sqlcomm3.Connection = sqlconn
sqlcomm3.CommandType = CommandType.StoredProcedure

sqlcomm3.CommandText = prob_proc(kk)
sqlcomm3.Parameters.Clear()
q = New SqlParameter
q.ParameterName = "@macs"
q.Direction = ParameterDirection.Input
q.SqlDbType = SqlDbType.NVarChar
q.Value = macs(k)
sqlcomm3.Parameters.Add(q)
q = New SqlParameter
q.ParameterName = "@gaussian"
q.Direction = ParameterDirection.Output
q.SqlDbType = SqlDbType.Int
sqlcomm3.Parameters.Add(q)
Try
sqlcomm3.ExecuteNonQuery()
Catch ex As Exception
q.Value = 0
End Try

Try
If Convert.IsDBNull(q.Value) = True Then
q.Value = 25
End If

If q.Value < 25 Then
q.Value = 25
End If

If k = 0 Then
probablistic5(kk) = q.Value
Else
probablistic5(kk) = probablistic5(kk) * q.Value
End If
Catch ex As Exception
If k = 0 Then
probablistic5(kk) = 25
Else
probablistic5(kk) = probablistic5(kk) * 25
End If
End Try
Next
Next
max1(4) = probablistic5(0)
For kk = 0 To 49
If max1(4) < probablistic5(kk) Then
max1(4) = probablistic5(kk)
position(4) = kk
End If
Next
'-------------------------------------------------------------------------------------------------
'this is the case when 6th macid is turned off....
macs = New String() {"00112052E5D0", "00112052E170", "0011218F1A60", "00112052ECC0", "00112052D840", "00112052F010"}
Dim probablistic6(49) As Long
For kk = 0 To 49
For k = 0 To 4
sqlcomm3.Connection = sqlconn
sqlcomm3.CommandType = CommandType.StoredProcedure
sqlcomm3.CommandText = prob_proc(kk)
sqlcomm3.Parameters.Clear()
q = New SqlParameter
q.ParameterName = "@macs"
q.Direction = ParameterDirection.Input
q.SqlDbType = SqlDbType.NVarChar
q.Value = macs(k)
sqlcomm3.Parameters.Add(q)
q = New SqlParameter
q.ParameterName = "@gaussian"
q.Direction = ParameterDirection.Output
q.SqlDbType = SqlDbType.Int
sqlcomm3.Parameters.Add(q)
Try
sqlcomm3.ExecuteNonQuery()
Catch ex As Exception
q.Value = 0
End Try

Try
If Convert.IsDBNull(q.Value) = True Then
q.Value = 25
End If

If q.Value < 25 Then
q.Value = 25
End If

If k = 0 Then
probablistic6(kk) = q.Value
Else
probablistic6(kk) = probablistic6(kk) * q.Value
End If
Catch ex As Exception
If k = 0 Then
probablistic6(kk) = 25
Else
probablistic6(kk) = probablistic6(kk) * 25
End If
End Try
Next
Next
max1(5) = probablistic6(0)
For kk = 0 To 49
If max1(5) < probablistic6(kk) Then
max1(5) = probablistic6(kk)
position(5) = kk
End If
Next

final_position = (position(0) + position(1) + position(2) + position(3) + position(4) + position(5)) / 6

Me.Controls.Remove(pbox)
SuspendLayout()
pbox.Image = System.Drawing.Image.FromFile(("C:\XandY\Image\dot2.jpg"))
pbox.Top = pointx(final_position + 1).Y
pbox.Left = pointx(final_position + 1).X
pbox.Width = 10
pbox.Height = 10
Me.Controls.Add(pbox)
pbox.BringToFront()
ResumeLayout()
End If
End If
End Sub

dglienna
June 14th, 2009, 12:04 AM
Download this link (http://code.msdn.microsoft.com/vbsamples/)
and step thru the code line by line

johnny123
June 14th, 2009, 12:33 AM
Hi..thank you so much for reply....i have seen the link and it seems to be new features in .net....as i said you i am a beginner in .net programming. .. can you please make that code to single loop instead of repeating the same code 6 times.....i would appreciate greatly for your help......

dglienna
June 14th, 2009, 03:55 PM
Explain it a little better. I see 6 MAC addresses, but it isn't clear what each loop is doing. I don't need every last detail, but I need to see WHAT is looping before I could help you combine them into one loop

I suppose you could just loop BACKWARDS down to 0?

johnny123
June 14th, 2009, 04:28 PM
Hi dglienna,

Thanks for reply dude....here i will explain the code in detail...
initially we pass 6 macids
But the second FOR loop considers only first 5 macids
i.e; For k=0 to 4 and calulates the value

in the next cycle ( i mean when 2nd macid is turned off comment) you can observe that the macids are changed...
the first macid in the first cycle is moved to last...this makes the diference... now in the second cycle the same For loop considers only 5 macids but the next 5 ids.....similarly i have done for 6 cycles...now i want to make it to 1 cycle can you please help me......thank you very very much dude..i really need your help...

dglienna
June 14th, 2009, 04:36 PM
And, what is the loop actually DOING?

johnny123
June 14th, 2009, 04:44 PM
That loop considers 5 macids and it connects to sql database..the database is having stored procedures and tables... actually the stored procedure compares the signal strengths given to this 5 macids from .net to the surveyed values which are in database and returns the probability value and a position value back to .net. this is a tracking application .so, after it returns the position value i need to show the position in picturebox. similary the code calculates 6 positions each time excluding one macid and finally i consider average of all 6 positions and the final position will be shown on the gui...

dglienna
June 14th, 2009, 06:03 PM
Still not sure about the procedure, but you could try LINQ-to-SQL

http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx

johnny123
June 14th, 2009, 10:27 PM
I have seen the links but could not get it..please help me in this program...I CAN XPLAIN THE CODE AS YOU WANT ...BUT PLEASE DONT NEGLECT...i am not at all getting an idea..i need your help....thank you so much...

dglienna
June 16th, 2009, 01:09 AM
I usually Refactor code to improve performance, but I'd need the whole app and db, which probably won't happen.

Have you tried Refactor?

johnny123
June 16th, 2009, 11:07 AM
No, i havnt tried the refactor.. can you please help me ....

dglienna
June 16th, 2009, 09:05 PM
I don't have your code. I don't have your db (or empty tables except for 1 or 2 records)

Put in a few Debug.Print statements, and print out what is happening for each loop, and then it'd be possible to analyze. Even easier if you can STEP THRU the program (pressing F8) and hovering over a variable to see it's value.

Add to that the fact that I have Vista 64bit, which doesn't really allow Edit and Continue after a break, and you'll see what a task this would be

ComITSolutions
July 11th, 2009, 09:58 AM
,


Here is the code....it is very big code...actually the first 20 lines repeats next 5 loops with different variables thats it..i want to make it into 1 single loop..i dont know how to do it...i will explain the code...



It is not the no of lines in the loop which slows down the process, what you perform in those lines, decides it. You are Hitting database 1500 times calling stored procedure, surely it will reduce the Performance!!.