Performance Discussion

B

Bobbo

Hi,

A colleague and I were having a weasely discussion about
which of the following methods is quicker...


Method 1:

var = False
If <cond> Then var = True



Method 2:

If <cond> Then
var = True
Else
var = False
End If


The first is more concise, yet the second is clearer.

Does anyone have anything to contribute to our debate, to
save us taking it outside?!? ;-)
 
B

Bob Barrows

You're right: "weasely" is a good term. I doubt a user will ever detect the
difference between these two methods. In a looping situation, I might use
the first method. In any other situation, it will not matter. Use whichever
suits your style.

YMMV,
Bob Barrows
 
B

Bobbo

For those interested, in a 100 million iteration loop,
Method 1 was the quickest (at approx 90% the time of
Method 2).

Incidentally, VB's IIF function was 10 times *slower* than
either of these two!

Right, I've finished being weasely now!

B.
 
R

Ray at

I don't really think this is what he was looking for though, hence the ":p"
thing. Like, he was probably just using t/f as an example. My guess is
that he was wondering between something more like:


var = "Joe Jones"
If <cond> var = "Mary Jones"

vs.

If <cond> Then
var = "Mary Jones"
Else
var = "Joe Jones"
End If

In that case, I'd probably bet a dollar on the top method.

Ray at work
 
C

Chris Hohmann

Bobbo said:
For those interested, in a 100 million iteration loop,
Method 1 was the quickest (at approx 90% the time of
Method 2).

Incidentally, VB's IIF function was 10 times *slower* than
either of these two!

Right, I've finished being weasely now!

B.

Here are my findings:

Method 1: If
Method 2: If..Else
Method 3:Let

True
Method 1: 1.371094
Method 2: 0.9726563
Method 3: 0.7382813

False
Method 1: 0.9101563
Method 2: 0.921875
Method 3: 0.7304688

Mod
Method 1: 2.003906
Method 2: 1.792969
Method 3: 1.191406

I tested three case. Condition false, condition true and condition =
iterator mod 2. The mod 2 condition is important since it covers the
boolean space. In all three cases, Ray's let method was the best
performer. When the condition was true or mod 2, method 2 (if..else)
beat out method 1 (if). Only when the condition was false did method 1
beat out method 2 and then only by a small margin. So if I had to rank
them it would be method 3 (let), method 2 (if..else), method 1 (if).

HTH
-Chris
 
M

Mark Schupp

Actually the 1st structure would be doing 2 string assignments if the
condition is true where the 2nd structure always does a single string
assignment and should be faster overall. That said, I often use the 1st
structure to reduce the nesting level of complex if statements.

---1.a---
var = "Joe Jones"
If False Then var = "Mary Jones"
1,000,000 tries =>3023 ms

---2.a---
If False Then
var = "Mary Jones"
Else
var = "Joe Jones"
End If
1,000,000 tries =>3004 ms

---1.b---
var = "Joe Jones"
If True Then var = "Mary Jones"
1,000,000 tries =>4547 ms

---2.b---
If True Then
var = "Mary Jones"
Else
var = "Joe Jones"
End If
1,000,000 tries =>3145 ms


--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17
 
R

Ray at

You are correct!

I ran through each method 10 million times, and got these results:

Method 1:
59 seconds

Method 2:
42 seconds
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top