BitmapData saving: now 50% faster on encoding

Posted on Tuesday 24 October 2006

I was trying to optimize the encoding routine for my BitmapData saving solution and then I thought: WWTUD? Well, I tried every trick in the book: lowering the number of bitwise operations, replacing + with | or vice-versa, unrolling the encoding loop, reversing the if… All failed until I tried something dead obvious that I thought would lead to nowhere: what if I make a couple of vars locals? I was accessing BitmapData.width directly, so I saved the value in a local… 10% faster. Not bad. Then I put the static BASE64_CHARS array as a local… Woah! 40% faster! Yep, all that time was being spent accessing a static var over and over again. I wouldn’t recommend dropping the use of static vars for that reason, but in this case since it’s accessed around 10-40,000 times per loop it’s definitely worth it. On this machine, my sample image (100,000 pixels) is now encoded in 32-bits in 1500 ms instead of 2900 ms. In the 24 bit and 12 bit cases, the difference is less dramatic, around 30-40%, but it’s still very sizable. The encoding algorithm hasn’t changed so the php side and the size of the sent data are exactly the same as previously.

You can download the updated zip here.


WordPress database error: [Can't open file: 'wp_comments.MYD'. (errno: 145)]
SELECT * FROM wp_comments WHERE comment_post_ID = '215' AND comment_approved = '1' ORDER BY comment_date

No comments have been added to this post yet.

Leave a comment




Your e-mail address is never displayed. If you run into issues with SpamKarma blocking you, email me at $patrick->5etdemi(com)


RSS feed for comments on this post | TrackBack URI