Something Interesting: If or Else?

ReAn

Dangerous Programmer...
Local time
Today, 10:34
Joined
Jun 25, 2004
Messages
250
How many of you have been programming and needed a simple two way conditional aka if blah = blah then ..... else ..... end if, where one way does some less relavant code, and sometimes wondering how you should structure you're logic (aka, where relavant code should be...)

I recently conducted a series of tests to determine which was more efficient, the If clause, or the else clause.

I did a repition test on Both If and on else and noticed that when simple assignments were made

aka

z = 1

The If clause was faster, and when calculations were involved (exact same calculation)

z = z + 1

the if clause was still faster. Repition scale was 100000000, and If was about 3 seconds faster than Else (7-10 second range)

Just an interesting snipit testing the logical code design behind VBA, for those of you interested.

Edit in italic text
 
Last edited:
Also did another test, turns out <> is ever so slightly faster than =
On a repitition of 100000000 <> was about 0.4 seconds faster than =

We're dealing in the 7-10 second range here btw, so that's 4%-5% faster.
 
The one that will open your eyes is doing a SELECT CASE construct.

The fastest case is the first one, followed by the second one, followed by the third one, etc... followed by the CASE ELSE.

This looks exactly like (and is implemented as) a "ladder" of If / ElseIf / ElseIf.... / Else statements, first case being the IF, subsequent cases being the ElseIf, and CASE ELSE being the Else clause.

But TECHNICALLY, you are measuring the difference between two code snippets where one snippet is

test (success, no branch); action; goto (place beyond end of ELSE clause)
and
test (fails, takes branch to beginning of ELSE clause); action; fall through to place beyond end of ELSE clause.

Assuming the actions to be the same, you are looking at the difference in machine times for a conditional and an unconditional branch. The test is the same for either case - it is the RESULT of the test that differs, and this is merely the yes/no setting of a condition code in the hardware flag register.
 
ReAn, I'm trying to follow what you put and I think I understand it but do you know which of these is faster:

Example 1)
If x <> y then
s = r
Else
s = t
end if

or

Example 2)
If x <> y then s = r
If x = y then s = t


Maybe it's poor practice but sometimes I've gotten into the habit of writing this like example 2 just to cut back on the number of lines. Is either one really preferable or doesn't it really matter for all practical purposes. :confused:
 
Probably #2, ill stress test it though, gimme a min...
 
Stress test:

Repititions: 100000000

Case: x dosent equal y

Example 1: 14.437 sec
Example 2: 20.0469 sec

Case: x equals y

Example 1: 12.531 sec
Example 2: 17.1499 sec

I suppose it's because example two has to make two conditional calculations, where as example 1 only has to make 1

Analogy for your understanding said:
Suppose you ask me if I am Female, in example one I would respond no, so you would automatically say, since there are only two choices, female and not female, then I must be male.

In example two you ask if i am female and I respond no. Then you ask if I am not female (male) and i respond yes.

see where im going?

Test Run with Access 2000 on:

CS Fire Monitor System Stats output said:
CPU Stats:

Speed - 2.59 GHz
Real Time Speed - 2,593.670 MHz

Vendor ID - GenuineIntel
Manufacturer - Intel
Name - Intel(R) Pentium(R) 4 CPU 2.60GHz
Type - Primary processor
Family - P4
Model - P4 (0.13 µm)
Brand - Unavailable
Stepping - 9
Logical - 1
ID - Unavailable
Serial Number - Unavailable

Memory Stats:

Total Memory - 1,021.984 MB
Used Memory - 570.797 MB
Free Memory - 451.188 MB
Percent Free - 44.1 %

Total Virtual Memory - 2.000 GB
Used Virtual Memory - 301.965 MB
Free Virtual Memory - 1.705 GB
Percent Free - 85.3 %

Max Page File - 2.407 GB
Used Page File - 531.305 MB
Free Page File - 1.889 GB
Percent Free - 78.4 %
 
Last edited:
That's interesting. I guess it doesn't necessarily pay to be lazy huh? ;)
Fortunately, the code that I usually run isn't that intense. I've also been trying to take a lot of people's suggestions and try to run more and more stuff through saved queries rather than code. Cheers! :)
 

Users who are viewing this thread

Back
Top Bottom