• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: if statements vs. switch
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: if statements vs. switch


  • Subject: Re: if statements vs. switch
  • From: Stefan Pantos <email@hidden>
  • Date: Thu, 5 Sep 2002 06:50:41 -0700 (PDT)

Hi,
I should compile to more efficent code as it uses a jump with an offset to find the correct one instead of a compare jump for each test. Also the line [prefs integerForKey:@"ControlsDisplaySide"] isn't re-evaluated each time. When optimised it would probably be like this for a if:

value = [prefs integerForKey:@"ControlsDisplaySide"];
if (value == 0)
setFrameTopLeftPoint:topLeft];
else if (value == 1)
setFrameTopLeftPoint:bottomLeft];
else if (value == 2)
setFrameTopLeftPoint:topRight];
else if (value == 3)
setFrameTopLeftPoint:bottomRight];

where as a switch would not do all the test. I guess you could think of it doing something like this:

value = [prefs integerForKey:@"ControlsDisplaySide"];
goto value;
0:
setFrameTopLeftPoint:topLeft];
goto end;
1:
setFrameTopLeftPoint:bottomLeft];
goto end;
2:
setFrameTopLeftPoint:topRight];
goto end;
3:
setFrameTopLeftPoint:bottomRight];
end:


Hope this helps,
Stefan

>Date: Wed, 4 Sep 2002 15:00:12 +0100
>Subject: if statements vs. switch
>From: Jeremy Dronfield <email@hidden>
>To: Cocoa-Dev Apple <email@hidden>
>
>A C question really, and a matter of interest rather than a problem.
> From the point of view of speed and/or efficiency, what are the relative
>merits of a chain of IF statements versus a switch? Take the following
>as an example:
>
>As ifs:
>if ([prefs integerForKey:@"ControlsDisplaySide"] == 0) [myWindow
>setFrameTopLeftPoint:topLeft];
> if ([prefs integerForKey:@"ControlsDisplaySide"] == 1) [myWindow
>setFrameTopLeftPoint:bottomLeft];
> if ([prefs integerForKey:@"ControlsDisplaySide"] == 2) [myWindow
>setFrameTopLeftPoint:topRight];
> if ([prefs integerForKey:@"ControlsDisplaySide"] == 3) [myWindow
>setFrameTopLeftPoint:bottomRight];
>
>As switch:
>switch ([prefs integerForKey:@"ControlsDisplaySide"]) {
> case 0:
> [myWindow setFrameTopLeftPoint:topLeft];
> break;
> case 1:
> [myWindow setFrameTopLeftPoint:bottomLeft];
> break;
> case 2:
> [myWindow setFrameTopLeftPoint:topRight];
> break;
> case 3:
> [myWindow setFrameTopLeftPoint:bottomRight];
> break;
> }
>
>Does the switch only read the "prefs" value once, or is this appearance
>misleading? Will one approach compile more efficiently (i.e. more
>compactly) than the other? Will changing the second thru' fourth "ifs"
>into "else ifs" actually make for cleaner running (I presume it would)?
>I suppose one could tidy up the ifs further by putting the prefs value
>into an int and using that for comparison, but what I'd really like to
>know is whether the switch is generally better. My "Introduction to C"
>just says vaguely that it's more convenient.
>-Jeremy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: if statements vs. switch
      • From: "Clark S. Cox III" <email@hidden>
  • Prev by Date: Re: Searching AddressBook
  • Next by Date: Re: if statements vs. switch
  • Previous by thread: Re: if statements vs. switch
  • Next by thread: Re: if statements vs. switch
  • Index(es):
    • Date
    • Thread