Results 1 to 7 of 7

Thread: Case Statements Vs. If Then Statements

  1. #1

    Case Statements Vs. If Then Statements

    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
    Who is this "General Failure" and why is he reading my hard disk?
    <br />
    <br /><A href="http://ps.craigedgar.com" target="_blank">Picture Slots - A Unique Slot Machine! Create your own Reels using your own Digital Photographs.</A>

  2. #2

    Case Statements Vs. If Then Statements

    HI Craig

    This topic may have your answers

    http://www.pascalgamedevelopment.com...highlight=case
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  3. #3

    Case Statements Vs. If Then Statements

    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
    Who is this &quot;General Failure&quot; and why is he reading my hard disk?
    <br />
    <br /><A href="http://ps.craigedgar.com" target="_blank">Picture Slots - A Unique Slot Machine! Create your own Reels using your own Digital Photographs.</A>

  4. #4

    Case Statements Vs. If Then Statements

    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.

  5. #5

    Case Statements Vs. If Then Statements

    I have bech some code

    Compiled on BDS2006

    [pascal]
    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);
    [/pascal]
    sorry the big code ops:

    3 bechs







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

    cause is better to write and manipulate
    From brazil (:

    Pascal pownz!

  6. #6

    Case Statements Vs. If Then Statements

    @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
    Who is this &quot;General Failure&quot; and why is he reading my hard disk?
    <br />
    <br /><A href="http://ps.craigedgar.com" target="_blank">Picture Slots - A Unique Slot Machine! Create your own Reels using your own Digital Photographs.</A>

  7. #7

    Case Statements Vs. If Then Statements

    Quote Originally Posted by captainproton
    @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
    From brazil (:

    Pascal pownz!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •