Showing category "Efficiency" (Show all posts)

Disposing and GC

Posted by Jessie Burns on Saturday, January 29, 2011, In : Efficiency 
Okay, I know that no one really knows or understands the GC.  I still want to touch on it briefly here.
Some people tend to go too far when trying to prevent or cleanup memory leaks.  If you remember 2 simple rules, it will save you a lot of headache.
1. Dispose of it when your done with it.  The Using statement is my favorite way to accomplish this.
2. Make sure any pointers back to it are set to Nothing.
Do this and the GC will do it's job and you wont have to over-engineer destroying eve...
Continue reading ...
 

Index Loops vs For Each

Posted by Jessie Burns on Saturday, January 29, 2011, In : Efficiency 
For Each loops are bad, mmmKay?
I avoid them for 2 main reasons:

1. Index loops are faster.
2. You cant modify a list inside a For Each loop.

I'll explain.
This will fail at the remove call:
For Each str As String In lstStr
     'test for something
     If str = "boosh"
          lstStr.Remove(str)
     End If
Loop
It fails because you have changed the list count and therefor the index.

This however, will work:
For i As Integer = lstStr.Count -1 To 0 Step -1
     'test for something
     If lstSt...

Continue reading ...
 
 

Categories

Recent Posts