All Forums >> [Scripting] >> WSH & Client Side VBScript >> RE: Multiple "If - End If" question Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
I prefer case over multiple Ifs just because I thinik it is easier to read. In general I try to avoid either and solve complex logic constructs through creative data structures.
What I found interesting about this thread is the assertion that If...Then is faster than Select Case. Now, for the last 3 years, I have been hearing that Select Case is faster, as the expression only has to be evaluated once, whereas in If...Then statements, the expression has to be evaluated for each 'Else' or 'ElseIf'. So, after this thread made its way to my greedy little eyes, I decided to do some benchmarking. Whee!
Well, I was stunned! Everything that I had been told was a LIE! Well, at least about select case vs. If...Then!!
_____________________________
"Would you like to touch my monkey?" - Dieter (Mike Meyers)
"It is better to die like a tiger, than to live like a pussy." -Master Wong, from Balls of Fury
I have some problems with this code, please explain:
quote:
end2 = Timer - start1
Wouldn't you want to subtract the time from start2 rather than start1?
I did a little rewriting and came up with this:
with these results: If...Then takes 0.15625 whereas Select Case takes 0.125 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.125 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.109375 to do the same work. If...Then takes 0.140625 whereas Select Case takes 0.125 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.109375 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.125 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.109375 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.125 to do the same work. If...Then takes 0.15625 whereas Select Case takes 0.125 to do the same work. If...Then takes 0.171875 whereas Select Case takes 0.125 to do the same work.
Of course each person trusts his/her own benchmarks. Using my benchmark code from the posting to the "array/function" problem (http://www.visualbasicscript.com/m_30061/tm.htm)
### Xplore #################################################################### === VBSArrayB: VBScript Arrays Bench ========================================== ----- 01 tElseIf ----- P: ( 10000000, a ) Ok. 00:00:06 for 01 tElseIf ----- 02 tElseIf ----- P: ( 10000000, z ) Ok. 00:00:20 for 02 tElseIf ----- 01 tSelect ----- P: ( 10000000, a ) Ok. 00:00:07 for 01 tSelect ----- 02 tSelect ----- P: ( 10000000, z ) Ok. 00:00:16 for 02 tSelect === VBSArrayB: 0 done (00:00:49) ============================================== ### Xplore erfolgreich beendet ################################################
In earnest - I don't doubt your results - I got
### Xplore #################################################################### === IfSelB: if/select benchmark =============================================== If...Then takes 60,00% of the time Select Case takes to do the same work. If...Then takes 50,00% of the time Select Case takes to do the same work. If...Then takes 60,00% of the time Select Case takes to do the same work. If...Then takes 60,00% of the time Select Case takes to do the same work. If...Then takes 50,00% of the time Select Case takes to do the same work. If...Then takes 60,00% of the time Select Case takes to do the same work. If...Then takes 60,00% of the time Select Case takes to do the same work. If...Then takes 50,00% of the time Select Case takes to do the same work. If...Then takes 50,00% of the time Select Case takes to do the same work. If...Then takes 50,00% of the time Select Case takes to do the same work. === IfSelB: 0 done (00:00:01) ================================================= ### Xplore erfolgreich beendet ################################################
from your code - but I really would like an explanation.
Ah - I didn't read kirrilian's contribution before coding - this variable name blunder could have been one of my own. But a more general caveat: the timer docs state explicitly:
Returns the number of seconds that have elapsed since 12:00 AM (midnight).
so you should plan your benchmarks for longer times (and be afraid of timing the loop overhead instead of the functions you're interested in).
This gives me 209% (4.82 sec) for End...If and 100% (2.31 sec) for Select Case... But if b=1 (first case) then time is 99.2% (1.2 sec) for End...If and 100% (1.209 sec) for Select Case! Try yourself.
I have some problems with this code, please explain:
quote:
end2 = Timer - start1
Wouldn't you want to subtract the time from start2 rather than start1?
Grrrr... Yes. I had used start1 instead of start2. My screwup. I switched the number, and realized that Select Case is in truth faster than IF...Then. Glad someone was around to point out my error before I started rewriting all of my scripts! heheheheh
_____________________________
"Would you like to touch my monkey?" - Dieter (Mike Meyers)
"It is better to die like a tiger, than to live like a pussy." -Master Wong, from Balls of Fury
... However, if only one expression is Null, that expression is treated as a zero-length string ("") when concatenated with the other expression. ... I really don't like "however" in the docs of a programming language. Checking the "+" Docs: ... If one or both expressions are Null expressions, result is Null.. Then I knew that I had to use "+" to concatenate strings. I can't risk to let concatenations with unexpected Null values pass thru silently.
Recently, I had the issue that a WQL query could give a variable a string value or a Null. I was glad that I could use '&' to show a number of strings resulting from the query, without loosing the content due to one Null. So a Null does not always mean that an operation went wrong. Ok, lets say that that are not unexpected Null values.