AS3/JS string concatenation methods performace test

October 9th, 2009  |  Published in Uncategorized

When writing BrainFlash, I was thinking if the string concatenations for program output will slow down the whole interpreter. That is because I was once hit that a year ago when dealing with XML.

I asked for better handling methods over StackOverflow. It is interesting that some of the answers shown that using “+=” is already the fastest. But I still want to know more about that, so I wrote my own simple testing program, which is below:

ActionScript version

http://get.adobe.com/flashplayer

source code

Methods used are:

  1. str += concateString;
  2. str = str.concat(concateString);
  3. array.push(concateString); … str = array.join(“”);
  4. vector.push(concateString); … str = vector.join(“”);
  5. byteArray.writeUTFBytes(concateString); … str = byteArray.readUTFBytes(byteArray.length);
  6. byteArray.writeMultiByte(concateString,”us-ascii”); … str = byteArray.readMultiByte(str.length,”us-ascii”);

The result shows that fastest method is using “+=”, but using Array/Vector is still very close to it. Using ByteArray is slow and with ASCII instead of UTF-8 is even slower…

And all the methods are performed reasonably fast, what is slow is when showing the resulting string on TextArea… It takes a few seconds! But when it is drawn, run it again and it will become normal speed, maybe there is some caching?

JavaScript version

I coded a JavaScript port too. See it here.

Methods used are:

  1. str += concateString;
  2. str = str.concat(concateString);
  3. array.push(concateString); … str = array.join(“”);

Interesting enough, the result is very similar to AS3. It is the opposite of what we believe using the Array trick will let it performs faster. “+=” is the fastest in most cases, if not, that’s not much difference.

I’ve only tested in IE8(Win), Firefox 3 (Win/Mac), Safari 4 (Win/Mac), Chrome 3 (Win). The really really really interesting part is in Chrome 3, using the concat method of String is x7000 SLOWER than the other two!! What are your results?

Tags: , ,

BrainFlash, the AS3 BrainFuck interpreter

October 8th, 2009  |  Published in Uncategorized

If you haven’t heard about Brainfuck yet, it is a famous esoteric programming language. If you want to know why it has such a name take a look at its hello-world program source code (from wikipedia):

+++ +++ +++ +           initialize counter (cell #0) to 10
[                       use loop to set the next four cells to 70/100/30/10
    > +++ +++ +             add  7 to cell #1
    > +++ +++ +++ +         add 10 to cell #2
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
    <<< < -                 decrement counter (cell #0)
]
>++ .                   print 'H'
>+.                     print 'e'
+++ +++ +.              print 'l'
.                       print 'l'
+++ .                   print 'o'
>++ .                   print ' '
<<+ +++ +++ +++ +++ ++. print 'W'
>.                      print 'o'
+++ .                   print 'r'
--- --- .               print 'l'
--- --- --.             print 'd'
>+.                     print '!'
>.                      print '\n'

the above program can be written as:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

So now you should know how lovely the language is and why I need to write a interpreter in Flash: now you can manipulate a ByteArray with BrainFuck code!!! And ya, the name of my interpreter is even more friendly than the language’s name! :)

Here is the result demo (source code here), type in a program and its input then click run. The default program is to add up two single-digit integers. And the output is correct only if the sum is single-digit too. And again the code is from wikipedia. You may find other BrainFuck source code to test from Panu Kalliokoski’s Brainfuck Archive.

The interpreter is written by only looking at its command description. There are some bugs as I know. For example when inputting multiple lines, the output may have wrong formating. And it can not have dynamic input like having inputs in a infinite loop. Actually any infinite loop will freeze your browser.

I don’t think I will go to improve it anytime soon. So please, don’t use it in production (will anyone??).

Tags: ,

Pixel Bender port of Scale2x and Scale3x

September 28th, 2009  |  Published in Uncategorized

scale2x

When I first saw the Scale2x, scale3x AS3 I am impressed. The algorithm is from scale2x sourceforge project and the as3 port is coded as a class that is very easy to reuse. Surely the AS3 port author nicoptere can make a PB port too as you may find out many PB cool stuff over his blog, but my pixel bending desire force me to do it myself :P

Here below the video shows my demo. I get a camera capture stream and used Posterizer which is downloaded from Pixel Bender Exchange to convert the image to 4-color-only, and than use the scale2x filter to enlarge the low resolution capture. The top-left corner thumbnail-like image is actually the original 1:1 input(160*120).

YouTube Preview Image

It is quite interesting to see for scale2x, PB’s version nearly doubles the performance of AS3′s. But for scale3x, there is not much different between PB and AS3. I think it is because scale3x has too many if-else which PB is not strong at.

BTW, I discovered a bug in PB while coding this. The bug make my code much longer as I work-a-round it… I reported it to the PB forum.

Demo app

Demo app’s Flex project with Pixel Bender source (The shorter but buggy versions are included too.)

Tags: ,