PDA

View Full Version : Case Statements Vs. If Then Statements



captainproton
16-07-2007, 08:06 PM
Does anybody know which is better to use, performance wise?

For example, a simple checkbox can be evaluated in two ways;

If Checkbox1.checked then Vtemp := 100
else VTemp := 0;

or

Case Checkbox1.checked of
True: VTemp:=100;
False: VTemp:=0;
End;

My query is related to the speed at which the two work. I read somewhere that the compiler will optimise the "case" better than the "If then..". Can anyone confirm that with actual experience?

Craig

technomage
16-07-2007, 08:20 PM
HI Craig

This topic may have your answers

http://www.pascalgamedevelopment.com/viewtopic.php?t=4283&highlight=case

captainproton
16-07-2007, 08:33 PM
Yep, that answers my question. In fact I did read that topic some time ago but as my memory is more like my programming (full of holes) I had forgotten about it.

Someone said on that thread "great question" which is typical for me. I always come up with the cool stuff after someone else has thought of it first. Imagine my disgust when I invented the "Wheel" only to find out it had been done before.

Thanks for pointing me in the right direction. For my purposes CASE seems to be best.

Craig

dmantione
16-07-2007, 08:54 PM
Well, if you just use true/false, an if might be better. However, if you have more than two cases, case is the construction to go for.

arthurprs
16-07-2007, 10:37 PM
I have bech some code

Compiled on BDS2006


var
i, n, j: Integer;
begin
i := 1;
Zprofiler.Mark(1, True);
case i of
1: i := 3;
2: i := 3;
3: i := 3;

end;
Zprofiler.Mark(1, False);
Zprofiler.Mark(2, True);
if i = 1 then
i := 3 else
if i = 2 then
i := 3 else
if i = 3 then
i := 3;
Zprofiler.Mark(2, False);
Zprofiler.Mark(3, True);
case i of
3: i := 3;
2: i := 3;
1: i := 3;
end;
Zprofiler.Mark(3, False);
Zprofiler.Mark(4, True);
if i = 3 then
i := 3 else
if i = 2 then
i := 3 else
if i = 1 then
i := 3;
Zprofiler.Mark(4, False);

j := 9;
Zprofiler.Mark(5, True);
case j of
1: j := 9;
2: j := 9;
3: j := 9;
4: j := 9;
5: j := 9;
6: j := 9;
7: j := 9;
8: j := 9;
9: j := 9;
end;
Zprofiler.Mark(5, False);
Zprofiler.Mark(6, True);
if j = 1 then
j := 9 else
if j = 2 then
j := 9 else
if j = 3 then
j := 9 else
if j = 4 then
j := 9 else
if j = 5 then
j := 9 else
if j = 6 then
j := 9 else
if j = 7 then
j := 9 else
if j = 8 then
j := 9 else
if j = 9 then
j := 9;

Zprofiler.Mark(6, False);

Zprofiler.Mark(8, False);

n := 3;
Zprofiler.Mark(9, True);
case n of
1: n := 3;
2: n := 3;
3: n := 3;
end;
Zprofiler.Mark(9, False);
Zprofiler.Mark(10, True);
if n = 1 then
n := 3 else
if n = 2 then
n := 3 else
if n = 3 then
n := 3;
Zprofiler.Mark(10, False);
Zprofiler.Mark(11, True);
case n of
3: n := 3;
2: n := 3;
1: n := 3;
end;
Zprofiler.Mark(11, False);
Zprofiler.Mark(12, True);
if n = 3 then
n := 3 else
if n = 2 then
n := 3 else
if n = 1 then
n := 3;
Zprofiler.Mark(12, False);

sorry the big code :oops:

3 bechs
http://img516.imageshack.us/img516/1706/capture16072007193322ih1.th.png (http://img516.imageshack.us/img516/1706/capture16072007193322ih1.png)

http://img516.imageshack.us/img516/3881/capture16072007193317hr0.th.png (http://img516.imageshack.us/img516/3881/capture16072007193317hr0.png)

http://img516.imageshack.us/img516/4407/capture16072007193311xf3.th.png (http://img516.imageshack.us/img516/4407/capture16072007193311xf3.png)


If is better but the dif is... small¬?¬?¬?¬?

cause is better to write and manipulate

captainproton
16-07-2007, 11:10 PM
@arthurprs - Thanks, that's what I wanted to see - some real evidence of speed changes.

Whilst the "case if then" question isn't vital to me it is something that I wanted to know. I am one of those guys that will spend hours pouring over my code trying to save 1kb of memory and make the exe 2kb smaller and have it run a half a second faster.

I do a lot of if a=b and b=c and c=d kind of stuff in my Slot Machine and am constantly trying to find better ways to do it.

Craig

arthurprs
17-07-2007, 03:14 AM
@arthurprs - Thanks, that's what I wanted to see - some real evidence of speed changes.

Whilst the "case if then" question isn't vital to me it is something that I wanted to know. I am one of those guys that will spend hours pouring over my code trying to save 1kb of memory and make the exe 2kb smaller and have it run a half a second faster.

I do a lot of if a=b and b=c and c=d kind of stuff in my Slot Machine and am constantly trying to find better ways to do it.

Craig

;]

No problem