Created
April 2, 2012 11:25
-
-
Save stickupkid/2282785 to your computer and use it in GitHub Desktop.
String Concatination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
import flash.text.TextField; | |
import flash.text.TextFieldAutoSize; | |
import flash.text.TextFormat; | |
import flash.utils.ByteArray; | |
import flash.utils.getTimer; | |
public class Main extends Sprite | |
{ | |
private static const PAD:Number = 5; | |
private var stats:TextField = new TextField(); | |
private static const MODE_PLUS_LITERAL:int = 1; | |
private static const MODE_PLUS_VARIABLE:int = 2; | |
private static const MODE_STRING_CONCAT:int = 3; | |
private static const MODE_BYTEARRAY_UTF_LITERAL:int = 4; | |
private static const MODE_BYTEARRAY_UTF_VARIABLE:int = 5; | |
private static const MODE_BYTEARRAY_ASCII_LITERAL:int = 6; | |
private static const MODE_BYTEARRAY_ASCII_VARIABLE:int = 7; | |
private static const MODE_STRING_BUILDER_LITERAL:int = 8; | |
private var mode:int = 0; | |
public function Main() | |
{ | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.frameRate = 60; | |
makeButtons("+= Literal", "+= Variable", "+= String Concat", "ByteArray UTF Literal", "ByteArray UTF Variable", "ByteArray ASCII Literal", "ByteArray ASCII Variable", "StringBuilder Literal"); | |
stats.autoSize = TextFieldAutoSize.LEFT; | |
stats.y = this.height + PAD; | |
addChild(stats); | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
private function makeButtons(...labels): void | |
{ | |
var curX:Number = PAD; | |
var y:Number = 0; | |
for each (var label:String in labels) | |
{ | |
var tf:TextField = new TextField(); | |
tf.mouseEnabled = false; | |
tf.selectable = false; | |
tf.defaultTextFormat = new TextFormat("_sans", 16, 0x0071BB); | |
tf.autoSize = TextFieldAutoSize.LEFT; | |
tf.text = label; | |
tf.name = "lbl"; | |
tf.background = true; | |
tf.backgroundColor = 0xffffff; | |
var button:Sprite = new Sprite(); | |
button.buttonMode = true; | |
button.graphics.beginFill(0xF5F5F5); | |
button.graphics.drawRect(0, 0, tf.width+PAD, tf.height+PAD); | |
button.graphics.endFill(); | |
button.graphics.lineStyle(1); | |
button.graphics.drawRect(0, 0, tf.width+PAD, tf.height+PAD); | |
button.addChild(tf); | |
button.addEventListener(MouseEvent.CLICK, onButton); | |
tf.x = PAD/2; | |
tf.y = PAD/2; | |
button.x = curX; | |
button.y = y; | |
addChild(button); | |
curX += button.width + PAD; | |
} | |
} | |
private function onButton(ev:MouseEvent): void | |
{ | |
var tf:TextField = ev.target.getChildByName("lbl"); | |
var lbl:String = tf.text; | |
switch (lbl) | |
{ | |
case "+= Literal": | |
mode = MODE_PLUS_LITERAL; | |
break; | |
case "+= Variable": | |
mode = MODE_PLUS_VARIABLE; | |
break; | |
case "+= String Concat": | |
mode = MODE_STRING_CONCAT; | |
break; | |
case "ByteArray UTF Literal": | |
mode = MODE_BYTEARRAY_UTF_LITERAL; | |
break; | |
case "ByteArray UTF Variable": | |
mode = MODE_BYTEARRAY_UTF_VARIABLE; | |
break; | |
case "ByteArray ASCII Literal": | |
mode = MODE_BYTEARRAY_ASCII_LITERAL; | |
break; | |
case "ByteArray ASCII Variable": | |
mode = MODE_BYTEARRAY_ASCII_VARIABLE; | |
break; | |
case "StringBuilder Literal": | |
mode = MODE_STRING_BUILDER_LITERAL; | |
break; | |
} | |
} | |
private function onEnterFrame(ev:Event): void | |
{ | |
if(mode < 0) { | |
return; | |
} | |
var timer:int = getTimer(); | |
var REPS:int = 10000000; | |
var i:int; | |
var str:String = ""; | |
var aaaaaaaaaa:String = "aaaaaaaaaa"; | |
var ba:ByteArray; | |
switch (mode) | |
{ | |
case MODE_PLUS_LITERAL: | |
for (i = 0; i < REPS; ++i) | |
{ | |
str += "aaaaaaaaaa"; | |
} | |
break; | |
case MODE_PLUS_VARIABLE: | |
for (i = 0; i < REPS; ++i) | |
{ | |
str += aaaaaaaaaa; | |
} | |
break; | |
case MODE_STRING_CONCAT: | |
for (i = 0; i < REPS; ++i) | |
{ | |
str = str.concat("aaaaaaaaaa"); | |
} | |
break; | |
case MODE_BYTEARRAY_UTF_LITERAL: | |
ba = new ByteArray(); | |
ba.writeShort(REPS*10); | |
for (i = 0; i < REPS; ++i) | |
{ | |
ba.writeUTFBytes("aaaaaaaaaa"); | |
} | |
ba.position = 0; | |
str = ba.readUTF(); | |
break; | |
case MODE_BYTEARRAY_UTF_VARIABLE: | |
ba = new ByteArray(); | |
ba.writeShort(REPS*10); | |
for (i = 0; i < REPS; ++i) | |
{ | |
ba.writeUTFBytes(aaaaaaaaaa); | |
} | |
ba.position = 0; | |
str = ba.readUTF(); | |
break; | |
case MODE_BYTEARRAY_ASCII_LITERAL: | |
ba = new ByteArray(); | |
for (i = 0; i < REPS; ++i) | |
{ | |
ba.writeMultiByte("aaaaaaaaaa", "us-ascii"); | |
} | |
ba.position = 0; | |
str = ba.readMultiByte(ba.length, "us-ascii"); | |
break; | |
case MODE_BYTEARRAY_ASCII_VARIABLE: | |
ba = new ByteArray(); | |
for (i = 0; i < REPS; ++i) | |
{ | |
ba.writeMultiByte(aaaaaaaaaa, "us-ascii"); | |
} | |
ba.position = 0; | |
str = ba.readMultiByte(ba.length, "us-ascii"); | |
break; | |
case MODE_STRING_BUILDER_LITERAL: | |
const builder:StringBuilder = new StringBuilder(); | |
for (i = 0; i < REPS; ++i) | |
{ | |
builder.add("aaaaaaaaaa"); | |
} | |
str = builder.toString(); | |
break; | |
} | |
stats.text = "Time:" + (getTimer() - timer) + "ms"; | |
mode = -1; | |
} | |
} | |
} | |
import flash.utils.ByteArray; | |
class StringBuilder { | |
private var _byteArray:ByteArray; | |
public function StringBuilder() { | |
_byteArray = new ByteArray(); | |
} | |
public function add(value:String):void { | |
_byteArray.position = 0; | |
if(_byteArray.length > 0) { | |
const l:int = _byteArray.readShort() + value.length; | |
_byteArray.position = 0; | |
_byteArray.writeShort(l); | |
_byteArray.position = _byteArray.length; | |
} else { | |
_byteArray.writeShort(value.length); | |
} | |
_byteArray.writeUTFBytes(value); | |
} | |
public function toString():String { | |
const p:uint = _byteArray.position; | |
_byteArray.position = 0; | |
const l:int = _byteArray.readShort(); | |
const r:String = _byteArray.readUTFBytes(l); | |
_byteArray.position = p; | |
return r; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment