Speed up a program

Gopher2011

Well-known member
Joined
Mar 3, 2011
Messages
111
Location
South England
Programming Experience
10+
Can anyone look at this code and see how i can speed it up?
Its calling a sub to count commas in long string.

Calling part
VB.NET:
[COLOR=#222222][FONT=Times New Roman]                    'Sort the incomming messages[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]                    Dim count_Coma As Integer = CountCharacter(_InMessToAction, ","c)[/FONT][/COLOR]


The sub. This takes about 0.7 of a second to count 20 commas in string _InMessToAction. Was hoping to get it down to 0.1... if at all possible..
example string = _InMessToAction = "0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000,0000"

VB.NET:
[COLOR=#222222][FONT=Times New Roman] Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]        Dim cnt As Integer = 0[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]        For Each c As Char In value[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]            Debug.Write(".")[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]            If c = ch Then cnt += 1[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]        Next[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]        Return cnt[/FONT][/COLOR]
[COLOR=#222222][FONT=Times New Roman]    End Function[/FONT][/COLOR]
 
While my measurement using that method reports around 100 milliseconds for a single call, the measurement is significantly affected by the calls to Debug.Write, without it the operation is hardly measurable in milliseconds.

Doing a loop using String.IndexOf method will be around 25% faster also.
 
Run a loop with IndexOf, utilising the StartingIndex overload to check for more occurrences would be my guess :D
 
Try to start searching the commas from value(0) to middle of the string and value(str.length-1) back to middle by using one for loop. Sorry i did'nt test this because I don't know where I can find the total second after running this.heheheh, Just trying to help.
 
Back
Top