Re: Small patch for loop macro
| Newsgroups | gmane.comp.java.helma.general |
|---|---|
| Message-ID | <[email protected]> |
> If you just have one string to output, it doesn't make much of a > difference, since the Helma Skin class will just do the res.write() > for you. If, on the other hand, your macro output is composed of many > small pieces, it's better to directly use res.write(), since > composing a string with multiple + concatenations adds a lot of > memory overhead (each + operation results in the whole string being > reallocated in memory). which is, btw., not true anymore in browsers (esp. firefox). i once benchmarked the various methods (see below) and found that only helma/rhino behaves pretty badly when concatenating strings via the "+" operator. the methods i checked: * String assignment: str = str + "foo" * String.concat: String.concat(str, "foo") // if available * Array assignment: arr[arr.length] = str * Array.push: arr.push(str) here's a typical result creating strings with 50.000 chars as an example (the first values represent the duration in milliseconds, the string lengths appear in brackets): Browser (IE) String assignment: 1031 (50000) Array assignment: 406 (50000) Array.push: 547 (50000) Server String assignment: 6256 (50000) Array assignment: 112 (50000) Array.push: 133 (50000) -- Browser (Moz) String assignment: 47 (50000) String.concat: 797 (50000) Array assignment: 172 (50000) Array.push: 203 (50000) Server String assignment: 6388 (50000) Array assignment: 337 (50000) Array.push: 249 (50000) (i attached the source code of the benchmark test to this message -- hope it gets through our firewall :) i am still wonderung what could be the reason that the string concatenation operator performs so differently in helma/rhino? ciao, tobi _______________________________________________ Helma-user mailing list [email protected] http://helma.org/mailman/listinfo/helma-user
str_benchmark.txt
(text/plain, 1.4 KB)
var str_benchmark = function() {
var benchmark = function() {
var log = function(str) {
str += "<br />";
(typeof res === "undefined") ? document.writeln(str) : res.writeln(str$
};
var MAX = 50000;
var start, str, arr;
start = new Date;
str = "";
for (var i=0; i<MAX; i+=1) {
str = str + "x";
}
log("String assignment: " + (new Date - start) + " (" + str.length + ")");
if (String.concat) {
start = new Date;
str = "";
for (var i=0; i<MAX; i+=1) {
str = String.concat(str, "x");
}
log("String.concat: " + (new Date - start) + " (" + str.length + ")");
}
start = new Date;
arr = [];
for (var i=0; i<MAX; i+=1) {
arr[arr.length] = "x";
}
arr.join();
log("Array assignment: " + (new Date - start) + " (" + arr.length + ")");
start = new Date;
arr = [];
for (var i=0; i<MAX; i+=1) {
arr.push("x");
}
arr.join();
log("Array.push: " + (new Date - start) + " (" + arr.length + ")");
return;
};
res.writeln("<h4>Browser</h4>");
res.writeln("<script>");
res.write("var benchmark = ");
res.writeln(benchmark.toSource() + ";");
res.writeln("benchmark();");
res.writeln("</script>");
res.writeln("<h4>Server</h4>");
benchmark();
return;
};