1

I have this piece of code:

palette = new Array(paletteSize);
for (var i:int=0;i<paletteSize;i++) {
  palette[i] = 0xFF000000
    | (inputStream.readUnsignedByte() << 16)
    | (inputStream.readUnsignedByte() << 8)
    | (inputStream.readUnsignedByte());
}

This code is executed around 300 times and each time paletteSize varies from 1-255. So, on the whole this code takes around 60-80ms. Can this be optimized in some way? inputStream(IDataInput) is a socket connection and it does not spend any time waiting on i/o. This code is only executed when enough number of bytes are available in stream.

4
  • 3
    Try use "Vector.<uint>" instead of Array. Also if you control the incoming stream you could send 4-byte colors and read them using readUnsignedInt instead, avoiding the bit shifting. Commented Nov 19, 2011 at 14:34
  • 2
    @VilleKrumlinde Why don't you make that an answer? Commented Nov 19, 2011 at 14:38
  • I wasn't sure because it's been a while since I did as3-development so I cowardly wrote a comment instead (downvoters are ruthless ;-) ), but I've made it an answer now. Commented Nov 19, 2011 at 15:08
  • 1
    you could also try loop unwinding to see if it improves anything: en.wikipedia.org/wiki/Loop_unwinding Commented Nov 19, 2011 at 15:28

1 Answer 1

2

Copied from my comment: Try use "Vector." instead of Array. Also if you control the incoming stream you could send 4-byte colors and read them using readUnsignedInt instead, avoiding the bit shifting.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to an array only, since it goes to bitmapdata.paletteMap() :( and i don't have control over incoming stream either
I see. Perhaps read both R and G as a single 16-bit short with "(inputStream.readUnsignedShort() << 8) | inputStream.readUnsignedByte()" then? Sensitive for endianess though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.