Johnson Jason
Johnson Jason

Reputation: 721

Repeatedly checking entire scripting dictionary via loop bad practice? VB6

I have a loop that checks for values and adds them to a dictionary if they do not exist. This check happens inside of a loop so it happens repeatedly. As the dictionary grows I imagine there is an inner loop going on with each check that is becoming costly. In an effort to speed up my entire routine I am wondering if this might be something I should optimize? If so, how?

  keycnt = 1
  For x = 1 to 500000
  STRVAL = returnarray(8, x)
            If Not STRDIC.Exists(STRVAL) Then
               STRDIC.Add STRVAL, keycnt
               keycnt = keycnt + 1
            End If
  next x

Upvotes: 2

Views: 69

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71177

If your dictionary is a Scripting.Dictionary and not some [poorly implemented] custom-made data structure class, then a key lookup should be O(1) complexity, not O(n) as you seem to imply; the growing number of keys has no impact on performance.

Key lookup in a hash table or dictionary is basically free, the only thing to fix in that code is the indentation.

Upvotes: 4

Related Questions