VBS Core Question

Which hub software is the best? Where can I find script XXX? Discuss it here...(no, this is not for advertising your hub...)

Moderator: Moderators

ButterflySoul
Posts: 210
Joined: 2003-01-23 22:24
Location: Nevada

VBS Core Question

Post by ButterflySoul » 2003-03-27 22:45

I've got a question about the VBS engine itself. It's for some code in DataArival, so I'm trying to get it as efficient as possible, considering how often that event is trigered =)

Let's say that I have :

Code: Select all

Select Case Right(sString, 1)
   Case "1" : <do stuff>
   Case "2" : <do stuff>
   Case "3" : <do stuff>
   Case "4" : <do stuff>
End Select


Assuming that "Right(sString, 1)" equals 5, will the VBS Engine evaluate "Right(sString, 1)", check if the result is 1, then "go back to the top" so to speak and evaluate Right(sString, 1) again, check if the result is 2, then evaluate it again before checking if it's 3, etc etc ?
Or will it instead evaluate "Right(sString, 1)" once and for all, "remember" the result, and match it against the cases ?

If it was for any other Sub, I'd just Dim a variable, put the result of "Right(sString, 1)" in it, and make the match against the variable instead, but since it's for DataArival, I thought it was worth trying to get it as performant as possible.
If no one knows, it's probably what I'll do anyway, but I thought I'd ask first... Anything to save a few CPU cycles and bytes of memory =p

(and no, in my actual script, I'm not matching anything against the first character from the right of a string in DataArival, it was just to keep the question as simple as possible =)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 13:31
Location: Canada

Post by TasMan » 2003-03-27 23:59

Ummm after doing a test, I've found that doing the save-to-variable is slightly faster.

In a test of 50000 loops with 10 choices (all wrong btw), the variable one was about 0.04 seconds faster. Mind you, this was with VB, but they act in much the same way. Needless to say, the performance gain is minimal, unless it's executed more times than I just did :P .

Btw it would be faster to do this, even if you said it doesnt matter :)

Code: Select all

  Select Case AscW(RightB(sString, 2))
    Case 49 '  "1"
    Case 50 '  "2"
    Case 51 '  "3"
    Case 52 '  "4"
  End Select

HaArD
Posts: 147
Joined: 2003-01-04 07:20
Location: Canada http://hub-link.sf.net

Post by HaArD » 2003-03-28 00:45

If there is even a remote possibility that "Right(sString, 1)" equals 5 you should add

Code: Select all

Case Else :  do stuff 

ButterflySoul
Posts: 210
Joined: 2003-01-23 22:24
Location: Nevada

Post by ButterflySoul » 2003-03-28 01:59

In a test of 50000 loops with 10 choices (all wrong btw), the variable one was about 0.04 seconds faster. Mind you, this was with VB, but they act in much the same way. Needless to say, the performance gain is minimal, unless it's executed more times than I just did


Or unless you're using something more complex than Right(sString, 1) :wink:
Thankie !
*goes off to dim an ugly memory eating variant without remorse now* =p

-----

If there is even a remote possibility that "Right(sString, 1)" equals 5 you should add

Code:
Case Else : do stuff


Actually, if it's anything else than the cases I match against I don't want anything special to be done. I simply want the Sub to continue flowing after the "End Select" and to finish as fast as possible. So I don't have a "Case Else", but it's on purpose.
Thx for mentioning it, though =)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 13:31
Location: Canada

Post by TasMan » 2003-03-28 02:05

Oops....I just realized that the 0.04 seconds was on the extra-variable one. It was probably caused by the variable assignment.

So it calculates it once!

(Might answer more when I wake up....)

ButterflySoul
Posts: 210
Joined: 2003-01-23 22:24
Location: Nevada

Post by ButterflySoul » 2003-03-28 06:19

*promptly removes the evil variant costing 0.04 seconds per call of the DataArival Sub (if it's due to the assignement) *

Thx for the update, and sweet dreams =)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

Who is online

Users browsing this forum: Google [Bot] and 0 guests