AS3使用Fast ByteArray可提速20倍
7 三 2014
Adobe官方博客有篇关于Fast ByteArray的文章,介绍了一种读写ByteArray的新方法,案例实测能提速20倍。
在 i7-3770 上使用Flash Player 12 的测试结果:
在Bebug模式下,速度只提高了2倍,但这不重要
在Release模式下,提速20倍!!
官方的源码有点问题,修改好的版本在这里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package { import flash.display.Sprite; import flash.system.ApplicationDomain; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.utils.ByteArray; import flash.utils.getTimer; import avm2.intrinsics.memory.li8; import avm2.intrinsics.memory.si8; public class DomainMemoryTest extends Sprite { private var BYTE_ARRAY_SIZE:Number = 10000000; private var resultsDisplay:TextField = new TextField(); private var normalByteArrayTime:uint = 0; private var fastByteArrayTime:uint = 0; public function DomainMemoryTest() { super(); setupDisplay(); timeNormalByteArray(); timeFastByteArray(); resultsDisplay.appendText("\nFast ByteArray is " + (normalByteArrayTime - fastByteArrayTime).toString() + " milliseconds faster."); } // Write and Read 'numInts' integers in/from the bytearray private function timeNormalByteArray() : void { var ba:ByteArray = new ByteArray(); var i:int = 0; var startTimer:int = getTimer(); for(i=0; i<BYTE_ARRAY_SIZE; i++) { ba.writeByte(100); } ba.position = 0; for(i=0; i<BYTE_ARRAY_SIZE; i++) { ba.readByte(); } var endTimer:int = getTimer(); normalByteArrayTime = endTimer - startTimer; resultsDisplay.appendText("Time to write and read normal ByteArray: " + normalByteArrayTime.toString()); } private function timeFastByteArray () : void { var ba:ByteArray = new ByteArray(); // Set the size of bytearray ba.length = BYTE_ARRAY_SIZE; ApplicationDomain.currentDomain.domainMemory = ba; var i:int = 0; var valueLoadedFromByteArray:int = 0; var startTimer:int = getTimer(); for(i=0; i<BYTE_ARRAY_SIZE; i++) { si8(100, i); } for(i=0; i<BYTE_ARRAY_SIZE; i++) { valueLoadedFromByteArray = li8(i); } trace(ba.length,ba.readByte()) var endTimer:int = getTimer(); fastByteArrayTime = endTimer - startTimer; resultsDisplay.appendText("\nTime to write and read fast ByteArray: " + fastByteArrayTime.toString()); } private function setupDisplay():void { resultsDisplay.x = 10; resultsDisplay.y = 10; resultsDisplay.border = true; resultsDisplay.multiline = true; resultsDisplay.wordWrap = true; resultsDisplay.autoSize = TextFieldAutoSize.LEFT; resultsDisplay.width = 400; addChild(resultsDisplay); } } } |
源码下载:DomainMemoryTest