insane VB efficiency

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
I hate VB. Let's get that straight. I prefer C++ on a Linux/Unix platform.

VB usually shouldn't be in the "highly technical" section, but read on. I'm forced to use VB for a college course I'm taking. (The Microsoft addicts at my university refuse to use open source software.)

I'm writing a text editor. This is code that will go in txtEdit_Change() to note that changes were made. A form-level boolean will be used later to either ask the user if he/she wants to quit without saving or to let him/her go on their way.

Which statement is more efficient?

'Statement A
If (blnChangesSaved = True) Then blnChangesSaved = False

'Statement B
blnChangesSaved=False


Namely, is a test on a boolean variable faster than setting a boolean variable? Yeah, this almost doesn't matter, but I'm obsessed with efficiency. Note that this boolean will normally be false about 99% of the time.

We DO have a programming forum, you know.

AnandTech Moderator
 

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
I knew all I needed was a break from coding to figure this out. If it's true 1% of the time, then it will have to both test and set it 99% of the time, or it can just set it. To whoever voted, thanks. Moderators, close this if you wish.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Ack, there is actually university courses in VB now?!?!?!??!???!?!

Gag, puke, gag, etc. -- VB is the worst thing to happen to computers ever. It's bad enough M$ marketing pulled the wool over businesses eyes and got them to believe its "easier", and then tried to force it down people's throats some more with ASP, but the idea its now being encouraged in schools is downright scary.

I voted for set without test is definately faster. No branch = no chance of branch mispredict. Setting one bool to another is pretty much atomic, and is almost definately in fact faster than branching over the set even in the case the test condition is false. Also, not doing the check makes your code smaller.
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Here is my thinking on it, coming off of a CompSci3 course in C++: to test then set, you have two operations happening: your test boolean is compared against true; then, your other variable is set to false. A boolean should be one bit (But, this is MS and VB, so it has two strikes against it, who knows it could be several kilobytes :) ), so retrieving it is basically the fastest thing a computer can do. Likewise, changing the value stored in a boolean should be lightning-fast.

Unless you're in a very tight memory situation (And I mean tight!), read and copy should run 1:1 in terms of speed because the variables they operate on would both reside in RAM. So, you've got two operations each taking time n to run, or in big-O notation it's O(2n). OTOH, there is your set it fragment where there is only one operation occuring. Hence, it would be O(n).

Cliffs notes: set it should be twice as fast as test then set, BUT we're talking about nanoseconds here.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Make sure that you declare the flag as a Boolean of course. If the type isn't declared it defaults to variant, and every VB access to a variant is a call to MSVBVM60.DLL, even for "native" code.