Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Case VS. If Then

Status
Not open for further replies.

Keyster86

IS-IT--Management
Jul 23, 2008
50
US
I need help. I have looked up various websites; however, I am getting conflicting results. May someone please confirm or deny if Select Case statements are slower than If then-if ladders? I have a script that seams to be running slower now since I moved over to Select Case VS. If Then. Before I go running to re-write the script, I want to get some professional input.

Code:
[COLOR=green]'My current VBScript code:[/color]
[COLOR=blue]FOR EACH[/color] objFileSize [COLOR=blue]IN[/color] objSource.Files
	[COLOR=blue]SELECT CASE[/color] UCASE(strExt)
		[COLOR=blue]CASE[/color] ".MP3"
		[COLOR=blue]CASE[/color] ".WAV"
		[COLOR=blue]CASE[/color] ".GIF"
		[COLOR=blue]CASE[/color] ".MOV"
		[COLOR=blue]CASE[/color] ".WMV"
		[COLOR=blue]CASE[/color] ".WMA"
		[COLOR=blue]CASE[/color] ".AVI"
		[COLOR=blue]CASE[/color] ".FLV"
		[COLOR=blue]CASE[/color] ".EXE"
		[COLOR=blue]CASE[/color] ".JPG"
		[COLOR=blue]CASE ELSE[/color]
		     Do Something
	[COLOR=blue]END SELECT[/color]
[COLOR=blue]NEXT[/color]
SELECT CASE

VS.

IF THEN
Code:
[COLOR=blue]FOR EACH[/color] objFileSize [COLOR=blue]IN[/color] objSource.Files
   strExt = UCASE(strExt)
	[COLOR=blue]IF[/color] strExt = ".MP3" [COLOR=blue]THEN[/color] 
     [COLOR=blue]ELSEIF[/color] strExt = ".WAV" [COLOR=blue]THEN[/color]
      [COLOR=blue]ELSEIF[/color] strExt = ".GIF" [COLOR=blue]THEN[/color]
       [COLOR=blue]ELSEIF[/color] strExt = ".MOV" [COLOR=blue]THEN[/color]
        [COLOR=blue]ELSEIF[/color] strExt = ".WMV" [COLOR=blue]THEN[/color]
         [COLOR=blue]ELSEIF[/color] strExt = ".WMA" [COLOR=blue]THEN[/color]
          [COLOR=blue]ELSEIF[/color] strExt = ".AVI" [COLOR=blue]THEN[/color]
           [COLOR=blue]ELSEIF[/color] strExt = ".FLV" [COLOR=blue]THEN[/color]
            [COLOR=blue]ELSEIF[/color] strExt = ".EXE" [COLOR=blue]THEN[/color]
             [COLOR=blue]ELSEIF[/color] strExt = ".JPG" [COLOR=blue]THEN[/color]
    [COLOR=blue]ELSE[/color]
	    Do Something
             [COLOR=blue]END IF[/color]
            [COLOR=blue]END IF[/color]
           [COLOR=blue]END IF[/color]
          [COLOR=blue]END IF[/color]
         [COLOR=blue]END IF[/color]
        [COLOR=blue]END IF[/color]
       [COLOR=blue]END IF[/color]
      [COLOR=blue]END IF[/color]
     [COLOR=blue]END IF[/color]
    [COLOR=blue]END IF[/color]
[COLOR=blue]NEXT[/color]

V/r,

SPC Key
United States Army
 
Please disregard this post as I found out the error and cause for heavy lag in my script.

Thanks everyone!


V/r,

SPC Key
United States Army
 
Try comma separating your options, since you are doing the same action for all of them.

Code:
FOR EACH objFileSize IN objSource.Files
    SELECT CASE UCASE(strExt)
        CASE ".MP3",".WAV",".GIF",".MOV",".WMV",".WMA",".AVI",".FLV",".EXE",".JPG"
        CASE ELSE
             Do Something
    END SELECT
Next


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Please disregard this post as I found out the error and cause for heavy lag in my script.

If you found your problem, it would be most helpful to us if you would state the solution. We (and future readers who may have the same question) should be able to see what the fix was and not be left wondering.

As for the If ... ElseIf ... Else ... End If code, I believe you had too many End If statements.

Code:
 IF strExt = ".MP3" THEN 
 ELSEIF strExt = ".WAV" THEN
 ELSEIF strExt = ".GIF" THEN
 ELSEIF strExt = ".MOV" THEN
 ELSEIF strExt = ".WMV" THEN
 ELSEIF strExt = ".WMA" THEN
 ELSEIF strExt = ".AVI" THEN
 ELSEIF strExt = ".FLV" THEN
 ELSEIF strExt = ".EXE" THEN
 ELSEIF strExt = ".JPG" THEN
 ELSE
    Do Something
 END IF
 
dbMark said:
If you found your problem, it would be most helpful to us if you would state the solution. We (and future readers who may have the same question) should be able to see what the fix was and not be left wondering.

As for the If ... ElseIf ... Else ... End If code, I believe you had too many End If statements.

Code:
IF strExt = ".MP3" THEN
 ELSEIF strExt = ".WAV" THEN
 ELSEIF strExt = ".GIF" THEN
 ELSEIF strExt = ".MOV" THEN
 ELSEIF strExt = ".WMV" THEN
 ELSEIF strExt = ".WMA" THEN
 ELSEIF strExt = ".AVI" THEN
 ELSEIF strExt = ".FLV" THEN
 ELSEIF strExt = ".EXE" THEN
 ELSEIF strExt = ".JPG" THEN
 ELSE
    Do Something
 END IF

Sure, I can't give you code really; however, I can tell you that my other coding unrelated to this post was causing the script to count the total size of all files when processing each file (slowing the script down). I did not state the solution to my problem in the initial "disregard" post as it was unrelated to my question here.

So for other programmers who might have a similar problem to this post, I would say check your other code and see what is lagging the script (versus Select Case versus If Then statements) because far as I can tell there is little to no difference in speeds between the two.

Hope this helps everyone.

~~ Thread Closed ~~

V/r,

SPC Key
United States Army
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top