I have a loop in my program that iterates at the start about 20,000 - 25,000 times per second. By the end of the loop, the iteration is down to about 6000 per second.
Here is my code.
AnagramQueue and FoundAnagrams are List(Of String).Code:Sub FindAnagrams(ByVal s As String) ' Remove the string we are currently using from the queue If Not AnagramQueue.Count = 0 Then AnagramQueue.RemoveAt(0) ' Loop through each character in the string For n As Integer = 0 To s.Length - 1 ' Since all Anagrams of a particular word have the same exact letters, ' alphabetizing them will help us weed out the duplicates. AlphaString = Alphabetize(s) ' This loop lops of the nth letter and adds the rest of the string to our lists. While AlphaString.Length > 0 NumPathsSearched += 1 ' If the anagram hasn't been found yet, add it. If Not FoundAnagrams.Contains(AlphaString) Then AnagramQueue.Add(AlphaString) FoundAnagrams.Add(AlphaString) UpdateAnagramUI() End If ' Prevent an OutOfRange exception If n >= AlphaString.Length Then Exit While Else AlphaString = AlphaString.Remove(n, 1) End If End While ' Recurse While AnagramQueue.Count > 0 FindAnagrams(AnagramQueue(0)) End While Next End Sub
The Alphabetize function merely returns the string with it's Chars in abc order.
Can you see any bottlenecks or any reasons for the slowdown?




Reply With Quote