0

What's the difference between this code snippet:

Sub Test()
  Dim i as Integer = 0
  Dim q as String = ""
  While i<10
    q=String.Format("Some text {0}", i)
    Console.WriteLn(q)
    i=i+1
  End While
End Dub

and this one:

Sub Test()
  Dim i as Integer = 0
  While i<10
    Dim q as String = String.Format("Some text {0}", i)
    Console.WriteLn(q)
    i=i+1
  End While
End Dub

in terms of memory allocation and performance ...

  • 3
    Have you tried profiling it? – user40980 Apr 20 '15 at 16:48
  • Don't you love it when people downvote without an explanation? – Mike Nakis Apr 20 '15 at 20:08
5

In terms of memory allocation and performance, the first snippet contains a superfluous assignment of the empty string to q before entering the loop.

From a performance point of view this should not be of any concern whatsoever, but from a best software engineering practices point of view, the second snippet is definitely preferable, since the variable is declared within the scope in which it is used.

Readability and maintainability are far more important than performance in the vast majority of scenarios out there, and statistically speaking, the chances that your business is an exception to this rule are slim.

My recommendation would be to go ahead and build your software product properly, and if it turns out that there is a performance problem, (most probably there won't be any,) then throw the profiler at it to see precisely where the problem is, and once you have found the problem spots, then go ahead and improve only those spots that need improvement.

Being concerned about the performance of every tiny little thing all over the place, and tweaking and hacking things proactively here and there just in order to squeeze clock cycles blindly, without having any idea whether these clock cycles are actually needed, is no way of writing software.

Clock cycles will always be wasted. Even if you abandoned Visual Basic in favour of Assembly Language you would soon discover that it is impossible not to waste clock cycles. The point is to only worry about clock cycles when you have a very good reason to do so, and in the vast majority of cases, you don't have any. Poorly performing software performs poorly mostly due to algorithmic problems, not due to programmers being careless with clock cycles.

P.S.

I don't know about Visual Basic, but in most decent programming languages the compiler is capable of warning you if you attempt to use a variable which has not been initialized yet, but this ability of the compiler is compromised by the very prevalent and very misguided practice of initializing variables to nonsensical defaults like Dim q as String = "" above. So, I would recommend avoiding Dim q as String = "" like the plague.

  • +1, especially for advice to avoid short-circuiting compiler warnings. – Nathan Tuggy Apr 20 '15 at 20:42
0

In the first example, I would assume that the variable has been declared in advance to avoid garbage collection issues, except there are only 10 iterations, and if you don't have enough ram to handle that, then you have a bigger issue on your hands than memory management. This might be useful to declare a variable only once, if that object takes up a lot of memory, but forcing a garbage collection may still be necessary, as the garbage collector doesn't instantly collect when the reference to an object goes out of scope. In general I try to keep declarations within the scope they are used, but sometimes there could be a reason to pre define your variables. Just think about why you are doing it, and run a benchmark to see if it helps improve your performance. Use whichever one works better for your situation, if both work the same, try to keep things declared within the scope they are used.

-2

They are the same in terms of performance. In VB.NET, a variable that is declared inside a loop keeps its value for the next itaration.

I would prefer the frist example as it is easier to read and probably has the intended behavior.

Here's the output of the two:

> Some text 0 
> Some text 1 
> Some text 2 
> Some text 3 
> Some text 4 
> Some text 5 
> Some text 6 
> Some text 7 
> Some text 8 
> Some text 9
> 
> Some text 0 
> Some text 1 
> Some text 2 
> Some text 3 
> Some text 4 
> Some text 5 
> Some text 6 
> Some text 7 
> Some text 8 
> Some text 9
> 
  • 1
    Why should not the second one reset the value on every iteration? A Dim which assigns a value is equivalent to a hoisted value-less Dim combined with an assignment statement at the point of where the Dim appeared in the original source. – supercat Apr 20 '15 at 18:50
  • @supercat - It was a design decision...social.msdn.microsoft.com/Forums/en-US/… – Jon Raynor Apr 20 '15 at 19:17
  • 2
    The linked example uses the form of Dim statement that doesn't specify a value. – supercat Apr 20 '15 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.