|
-
Jul 17th, 2000, 04:42 AM
#1
Thread Starter
Fanatic Member
Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...
-
Jul 17th, 2000, 04:46 AM
#2
Hyperactive Member
Not much advantage
It's just less code and simpler to follow, that's all.
Signed, Rodik ([email protected])
Programmer,usesVB6ED
===========================
Copyright©RodikCo,2002.
Dont mind this signature ;] Its old
-
Jul 17th, 2000, 04:47 AM
#3
Thread Starter
Fanatic Member
Thanks Rodik, an honest answer I feel...
-
Jul 18th, 2000, 04:19 AM
#4
Well ....
Actually, yes, it is a less code and less headache thing. But you need to know when to use what. Whenever you want to choose, if you have two things to choose from, use 'if'. If you have more than 3 things to choose from, use 'case'. If you have 3 things, (take one and pass on the remaining to me!) you can use either 'if' or 'case', whatever you like.
The important point is 'two is a company, three is a crowd' applies to the 'if' statement. Whenever the number of choices increases, 'if' will become complicated to follow.
-
Jul 18th, 2000, 07:44 AM
#5
Fanatic Member
In Fact VB take less time to translate a CASE than IF
-
Jul 18th, 2000, 11:06 AM
#6
Hyperactive Member
Just note that large case statement will be faster than an if statement. If you use multiple if statements, it has to check each one before continuing on to the next. If you use one large case statement, it automatically matches a case and goes right to it, doesn't have to check all the other cases.
-
Jul 18th, 2000, 12:23 PM
#7
PowerPoster
So what about ElseIf? I made some performance tests and the Ifs were always faster than Select Case...
-
Jul 18th, 2000, 12:44 PM
#8
Monday Morning Lunatic
How many case/elseif statements did you use?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 18th, 2000, 12:49 PM
#9
Hyperactive Member
All I know is I had a function once, it was all If, and then I changed it to a Case, and it went much faster. I would like you to perform some tests, and have the results posted on my desk by twenty hundred hours.
-
Jul 18th, 2000, 01:15 PM
#10
Hyperactive Member
Paul - the aussie in Japan
Maybe you could get Paul - the aussie in Japan do some performance tests, he seems sharp and knows the workings of getTickCount.
-
Jul 18th, 2000, 01:30 PM
#11
Monday Morning Lunatic
What d'ya mean - workings? It's dead simple!
Code:
lBefore = GetTickCount
...
lElapsed = GetTickCount - lBefore
lElapsed has the time taken for ... to execute (in ms).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 18th, 2000, 01:55 PM
#12
Hyperactive Member
I didnt say it was difficult, I am just too dam lazy to do it, but I figure ya throw Paul a nice line, he might do it.
I am interested in knowing which is faster, but have no drive to do it myself.
-
Jul 18th, 2000, 02:20 PM
#13
Fanatic Member
Yeah, and to work out which one's faster you could use a Case or an If statement. 
By the way, is IIf slower then an If...ElseIf statement?
-
Jul 18th, 2000, 04:57 PM
#14
transcendental analytic
IIf is definitely slower than If...ElseIf
Also done tests and IF, elseif, elseif...End if is faster than select case.
For performance testing, if you use an loop with for next you will get missleading results, and that's what you need to do with gettickcount. For more exact results you could use querryperformace counter, which is accurate to about 1 microsecond, also dependent on hardware...
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 18th, 2000, 10:45 PM
#15
Fanatic Member
Code:
If InDoubt Then
Select Case of InDoubt
Case Something
Dummy="Hmmmmmmmmmmmm"
Case Else
DoEvents()
End Select
Else
DoEvents()
End If
Hmmmmmmmmmmmm,
Makes one stop and think...
DocZaf
{;->
-
Jul 19th, 2000, 01:30 AM
#16
Fanatic Member
Hi!, Spotted my name and thought I'd drop in.
I think we went through this a while ago and were all dissapointed to find out that heaps of elseif's were faster than a case statement, we were hoping to find that the case was faster because it looks cleaner.
In C the case statement is losts faster because it can skip to the bottom of the statement after it executes the True code, but C doesn't have an elseif statement, just else if (a new if statement after the else) which means losts of extra evaluation.
Personally I try never to use more than 1 or 2 elseifs in an if statement, but that's for readablilty, mostly the impact of using a case statement is so small that there are almost always better perfomance tunings that can be done.
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jul 19th, 2000, 07:14 AM
#17
Well ....
All talking about whether IF is faster or CASE is faster, where the time wasted (if any) in any method has to be measured in micro/mili/nano seconds (whichever is the smallest unit) ?????
Let me ask you all a simple question: You have to use either IF or CASE in your project. You have to make a choice from two options, what would you choose? And if you were to choose one from 15 options, what would you choose?
I feel you should also consider the debugging strain on the programmer. It should not be that you saved 16 miliseconds by using IF instead of CASE and lost 16 hours in debugging the nested IFs.
-
Jul 19th, 2000, 07:25 AM
#18
Frenzied Member
Hm 16 Millicsecond in the main loop would quite nice!
Sanity is a full time job
Puh das war harter Stoff!
-
Jul 19th, 2000, 07:29 AM
#19
Hyperactive Member
Told ya Paul would know...
Thx Paul
-
Jul 19th, 2000, 01:14 PM
#20
Fanatic Member
The speed difference is minimal, as in unless you need to loop it over 100,000 time, you'll not notice. Use which ever makes you're code more readable and understandable.
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jul 19th, 2000, 01:43 PM
#21
C++
Paul, regarding using if's in C++. You don't necessarily have to use else whenever you are using a lot of if's.
This would work fine as well.
Code:
if( Choice == '*' )
cout << "Multiplication";
if( Choice == '/' )
cout << "Division";
if( Choice == '+' )
cout << "Addition";
But, yes, I agree wih you on using a switch statement instead because you easily insert a break into it.
-
Jul 19th, 2000, 01:56 PM
#22
Monday Morning Lunatic
Although in many cases this wouldn't work because you need to avoid the possibility of satisfying more than one if statement.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 19th, 2000, 06:08 PM
#23
Gee this is real interesting
Paul is dead right, in any version of basic l have used vb/Universe/Pick etc etc. Continuos If statements are fractionally faster than Case statements, strange but true.
Even though each one of the IF statements has to be tested, the CASE statement does it in a sneaker way too
E.g
Code:
Select Case iValue
Case 1
Case 2
Case 3
Case 4
End Select
If iValue = 1
If iValue = 2
If iValue = 3
If iValue = 4
Now suppose iValue = 1, the first If condition is executed with each subsequent If being tested. With the Case statement, the first Case is executed followed by each line of code being tested for the End Select statement. Hence the apparent paradox nature of the speed test.
Of course with the If construction you would throw in an Exit Sub or whatever to by-pass the redundant testing.
Its up to you and your style of coding. Cause a number of people will end up modifying my code over time, l use case for large numbers of tests to allow for greater readability. Phantomd uses nested If statements, because he prefers them....don't trust a unix guy to do anything easy...
Just sit back and think...gee in a couple of years l might have to modify this code, would l be able to read it easily to make a change.
-
Oct 30th, 2001, 10:51 AM
#24
Fanatic Member
Case VS IF
Originally posted by coox
Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...
Not only code is easest for read, Computer only test var once
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|