Re: [Flashcoders] Even odds
Geeks ;) 2005/12/9, Mark Winterhalder <[EMAIL PROTECTED]>: > > OK, I ran some tests of my own. After repeating it a couple of times, > with various outcomes, the trend seems to be that a) the cast is about > 10% faster than the equality, and b) the bitwise and is about 10% > faster than the modulo. > I used MTASC to compile, FP8 under wine on Linux, and the ActionStep > Debug Panel (ActionStep alpha 1 has been released! check it out: > actionstep.org). > I decided to go with triggering it onKeyDown, to avoid initialization > of the player and to be easily able to run repeated tests. > > Here's the code: > > var bench:Object = {}; > bench.onKeyDown = function () { > var repeats:Number = 50; > var i:Number = repeats; > var foo; > var start:Number = getTimer(); > while( i-- ) { > foo = ( Math.random() * 10 )|0; > } > var overhead:Number = getTimer() - start; > trace( "overhead: " + overhead ); > i = repeats; > start = getTimer(); > while( i-- ) { > foo = ( Math.random() * 10 )|0 % 2 == 1; > } > trace( "modulo, equality: " + ( getTimer() - start - overhead ) ); > i = repeats; > start = getTimer(); > while( i-- ) { > foo = Boolean( ( Math.random() * 10 )|0 % 2 ); > } > trace( "modulo, cast: " + ( getTimer() - start - overhead ) ); > i = repeats; > start = getTimer(); > while( i-- ) { > foo = ( Math.random() * 10 )|0 & 1 == 1; > } > trace( "and, equality: " + ( getTimer() - start - overhead ) ); > i = repeats; > start = getTimer(); > while( i-- ) { > foo = Boolean( ( Math.random() * 10 )|0 & 1 ); > } > trace( "and, cast: " + ( getTimer() - start - overhead ) ); > } > Key.addListener( bench ); > > //mark > > -- > http://snafoo.org/ > jabber: [EMAIL PROTECTED] > ___ > 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] Even odds
Seems my last email got swallowed by the void. Here it is: Nice tests! You're totally right. I had no idea that Macromedia did any optimization during compilation. Very cool. It's also pretty interesting that Java did not seem to optimize the very same code (the numbers seem to suggest it). haha, thanks for the plug. Scott -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mark Winterhalder Sent: December 9, 2005 12:09 AM To: Flashcoders mailing list Subject: Re: [Flashcoders] Even odds OK, I ran some tests of my own. After repeating it a couple of times, with various outcomes, the trend seems to be that a) the cast is about 10% faster than the equality, and b) the bitwise and is about 10% faster than the modulo. I used MTASC to compile, FP8 under wine on Linux, and the ActionStep Debug Panel (ActionStep alpha 1 has been released! check it out: actionstep.org). I decided to go with triggering it onKeyDown, to avoid initialization of the player and to be easily able to run repeated tests. Here's the code: var bench:Object = {}; bench.onKeyDown = function () { var repeats:Number = 50; var i:Number = repeats; var foo; var start:Number = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0; } var overhead:Number = getTimer() - start; trace( "overhead: " + overhead ); i = repeats; start = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0 % 2 == 1; } trace( "modulo, equality: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = Boolean( ( Math.random() * 10 )|0 % 2 ); } trace( "modulo, cast: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0 & 1 == 1; } trace( "and, equality: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = Boolean( ( Math.random() * 10 )|0 & 1 ); } trace( "and, cast: " + ( getTimer() - start - overhead ) ); } Key.addListener( bench ); //mark -- http://snafoo.org/ jabber: [EMAIL PROTECTED] ___ 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] Even odds
Nice tests! You're totally right. I had no idea that Macromedia did any optimization during compilation. Very cool. It's also pretty interesting that Java did not seem to optimize the very same code (the numbers seem to suggest it). haha, thanks for the plug. Scott -Original Message- From: [EMAIL PROTECTED] on behalf of Mark Winterhalder Sent: Fri 12/9/2005 12:09 AM To: Flashcoders mailing list Cc: Subject: Re: [Flashcoders] Even odds OK, I ran some tests of my own. After repeating it a couple of times, with various outcomes, the trend seems to be that a) the cast is about 10% faster than the equality, and b) the bitwise and is about 10% faster than the modulo. I used MTASC to compile, FP8 under wine on Linux, and the ActionStep Debug Panel (ActionStep alpha 1 has been released! check it out: actionstep.org). I decided to go with triggering it onKeyDown, to avoid initialization of the player and to be easily able to run repeated tests. Here's the code: var bench:Object = {}; bench.onKeyDown = function () { var repeats:Number = 50; var i:Number = repeats; var foo; var start:Number = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0; } var overhead:Number = getTimer() - start; trace( "overhead: " + overhead ); i = repeats; start = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0 % 2 == 1; } trace( "modulo, equality: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = Boolean( ( Math.random() * 10 )|0 % 2 ); } trace( "modulo, cast: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0 & 1 == 1; } trace( "and, equality: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = Boolean( ( Math.random() * 10 )|0 & 1 ); } trace( "and, cast: " + ( getTimer() - start - overhead ) ); } Key.addListener( bench ); //mark -- http://snafoo.org/ jabber: [EMAIL PROTECTED] ___ 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] Even odds
OK, I ran some tests of my own. After repeating it a couple of times, with various outcomes, the trend seems to be that a) the cast is about 10% faster than the equality, and b) the bitwise and is about 10% faster than the modulo. I used MTASC to compile, FP8 under wine on Linux, and the ActionStep Debug Panel (ActionStep alpha 1 has been released! check it out: actionstep.org). I decided to go with triggering it onKeyDown, to avoid initialization of the player and to be easily able to run repeated tests. Here's the code: var bench:Object = {}; bench.onKeyDown = function () { var repeats:Number = 50; var i:Number = repeats; var foo; var start:Number = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0; } var overhead:Number = getTimer() - start; trace( "overhead: " + overhead ); i = repeats; start = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0 % 2 == 1; } trace( "modulo, equality: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = Boolean( ( Math.random() * 10 )|0 % 2 ); } trace( "modulo, cast: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = ( Math.random() * 10 )|0 & 1 == 1; } trace( "and, equality: " + ( getTimer() - start - overhead ) ); i = repeats; start = getTimer(); while( i-- ) { foo = Boolean( ( Math.random() * 10 )|0 & 1 ); } trace( "and, cast: " + ( getTimer() - start - overhead ) ); } Key.addListener( bench ); //mark -- http://snafoo.org/ jabber: [EMAIL PROTECTED] ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Even odds
On 12/9/05, Scott Hyndman <[EMAIL PROTECTED]> wrote: > I just did a test. The bitwise approach is not faster...although when you > think about it, it really should be. Hi Scott, the test won't work because (50010001 % 2 == 1) always resolves to false and the compiler will optimize it (at least MMC, not sure about MTASC). basically, the same is true for Boolean(50010001 & 1), but maybe it just optimizes the (50010001 & 1) part because Boolean might be a custom replacement for the intrinsic class? mark > Here is the test code: > > // Modulo - Time ~225ms > var time:Number = getTimer(); > for (var i:Number = 0; i < 8; i++) { > var isOdd:Boolean = 50010001 % 2 == 1; > } > trace(getTimer() - time); > > // Bitwise And - Time: ~356ms > time = getTimer(); > for (var i:Number = 0; i < 8; i++) { > var isOdd:Boolean = Boolean(50010001 & 1); > } > trace(getTimer() - time); > > I also did a quick test in Java. The bitwise AND approach is about 50% > faster...really makes you wonder. Maybe AS3 is an improvement. > > Scott > > -Original Message- > From: [EMAIL PROTECTED] on behalf of Boon Chew > Sent: Thu 12/8/2005 9:34 PM > To: Flashcoders mailing list > Cc: > Subject:Re: [Flashcoders] Even odds > > Yet another way (and probably the fastest): > > var isOdd:Boolean = Boolean(num & 1); > > - boon > > Mike Boutin <[EMAIL PROTECTED]> wrote: Is it possible to check a number to > see if its even / odd? > > > Thanks! > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > > - > Yahoo! Shopping > Find Great Deals on Holiday Gifts at Yahoo! Shopping > ___ > 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 > > > -- http://snafoo.org/ jabber: [EMAIL PROTECTED] ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Even odds
I just did a test. The bitwise approach is not faster...although when you think about it, it really should be. Here is the test code: // Modulo - Time ~225ms var time:Number = getTimer(); for (var i:Number = 0; i < 8; i++) { var isOdd:Boolean = 50010001 % 2 == 1; } trace(getTimer() - time); // Bitwise And - Time: ~356ms time = getTimer(); for (var i:Number = 0; i < 8; i++) { var isOdd:Boolean = Boolean(50010001 & 1); } trace(getTimer() - time); I also did a quick test in Java. The bitwise AND approach is about 50% faster...really makes you wonder. Maybe AS3 is an improvement. Scott -Original Message- From: [EMAIL PROTECTED] on behalf of Boon Chew Sent: Thu 12/8/2005 9:34 PM To: Flashcoders mailing list Cc: Subject: Re: [Flashcoders] Even odds Yet another way (and probably the fastest): var isOdd:Boolean = Boolean(num & 1); - boon Mike Boutin <[EMAIL PROTECTED]> wrote: Is it possible to check a number to see if its even / odd? Thanks! ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders - Yahoo! Shopping Find Great Deals on Holiday Gifts at Yahoo! Shopping ___ 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] Even odds
Yet another way (and probably the fastest): var isOdd:Boolean = Boolean(num & 1); - boon Mike Boutin <[EMAIL PROTECTED]> wrote: Is it possible to check a number to see if its even / odd? Thanks! ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders - Yahoo! Shopping Find Great Deals on Holiday Gifts at Yahoo! Shopping ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Even odds
Awesome, thank you both Scott Hyndman wrote: var isOdd:Boolean = number % 2 == 1; -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mike Boutin Sent: December 8, 2005 7:40 PM To: Flashcoders mailing list Subject: [Flashcoders] Even odds Is it possible to check a number to see if its even / odd? Thanks! ___ 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] Even odds
var isOdd:Boolean = number % 2 == 1; -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mike Boutin Sent: December 8, 2005 7:40 PM To: Flashcoders mailing list Subject: [Flashcoders] Even odds Is it possible to check a number to see if its even / odd? Thanks! ___ 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] Even odds
Use your friend the modulus operater: If( I % 2 == 0) { //even number code } Else { //odd number code } This operater returns the remainder of a numbers division. (stupid email app is trying to capitilize my code) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mike Boutin Sent: Thursday, December 08, 2005 4:40 PM To: Flashcoders mailing list Subject: [Flashcoders] Even odds Is it possible to check a number to see if its even / odd? Thanks! ___ 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