Sign in to follow this  
Followers 0
d0n

For loop vs Select case

2 posts in this topic

So i was wondering which one is faster if i want to do something like this:

Check a bunch of string with StringRegExp, would it be faster to

1) Use for loop and loop till i get the correct string then exitloop

OR

2) using Select Case to filter through to find the correct string

For sure using the for loop will produce a much cleaner code, however i am interested in which one provides better speed

thanks for help

Share this post


Link to post
Share on other sites



You can find that by yourself - just time the execution for both and see which is faster.

Faster or slower depends on what code you have inside these loops.

You can see what difference it makes when you comment/uncomment $ttt=1 in the For/Next loop.

$str = ""
For $i = 1 to 100000
    $str &= Chr(Random(97, 121, 1))
Next
$str &= "z"
$CharARR = StringSplit($str, "")

$forTIME = TimerInit()
For $i = 1 to $CharARR[0]
    ;$ttt = 1
    If $CharARR[$i] = "z" Then ExitLoop
Next
ConsoleWrite("For: "&TimerDiff($forTIME)&@CRLF)

$i = 1
$SelectTIME = TimerInit()
Do
    $i += 1
Until $CharARR[$i] = "z"
ConsoleWrite("Do: "&TimerDiff($SelectTIME)&@CRLF)

So - to answer your question: it depends on the code you want to use - test and see.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0