How to quickly perform a number to int conversion ?
So we have a var:n:Number;
,
to convert our Number to an int, the simple way is:
var i:int = int(n);
another way, is to just let the compiler do the conversion
var i:int = n;
or force an integer conversion by performing a degenerated operation:
var i:int = n >>> 0;
or:
var i:int = n >> 0;
and now using bits twiddling tricks and alchemy to extract the integer part:
first a magic number:
const D_DOUBLE_MAGIC:Number = 6755399441055744.0; //2^52 * 1.5, uses limited precision to floor
now the conversion:
fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0);
(fastmem class is from Azoth http://www.buraks.com/azoth/ , and alchemy opcode is injected to the swf after normal compilation using azoth.exe, the same thing can be done with apparat, or directy in fash with latest flash)
which method is the fastest?
Our winner, Sree&Achemy method !
What do you get on your computer and flash version ?
protected function integerNumber():void { var i:int = 0; var a:int; var b:int = Math.random(); var val:Number; const D_DOUBLE_MAGIC:Number = 6755399441055744.0; //2^52 * 1.5, uses limited precision to floor var time1:uint = getTimer(); for(i= 0;i<5000000;i++) { val = Number((i&16383)-8192);val*=0.25; a = int(val); a = int(val); a = int(val); a = int(val); a = int(val); a = int(val); a = int(val); a = int(val); a = int(val); a = int(val); } var time2:uint = getTimer(); for(i= 0;i<5000000;i++) { val = Number((i&16383)-8192);val*=0.25; a = val >>0; a = val >>0; a = val >>0; a = val >>0; a = val >>0; a = val >>0; a = val >>0; a = val >>0; a = val >>0; a = val >>0; } var time3:uint = getTimer(); for(i= 0;i<5000000;i++) { val = Number((i&16383)-8192);val*=0.25; a = val; a = val; a = val; a = val; a = val; a = val; a = val; a = val; a = val; a = val; } var time4:uint = getTimer(); for(i= 0;i<5000000;i++) { val = Number((i&16383)-8192);val*=0.25; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; a = val >>>0; } var time5:uint = getTimer(); for(i= 0;i<5000000;i++) { val = Number((i&16383)-8192);val*=0.25; fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); fastmem.fastSetDouble(val+D_DOUBLE_MAGIC,0); a = fastmem.fastGetI32(0); } var time6:uint = getTimer(); startTest(); setTest( "=int(n)" , "NumberToInteger", time2-time1 ); setTest( "=n" , "NumberToInteger", time4-time3 ); setTest( "=n >>> 0" , "NumberToInteger", time5-time4 ); setTest( "=n >> 0" , "NumberToInteger", time3-time2 ); setTest( "=Alchemy_Sree(n)" , "NumberToInteger", time6-time5 ); endTest(); }
Leave a Reply