Search code examples
actionscript-3actionscriptxor

Actionscript, improving xor performance?


I am using the below code to process some large files.

var joinedBytes:ByteArray = new ByteArray;
joinedBytes.length = _chunkSize;

for (var i:Number = 0; i < _chunkSize; i++) {
    joinedBytes.writeByte(_xorBytesBuffer[i]^_rndBytesBuffer[i]);
}

Its taking about 2.5 seconds to process 10mb of data on a desktop.

Is this normal performance?

Does any way exist to speed it up?

I think that some of the time is writing to the byte array.

EDIT:

_xorBytesBuffer and _rndBytesBuffer are both byte arrays.


Solution

  • I didn't test everything. I could be wrong somewhere below but...

    ByteArray is faster

    The [i] suggest you are using Vector/Array using another ByteArray for _xorBytesBuffer and _rndBytesBuffer should speed things up. You want also operate on larger data i.e. writeUnsignedInt() instead of writeByte()

    See also this question

    uint is faster vs Number

    And if you only have 10MB, you would like to use var i:uint instead of Number.

    Another thing is you can replace i++ wit ++i though I did't really test if this has much impact - I only heard that it's faster.

    Remove additional steps.

    You could even try something like:

    for (var i:uint = 0; i < _chunkSize;) {
        joinedBytes.writeByte(_xorBytesBuffer[i]^_rndBytesBuffer[i++]);
    }
    

    Please let us know it _rndBytesBuffer[i++] makes any difference ;)

    Wait, I just said to not use indicies but another ByteArrays... Well If you still want to try above still let us know how it performs ;)

    Make sure your condition check is as simple as possible.

    Make sure you have something like var _chunkSize:uint instead of

    function _chunkSize(){return something;}