Velocity Reviews > Performance Discussion

# Performance Discussion

Bobbo
Guest
Posts: n/a

 07-02-2003
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?!?

Bob Barrows
Guest
Posts: n/a

 07-02-2003
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

YMMV,
Bob Barrows

Bobbo wrote:
> 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?!?

Ray at
Guest
Posts: n/a

 07-02-2003

var = <cond>

Ray at work

"Bobbo" <(E-Mail Removed)> wrote in message
news:099c01c3409c\$85593e00\$(E-Mail Removed)...
> 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?!?

Bobbo
Guest
Posts: n/a

 07-02-2003
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.

Ray at
Guest
Posts: n/a

 07-02-2003
I don't really think this is what he was looking for though, hence the ""
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

"Dave Anderson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Ray at <%=sLocation%>" wrote:
> >
> >
> > var = <cond>

>
> Amen.
>
>
>
> --
> Dave Anderson
>
> Unsolicited commercial email will be read at a cost of \$500 per message.

Use
> of this email address implies consent to these terms. Please do not

contact
> me directly or ask me to contact you directly for assistance. If your
> question is worth asking, it's worth posting.
>
>

Chris Hohmann
Guest
Posts: n/a

 07-02-2003
"Bobbo" <(E-Mail Removed)> wrote in message
news:0abe01c340a8\$ee6a8eb0\$(E-Mail Removed)...
> 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

Mark Schupp
Guest
Posts: n/a

 07-02-2003
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
--
Integrity eLearning
Online Learning Solutions Provider
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.ielearning.com
714.637.9480 x17

"Ray at <%=sLocation%>" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I don't really think this is what he was looking for though, hence the

""
> 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
>
> "Dave Anderson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "Ray at <%=sLocation%>" wrote:
> > >
> > >
> > > var = <cond>

> >
> > Amen.
> >
> >
> >
> > --
> > Dave Anderson
> >
> > Unsolicited commercial email will be read at a cost of \$500 per message.

> Use
> > of this email address implies consent to these terms. Please do not

> contact
> > me directly or ask me to contact you directly for assistance. If your
> > question is worth asking, it's worth posting.
> >
> >

>
>

Ray at
Guest
Posts: n/a

 07-03-2003
You are correct!

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

Method 1:
59 seconds

Method 2:
42 seconds

"Mark Schupp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> --
> Integrity eLearning
> Online Learning Solutions Provider
> (E-Mail Removed)
> http://www.ielearning.com
> 714.637.9480 x17
>
>
> "Ray at <%=sLocation%>" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I don't really think this is what he was looking for though, hence the

> ""
> > 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
> >
> > "Dave Anderson" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > "Ray at <%=sLocation%>" wrote:
> > > >
> > > > How about:
> > > >
> > > > var = <cond>
> > >
> > > Amen.
> > >
> > >
> > >
> > > --
> > > Dave Anderson
> > >
> > > Unsolicited commercial email will be read at a cost of \$500 per

message.
> > Use
> > > of this email address implies consent to these terms. Please do not

> > contact
> > > me directly or ask me to contact you directly for assistance. If your
> > > question is worth asking, it's worth posting.
> > >
> > >

> >
> >

>
>