[haXe] StringBuf in Flash 8 is slow when concatenating strings
Asger Ottar Alstrup
asger at area9.dk
Wed Jan 3 16:24:03 CET 2007
Hi,
I profiled some slow code today, and found that using StringBuf in a Flash
setting was slowing things down tremendously compared to just using Strings
directly.
Looking closer at the problem, it seems that the slow-down comes from using
reflection to find out what we are StringBug.add'ing. I added a addString
interface and got a 15% speedup in my code. (If I use a String directly, I
got a total 33% speedup.)
I was wondering whether StringBuf should have an addString interface to
avoid the reflection overhead. Also, I wondered whether it might be faster
to implement StringBuf by using an Array which is join'ed together at the
end.
So I made a little benchmark:
var count = 100000;
Profiler.get().profile("Start");
var s = "";
for (i in 0...count) {
s += "s";
}
Profiler.get().profile("String");
var sb = new StringBuf();
for (i in 0...count) {
sb.add("s");
}
var result = sb.toString();
Profiler.get().profile("StringBuf");
var sb2 = new StringBuf();
for (i in 0...count) {
sb2.addString("s"); // New StringBuf method
}
var result2 = sb2.toString();
Profiler.get().profile("StringBuf 2");
var ar = [];
for (i in 0...count) {
ar.push("s");
}
var arrayResult = ar.join("");
Profiler.get().profile("Array join");
The results are for Flash 8 target:
String: 0.09 s
StringBuf: 1.965 s
StringBuf 2: 0.354 s
Array join: 0.577
So conclusions for Flash are:
- StringBuf is slower than native String for string concatenation
- Adding a addString interface to StringBuf helps string concatenation a lot
- Array joining is not faster than using normal String for string
concatenation
If the strings to be concatenated are longer than just one character, the
relative results do not change much. The gap between Array join and modified
StringBuffer is reduced a little, but the overall rank remains: String is
king, followed by modified StringBuf, followed by Array.join, and at the
end, we have current StringBuf which is terrible in Flash 8 target.
Regards,
Asger
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.motion-twin.com/pipermail/haxe/attachments/20070103/0d7b0aaf/attachment.htm
More information about the Haxe
mailing list