Click to See Complete Forum and Search --> : Improving Performance


Hari Prasad
July 31st, 2001, 12:39 AM
HI,
Does the size of a function(number of lines)or procedure affect the performace of the program. i mean the speed of execution etc. Has anybody have any document or suggestion on this.

Regards
Hari

Cakkie
July 31st, 2001, 01:32 AM
It's not really the size that matters (hmm, where did I hear this before). At least, not when compiled. In the VB IDE, your program might run slower when it has more lines, but compiled, it doesn't.
An example

' this
If a = 1 then a = 2 else a = 3
' will run as fast as
If a = 1 then
a = 2
else
a = 3
End If



Why? Simple, you are doing the same. On the other hand:

' this
a = a + 1
a = a + 1
' will be slower as
a = a + 2



' why? You're not doing the same. Ok, the result will be that in both cases a will be 2, but the way you do it is different.
Again:

Dim a as integer, b as integer, c as integer, d as integer
' runs as fast as
Dim a as integer
Dim b as integer
Dim c as integer
Dim d as integer
' and runs faster then
Dim a, b, c, d as integer



Why? There must be allocated 4 spaces in memory, and even in the first statement, they're all at the same line, they will be processed seperately.
The last statement is slower, because a, b and c don't have a type, and will be variant, which takes up more space (time) in memory.

As always, it is up to you to keep your code as short as possible, yet as readable as possible. The key thing is to keep the heavy things as short as possible, and as efficient as possible.

Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook