动作脚本3,生成随机数字和放置在字节数组,更快? [英] Actionscript 3 , generating random numbers and placing in byte array, faster?
本文介绍了动作脚本3,生成随机数字和放置在字节数组,更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for(var i:Number = 0; i< reqLength; i ++){
rnd = Math.random()* 255;
randomBytes.writeByte(rnd);
}
有更快捷的方法吗?
我正在生成 int ,因为我正在创建扩展ASCII字符的字节数组。
var aBytes:ByteArray = new ByteArray;
//在填充之前设置最终的ByteArray长度。
//节省大约30%的时间。
aBytes.length = 10000000;
//写入2 500 000 x 4字节无符号整数,而不是10 000 000 x 1字节。
//你的操作会减少4倍,因此速度要快4倍。
for(var i:int = 0; i< 2500000; i ++)
{
//不要使用局部变量=更少的操作。
aBytes.writeUnsignedInt(Math.random()* uint.MAX_VALUE);
}
还有另一个有趣的选项,工作速度更快,比如100ms:
var aRaster:BitmapData = new BitmapData(5000,500,true, 0x000000处);
var aBytes:ByteArray = new ByteArray;
aRaster.noise(256 * Math.random());
Raster.copyPixelsToByteArray(aRaster.rect,aBytes);
I am trying to generate large byte arrays of random bytes. Using the below code I can generate 10 million random bytes and place in a byte array in about 4 seconds. 2 seconds for the generation and 2 second to place on the array.
for (var i:Number = 0; i < reqLength; i++){
rnd = Math.random() * 255;
randomBytes.writeByte(rnd);
}
Does a faster way exist?
I am generating ints because I am creating a byte array of extended ASCII chars.
解决方案
With a few tweaks I tuned it down from 4s to 0.5s.
var aBytes:ByteArray = new ByteArray;
// Set the final ByteArray length prior to filling it.
// It saves about 30% of elapsed time.
aBytes.length = 10000000;
// Write 2 500 000 x 4 byte unsigned ints instead of 10 000 000 x 1 bytes.
// You'll get 4 times less operations thus it is about 4 times faster.
for (var i:int = 0; i < 2500000; i++)
{
// Don't use local variable = less operations.
aBytes.writeUnsignedInt(Math.random() * uint.MAX_VALUE);
}
P.S. There's another funny option, works much faster, like 100ms:
var aRaster:BitmapData = new BitmapData(5000, 500, true, 0x000000);
var aBytes:ByteArray = new ByteArray;
aRaster.noise(256 * Math.random());
aRaster.copyPixelsToByteArray(aRaster.rect, aBytes);
这篇关于动作脚本3,生成随机数字和放置在字节数组,更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文