Combining Strings and variables—yet another performance test
I’ve been doing a ton of performance testing lately. It’s important to know which methods are the fastest, especially if you’ve been using some of the slowest methods all your life—I’m a victim. Getting in the mindset of “performance first” starts the snowball in improving your apps. This time I’m testing methods for combining Strings, with the hope that I can save a few milliseconds.
Taking a step back to look at memory, Strings are surprisingly heavy. It’s such a commonly used class, you’d think they would be the lightest of them all, but nope. The String class is THE heaviest class in DestroyTwitter, accounting for 40% of its memory usage. The Class class comes in second with 39% and TextField in third with a measly 4%. I always thought BitmapData took the crown, but it limps in fifth with 1.5% just behind the Function class.
Back to speed, we have three of the fastest String combination methods facing off. There’s String+String, String+=String, and String.concat(). I really wish AS3 handled String combination like PHP, allowing embedded variables without multiple Strings or slow methods like StringUtil.substitute(). Because of this, combining Strings and variables involve a ton of quotes and plus signs or a slower String.concat() method. Here are the results followed by the code:
[SWF] Application.swf - 392,771 bytes after decompression –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– + (100000 iterations) Player version: MAC 10,0,32,18 (debug) –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– method...................................................ttl ms...avg ms + 336 0.00 –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– += (100000 iterations) Player version: MAC 10,0,32,18 (debug) –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– method...................................................ttl ms...avg ms += 369 0.00 –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– concat (100000 iterations) Player version: MAC 10,0,32,18 (debug) –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– method...................................................ttl ms...avg ms concat 496 0.00 ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package app.test { import com.gskinner.utils.PerformanceTest; import flash.display.Sprite; import flash.utils.setTimeout; public class Test extends Sprite { public var p:PerformanceTest; public var string:String = ""; public var firstname:String = "Jonnie"; public var lastname:String = "Hallman"; public var adjective:String = "fun"; public var noun:String = "guy"; public function Test() { p = PerformanceTest.getInstance(); setTimeout(run, 1000); } public function run ():void { var it:uint = 100000; p.testFunction(stringAddition, it, "+"); p.testFunction(stringAdditionEquals, it, "+="); p.testFunction(stringConcat, it, "concat"); } public function stringAddition ():void { string = "" + firstname + lastname + "is" + "a" + adjective + noun + "to" + "be" + "around." } public function stringAdditionEquals ():void { string = ""; string += firstname; string += lastname; string += "is"; string += "a"; string += adjective; string += noun; string += "to"; string += "be"; string += "around."; } public function stringConcat ():void { string = ""; string.concat( firstname, lastname, "is", "a", adjective, noun, "to", "be", "around."); } } } |






