RE: [Flashcoders] switch statement more efficient than if...else?
One thing that nobody who writes switch statements seemed to know (me included) is you can (and, apparently, should) write them like this: I knew that. I guess you missed this in my post. After I showed the switch statement, I said, (or the same thing above but with brackets) Jason Merrill Bank of America Global Technology Operations Global Risk LLD eTools Multimedia Join the Bank of America Flash Platform Developer Community Are you a Bank of America associate interested in innovative learning ideas and technologies? Check out our internal GTO Innovative Learning Blog subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
- Original Message - From: Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] To: flashcoders flashcoders@chattyfig.figleaf.com Sent: Wednesday, June 25, 2008 3:12 PM Subject: [Flashcoders] switch statement more efficient than if...else? Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch I doubt that there's anything in it - the compiler writes your if statement for you with a switch statement. It would be better to be more concerned about readability and maintainability, where the switch statement will win hands down for many cases.. ;-) Paul al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
Switch/case is a bit quicker. On Wed, Jun 25, 2008 at 10:12 AM, Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] wrote: Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
One thing worth noting is switch statement uses strict equality (===) therefore it's not exactly the same as if/else with equality (==). Kenneth Kawamoto http://www.materiaprima.co.uk/ Paul Andrews wrote: Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch I doubt that there's anything in it - the compiler writes your if statement for you with a switch statement. It would be better to be more concerned about readability and maintainability, where the switch statement will win hands down for many cases.. ;-) Paul al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
I doubt there's a noticeable difference between a switch and a series of if /else if statements. In AS 3.0 bytecode, though, switches and equivalent conditionals are not necessarily compiled into the same bytecode. In fact, there's a lookupswitch instruction (opcode 0x1b). Of course, any switch can be implemented as a series of if / else if statements, but that's up to the compiler. From my (limited) experience, the Flash IDE always uses a switch bytecode whenever it finds a switch construct in Actionscript code. I don't know how it affects runtime performance, but my guess is that it doesn't have much impact. And, thinking about it, maybe that opcode exists because it's an optimized way to compile / execute a series of related conditions. Cheers Juan Pablo Califano 2008/6/25, eric e. dolecki [EMAIL PROTECTED]: Switch/case is a bit quicker. On Wed, Jun 25, 2008 at 10:12 AM, Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] wrote: Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
hey thanks for all of your responses al.z On 25 Jun 2008, at 16:24, Juan Pablo Califano wrote: I doubt there's a noticeable difference between a switch and a series of if /else if statements. In AS 3.0 bytecode, though, switches and equivalent conditionals are not necessarily compiled into the same bytecode. In fact, there's a lookupswitch instruction (opcode 0x1b). Of course, any switch can be implemented as a series of if / else if statements, but that's up to the compiler. From my (limited) experience, the Flash IDE always uses a switch bytecode whenever it finds a switch construct in Actionscript code. I don't know how it affects runtime performance, but my guess is that it doesn't have much impact. And, thinking about it, maybe that opcode exists because it's an optimized way to compile / execute a series of related conditions. Cheers Juan Pablo Califano 2008/6/25, eric e. dolecki [EMAIL PROTECTED]: Switch/case is a bit quicker. On Wed, Jun 25, 2008 at 10:12 AM, Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] wrote: Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
I'd always thought that the most efficient was the switch I know that the haXe compiler uses a jump table, I'm not sure about the AS3 compiler. But as already mentioned there is an extra opcode for switch, and even if the compiler doesn't a jump table, it might use one in the future. So, switch at least isn't slower, even if it isn't faster yet it has the potential to become faster in the future, and it's more readable in any case -- making switch the preferable choice. The question should be is switch on int faster than switch on String? Mark On Wed, Jun 25, 2008 at 4:12 PM, Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] wrote: Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
Actually, tt is my (perhaps wrong) understanding that this is not the case. In the past, both Macromedia and Adobe sources have stated that switch does not offer a performance boost--just readability and functionality (optional break lines) enhancements over if statements. I have been wrong many times, however. This may be such a case. On 6/25/08 10:48 AM, eric e. dolecki wrote: Switch/case is a bit quicker. Rich http://www.LearningActionScript3.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
and switch on Number ? :P I read somewhere that Number was quicker than int though I still can't believe it cedric I'd always thought that the most efficient was the switch I know that the haXe compiler uses a jump table, I'm not sure about the AS3 compiler. But as already mentioned there is an extra opcode for switch, and even if the compiler doesn't a jump table, it might use one in the future. So, switch at least isn't slower, even if it isn't faster yet it has the potential to become faster in the future, and it's more readable in any case -- making switch the preferable choice. The question should be is switch on int faster than switch on String? Mark On Wed, Jun 25, 2008 at 4:12 PM, Allandt Bik-Elliott (Receptacle) [EMAIL PROTECTED] wrote: Hi guys quick question that came up in a conversation I had the other day - are switch statements more or less efficient than a series of if...else statements in either AS2 or AS3? I'd always thought that the most efficient was the switch al.z ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
I disagree that switch statements are more readable than if else statements, at least if you put your braces on their own lines. if (true) { // something } else if (true) { // something else } else if (true) { // yet another outcome } I don't think either is more readable than the other, unless you write your if else or switch statements poorly (no whitespace). ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] switch statement more efficient than if...else?
I disagree that switch statements are more readable than if else statements, at least if you put your braces on their own lines. I wouldn't disagree, but I think it's more about personal preference. Maybe it's just aesthetics, but I prefer: switch(b) { case apple: do something break; case bananas: do something else break; case pears: do yet something else break; } (or the same thing above but with brackets) over: if (b==apple) { do something } else if (b==bananas) { do something else } else if (b==pears) { do yet something else } Jason Merrill Bank of America Global Technology Operations Global Risk LLD eTools Multimedia Join the Bank of America Flash Platform Developer Community Are you a Bank of America associate interested in innovative learning ideas and technologies? Check out our internal GTO Innovative Learning Blog subscribe. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] switch statement more efficient than if...else?
Steven Sacks wrote: I disagree that switch statements are more readable than if else statements, at least if you put your braces on their own lines. I'm a die-hard switch/case guy, but I agree with Steven on this. Neither is inherently easier or harder to read. Of course, I personally prefer switch statements, but I think it's a matter of style. Steven's example is every bit as readable as a switch statement. Also, with switch statements, you have to be careful about nesting switch statements. I've seen switch statements with nested switches that are 3-4 pages long. There has to be a better way to write code than that. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
better way than nested switches that are 3-4 pages long = if/else statements ;) On Wed, Jun 25, 2008 at 11:25 PM, Kerry Thompson [EMAIL PROTECTED] wrote: Steven Sacks wrote: I disagree that switch statements are more readable than if else statements, at least if you put your braces on their own lines. I'm a die-hard switch/case guy, but I agree with Steven on this. Neither is inherently easier or harder to read. Of course, I personally prefer switch statements, but I think it's a matter of style. Steven's example is every bit as readable as a switch statement. Also, with switch statements, you have to be careful about nesting switch statements. I've seen switch statements with nested switches that are 3-4 pages long. There has to be a better way to write code than that. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] switch statement more efficient than if...else?
One thing that nobody who writes switch statements seemed to know (me included) is you can (and, apparently, should) write them like this: switch (n) { case 0: { foo(); break; } case 1: { bar(); return; } case 2: { baz(); return; } default: { blech(); break; } } I definitely find that more readable than the old way. http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders