NadAngel
10-15-2012, 02:30 PM
Here is an interesting benchmark test I have made to test the performance of two methods for building a comma delimited string.
An active demo is here:
http://benjaminpajk.host22.com/test1.html
<!doctype html>
<!--Author: Benjamin Pajk-->
<html lang="sl">
<head>
<meta charset="UTF-8">
<title>HTML5 Template</title>
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" value="IE=9">
<!--
<link rel="stylesheet" href="css/style.css">
-->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var numTests=50; //number of benchmark test samples
var numIterations=1048576; //one mega (10*1024*1024=1048576) iterations
var testString='';
var start;
var end;
var i;
var testArray=new Array();
function method(){
this.method1=-1;
this.method2=-1;
}
/*perform benchmark test for bulding comma delimited string*/
//method 1 checks if we need to insert the comma on every iteration
function method1(){
testString='';
start = new Date();
insertComma=false;
for(i=0; i<numIterations; i++){
if (insertComma){
testString+=',';
}else{
insertComma=true;
}
testString+='aa bb';
}
end = new Date();
$('#testResult1').val(end-start);
return end-start;
}
//method 2 inserts the comma on every iteration and removes the last comma when it finishes
function method2(){
testString='';
start = new Date();
for(i=0; i<numIterations; i++){
testString+='aa bb,';
}
testString=testString.slice(0,testString.length-1);
end = new Date();
$('#testResult2').val(end-start);
return end-start;
}
//test function (recursive)
function cmp(x){
if (x>0){
//recursive call with 500 ms delay
setTimeout(function(){cmp(x-1)}, 500);
var obj=new method();
obj.method1=method1();
obj.method2=method2();
testArray.push(obj);
$('#parallelTest').text('Testing: '+Math.round((1-x/numTests)*100)+' %');
}else{
//calculate the average
var sumArr1=0;
var sumArr2=0;
for(var k=0; k<testArray.length; k++){
sumArr1+=testArray[k].method1;
sumArr2+=testArray[k].method2;
}
//display average results
$('#avgResult1').val(sumArr1/testArray.length);
$('#avgResult2').val(sumArr2/testArray.length);
$('#testResult1').val('');
$('#testResult2').val('');
$('#parallelTest').text('Start parallel test');
}
}
$('#parallelTest').click(function(){
//reset the variables
testArray=[];
$('#avgResult1').val('');
$('#avgResult2').val('');
//call the benchmark test function
cmp(numTests);
});
});
</script>
</head>
<body>
<p>
This script tests the performace of two methods for building comma delimited strings. Each test iterates 1048576 (1024*1024) times and adds "aa bb" with a comma
delimeter on every iteration. The test is performed 50 times for both methods and the average time per test is calculated for both methods. The results are expressed
in milliseconds.<br>
</p>
<p>
Example: aa bb,aa bb,aa bb,aa bb,aa bb,aa bb<br>
</p>
<p>
<b>method 1:</b> checks if we need to insert the comma on every iteration<br>
<b>method 2:</b> inserts the comma on every iteration and removes the last comma when it finishes<br>
</p>
<p>
Method 1:<input type="text" id="testResult1" readonly="readonly" value="">ms<br>
Method 2:<input type="text" id="testResult2" readonly="readonly" value="">ms<br>
Method 1 (average):<input type="text" id="avgResult1" readonly="readonly" value="">ms<br>
Method 2 (average):<input type="text" id="avgResult2" readonly="readonly" value="">ms<br>
<button id="parallelTest">Start parallel test</button>
</p>
<p>
Preliminary results show, that IE9 is in favor of the first method while Chrome Firefox and Opera are in favour of the second method.<br>
Tested browsers:<br>
Internet Explorer 9.0.7<br>
Firefox 16.0<br>
Chrome (ver. 22.0.1229.94 m)<br>
Opera 12.02<br>
</p>
The main difference in browser performace is in the way the function <b>testString=testString.slice(0,testString.length-1);</b> is executed.
A good interpreter (Firefox, Opera, Chrome) will not actualy copy the string but will just delete the last cell of the existing string.
<p>
</p>
</body>
</html>
An active demo is here:
http://benjaminpajk.host22.com/test1.html
<!doctype html>
<!--Author: Benjamin Pajk-->
<html lang="sl">
<head>
<meta charset="UTF-8">
<title>HTML5 Template</title>
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" value="IE=9">
<!--
<link rel="stylesheet" href="css/style.css">
-->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var numTests=50; //number of benchmark test samples
var numIterations=1048576; //one mega (10*1024*1024=1048576) iterations
var testString='';
var start;
var end;
var i;
var testArray=new Array();
function method(){
this.method1=-1;
this.method2=-1;
}
/*perform benchmark test for bulding comma delimited string*/
//method 1 checks if we need to insert the comma on every iteration
function method1(){
testString='';
start = new Date();
insertComma=false;
for(i=0; i<numIterations; i++){
if (insertComma){
testString+=',';
}else{
insertComma=true;
}
testString+='aa bb';
}
end = new Date();
$('#testResult1').val(end-start);
return end-start;
}
//method 2 inserts the comma on every iteration and removes the last comma when it finishes
function method2(){
testString='';
start = new Date();
for(i=0; i<numIterations; i++){
testString+='aa bb,';
}
testString=testString.slice(0,testString.length-1);
end = new Date();
$('#testResult2').val(end-start);
return end-start;
}
//test function (recursive)
function cmp(x){
if (x>0){
//recursive call with 500 ms delay
setTimeout(function(){cmp(x-1)}, 500);
var obj=new method();
obj.method1=method1();
obj.method2=method2();
testArray.push(obj);
$('#parallelTest').text('Testing: '+Math.round((1-x/numTests)*100)+' %');
}else{
//calculate the average
var sumArr1=0;
var sumArr2=0;
for(var k=0; k<testArray.length; k++){
sumArr1+=testArray[k].method1;
sumArr2+=testArray[k].method2;
}
//display average results
$('#avgResult1').val(sumArr1/testArray.length);
$('#avgResult2').val(sumArr2/testArray.length);
$('#testResult1').val('');
$('#testResult2').val('');
$('#parallelTest').text('Start parallel test');
}
}
$('#parallelTest').click(function(){
//reset the variables
testArray=[];
$('#avgResult1').val('');
$('#avgResult2').val('');
//call the benchmark test function
cmp(numTests);
});
});
</script>
</head>
<body>
<p>
This script tests the performace of two methods for building comma delimited strings. Each test iterates 1048576 (1024*1024) times and adds "aa bb" with a comma
delimeter on every iteration. The test is performed 50 times for both methods and the average time per test is calculated for both methods. The results are expressed
in milliseconds.<br>
</p>
<p>
Example: aa bb,aa bb,aa bb,aa bb,aa bb,aa bb<br>
</p>
<p>
<b>method 1:</b> checks if we need to insert the comma on every iteration<br>
<b>method 2:</b> inserts the comma on every iteration and removes the last comma when it finishes<br>
</p>
<p>
Method 1:<input type="text" id="testResult1" readonly="readonly" value="">ms<br>
Method 2:<input type="text" id="testResult2" readonly="readonly" value="">ms<br>
Method 1 (average):<input type="text" id="avgResult1" readonly="readonly" value="">ms<br>
Method 2 (average):<input type="text" id="avgResult2" readonly="readonly" value="">ms<br>
<button id="parallelTest">Start parallel test</button>
</p>
<p>
Preliminary results show, that IE9 is in favor of the first method while Chrome Firefox and Opera are in favour of the second method.<br>
Tested browsers:<br>
Internet Explorer 9.0.7<br>
Firefox 16.0<br>
Chrome (ver. 22.0.1229.94 m)<br>
Opera 12.02<br>
</p>
The main difference in browser performace is in the way the function <b>testString=testString.slice(0,testString.length-1);</b> is executed.
A good interpreter (Firefox, Opera, Chrome) will not actualy copy the string but will just delete the last cell of the existing string.
<p>
</p>
</body>
</html>