Given an integer :
var i:int;
How to quickly convert it to a Number?
var n:Number = Number(i);
what about:
var n:Number = i;
Can we go faster ? Well, I tried various bit tricks, but I could not get a faster version than the simple n=i;
here one version I tried (for positive integers):
const D_DOUBLE_MAGIC:Number = 6755399441055744.0; //2^52 * 1.5, uses limited precision to floor //set exponent and bit sign: fastmem.fastSetI32(0x43380000,4);
now the conversion:
fastmem.fastSetI32(i,0); n = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC;
unfortunately, this alchemy version is slower:
I also tried this scenario : given a byteArray containing a serie of integers, starting at the end of the byteArray,and walking back, we can convert each integer to number, by adding 4 bytes to the next integer with the magic 0x43380000, and read the 8 bytes as a Number.. but even with this scenario, the alchemy trick is slower than a direct conversion.
What do you get ?
integer to number code:
protected function integerToNumber():void { //alchemy initialization - using Azoth var ba:ByteArray= new ByteArray(); ba.endian = Endian.LITTLE_ENDIAN; ba.length = 1024; fastmem.fastSelectMem(ba); //azoth var i:int = 0; var a:Number; var val:int; const D_DOUBLE_MAGIC:Number = 6755399441055744.0; //2^52 * 1.5, uses limited precision to floor fastmem.fastSetI32(0x43380000,4); var time1:uint = getTimer(); for(i= 0;i<5000000;i++) { val = i;//-2500000; a = Number(val); a = Number(val); a = Number(val); a = Number(val); a = Number(val); a = Number(val); a = Number(val); a = Number(val); a = Number(val); a = Number(val); } var time2:uint = getTimer(); for(i= 0;i<5000000;i++) { val = i;//-2500000; a =val; a =val; a =val; a =val; a =val; a =val; a =val; a =val; a =val; a =val; } var time3:uint = getTimer(); for(i= 0;i<5000000;i++) { val = i; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(val,0); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; } var time4:uint = getTimer(); startTest(); setTest( "n=Number(i)" , "IntegerToNumber", time2-time1 ); setTest( "n=i" , "IntegerToNumber", time3-time2 ); setTest( "=Alchemy_BenSree(n)" , "IntegerToNumber", time4-time3 ); endTest(); }
integer to number in ByteArray code:
protected function byteArray_intToNumber():void { //alchemy initialization - using Azoth var ba:ByteArray= new ByteArray(); ba.endian = Endian.LITTLE_ENDIAN; ba.length = 1024; fastmem.fastSelectMem(ba); //azoth var a:Number; var i:int; 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++) { a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); a = fastmem.fastGetI32(0); } var time2:uint = getTimer(); for(i=0;i<5000000;i++) { fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; fastmem.fastSetI32(0x43380000,4); a = fastmem.fastGetDouble(0)-D_DOUBLE_MAGIC; } var time3:uint = getTimer(); startTest(); setTest( "direct conversion" , "ByteArray_IntegerToNumber", time2-time1 ); setTest( "alchemy trick" , "ByteArray_IntegerToNumber", time3-time2 ); endTest(); }
Leave a Reply