Dr said:
JRS: In article <
[email protected]>,
dated Mon, 19 Sep 2005 18:29:23, seen in Java
script Dude said:
In many languages, it is necessary to string together multiple strings
into one string for use over multiple lines of code.
Which one is the most efficient from the interpreters perspective:
Case 1:
str += '<?xml version="1.0" encoding="' + charset + '"?>\n';
str += '<view-source-with version="1.1">\n';
str += ' <default-item-index>' + this.defaultItem +
'</default-item-index>\n';
str += ' <show-frame-warning>' + this.frameWarning
+'</show-frame-warning>\n';
Case 2:
str = '<?xml version="1.0" encoding="' + charset + '"?>\n'+
'<view-source-with version="1.1">\n'+
' <default-item-index>' + this.defaultItem +
'</default-item-index>\n'+
' <show-frame-warning>' + this.frameWarning
+'</show-frame-warning>\n'+
...
Case 1 is the most common but case 2 is my personal preference. It
takes less code to write but I'm not sure how effective it is when it
reaches the language optimization and interpreter.
Any ideas which is better from a raw interpreter performance
perspective?
charset = "ddd"
K = 8778
D0 = new Date()
J = K ; while (J--) {
str = ""
str += '<?xml version="1.0" encoding="' + charset + '"?>\n';
str += '<view-source-with version="1.1">\n';
str += ' <default-item-index>' +
'</default-item-index>\n';
str += ' <show-frame-warning>' +
+'</show-frame-warning>\n';
}
D1 = new Date()
J = K ; while (J--) {
str = '<?xml version="1.0" encoding="' + charset + '"?>\n'+
'<view-source-with version="1.1">\n'+
' <default-item-index>' +
'</default-item-index>\n'+
' <show-frame-warning>' +
+'</show-frame-warning>\n'
}
D2 = new Date()
J = K ; while (J--) {
str = ['<?xml version="1.0" encoding="' + charset + '"?>\n',
'<view-source-with version="1.1">\n',
' <default-item-index>' ,
'</default-item-index>\n',
' <show-frame-warning>' ,
+'</show-frame-warning>\n' ]
str.join("")
}
D3 = new Date()
X = [D3-D2, D2-D1, D1-D0] // RESULT typically 1210,660,930 for me.
That's reasonable, since the best way just says what is to be done,
without detailing a mechanism. Results may differ with other systems.
Results appear to be affected by the order in which the blocks are run
and (I guess) by optimisation that appears to introduce unpredictable
results.
Adopting the above order as C1, C2, C3, the following were achieved in
Firefox:
C1, C2, C3 => 1200, 360, 340
C1, C3, C2 => 313, 1359, 375
C3, C2, C1 => 360, 280, 1260
C2, C3, C1 => 375, 1310, 300
Results were similar in IE but two to three times faster. I have no
idea why say C3 takes four times longer when run second as opposed to
first - maybe some internal optimisation occurs that affects the
performance of individual blocks.
Below is a script that runs each block as a separate function. They can
be called in any order, the order still seems important but is less
significant than when all in one function. I guess each function should
be called using some random time displacement and setTimeout to make
them as independent as possible.
Times for Firefox 'plainConcat' are the same as for IE, but 'plusEquals'
and 'arrayJoin' take about twice as long. The following are examples of
the best (lowest) times achieved overall for various orders:
Order 1:
plainConcat: 94
plusEqual: 203
joinArray: 297
Order 2:
plainConcat: 94
joinArray: 546
plusEqual: 250
Order 3:
joinArray: 265
plusEqual: 157
plainConcat: 125
The variance for an individual method between each run is often greater
than that between each method: Jim's advice in his last paragraph is sound.
<script type="text/javascript">
var charset = "blah"
function plusEqual(n)
{
var str;
while (n--) {
str = ''
str += '<?xml version="1.0" encoding="';
str += charset + '"?>\n';
str += '<view-source-with version="1.1">\n';
str += ' <default-item-index>' + '</default-item-index>\n';
str += ' <show-frame-warning>' + '</show-frame-warning>\n';
}
return str;
}
function plainConcat(n)
{
var str;
while (n--) {
str = ''
+ '<?xml version="1.0" encoding="'
+ charset + '"?>\n'
+ '<view-source-with version="1.1">\n';
+ ' <default-item-index>' + '</default-item-index>\n';
+ ' <show-frame-warning>' + '</show-frame-warning>\n';
}
return str;
}
function joinArray(n)
{
var str;
while (n--) {
str = [
'<?xml version="1.0" encoding="',
charset,
'"?>\n',
'<view-source-with version="1.1">\n',
' <default-item-index>',
'</default-item-index>\n',
' <show-frame-warning>',
'</show-frame-warning>\n'
];
}
return str.join("")
}
function doInOrder()
{
var num = 10000;
var msg = [];
var st, ft;
for (var i=0, j = arguments.length; i<j; i++){
st = new Date();
window[arguments
](num);
ft = new Date();
msg = arguments + ': ' + (ft-st);
}
return msg;
}
</script>
<div id='xx'></div>
<script type="text/javascript">
var x = doInOrder(
'plainConcat'
,
'plusEqual'
,
'joinArray'
);
document.getElementById('xx').innerHTML = x.join('<br>');
</script>