kentyler
04-26-2003, 02:55 PM
In the ColdFusion docs it says that sizing an array to a known number of records can improve performance, especially if there are more than 500 elements in the array.
Do you think this would hold in laszlo as well ?
You can try running some tests yourself. Here's
some example code which uses a timer to test the
speed of a quicksort vs. the builtin Flash sort
routine. You could change this to test the speed of allocating and filling an array with,say, 10000 elements.
<canvas debug="true" width="1000" height="800">
<script>
<![CDATA[
Array.prototype.quicksort_1 = function (a, l,r) {
var i=l, j=r, w, x=a[Math.floor((l+r)/2)];
do {
while(a[i] < x) i++;
while(x < a[j]) j--;
if(i<=j){
w = a[i];
a[i++] = a[j];
a[j--] = w;
}
} while(i <= j);
if(l < j) this.quicksort_1(a, l,j);
if(i < r) this.quicksort_1(a, i, r);
}
Array.prototype.quicksort = function () {
this.quicksort_1(this, 0, this.length-1);
}
]]>
</script>
<view bgcolor="#cccccc" width="200" height="400" name="v" oninit="sortstuff()" >
<simplelayout/>
<text bgcolor="#ffcccc" id="timer" multiline="false" height="120" selectable="true"></text>
<button onclick="parent.doquicksort('quicksort 5', 5)">Quicksort 5</button>
<button onclick="parent.doquicksort('quicksort 10', 10)">Quicksort 10</button>
<button onclick="parent.doquicksort('quicksort 100', 100)">Quicksort 100</button>
<button onclick="parent.doquicksort('quicksort 1K', 1000)">Quicksort 1k</button>
<button onclick="parent.doquicksort('quicksort 2K', 2000)">Quicksort 2k</button>
<button onclick="parent.doquicksort('quicksort 4K', 4000)">Quicksort 4k</button>
<button onclick="parent.doquicksort('quicksort 8K', 8000)">Quicksort 8k</button>
<button onclick="parent.dosort('flash sort 5', 5)">Flash Sort 5</button>
<button onclick="parent.dosort('flash sort 10', 10)">Flash Sort 10</button>
<button onclick="parent.dosort('flash sort 100', 100)">Flash Sort 100</button>
<button onclick="parent.dosort('flash sort 1K', 1000)">Flash Sort 1k</button>
<button onclick="parent.dosort('flash sort 2K',2000)">Flash Sort 2k</button>
<button onclick="parent.dosort('flash sort 4K',4000)">Flash Sort 4k</button>
<button onclick="parent.dosort('flash sort 8K',8000)">Flash Sort 8k</button>
<method name="dosort" args="msg, n">
<![CDATA[
var i;
this.a = new Array();
for (i=0; i < n; i++) {
a[i] = n-i;
}
var start = getTimer();
a.sort();
debug.write( msg + ": " + (getTimer() - start));
]]>
</method>
<method name="doquicksort" args="msg, n">
<![CDATA[
var i;
this. a = new Array();
for (i=0; i < n; i++) {
a[i] = n-i;
}
var start = getTimer();
a.quicksort();
debug.write( msg + ": " + (getTimer() - start) );
]]>
</method>
<text>__________________________</text>
<text multiline="false" bgcolor="#cccccc" width="200" height="400" id="txt"/>
<method name="sortstuff">
<![CDATA[
var a = ['foo', 'bar', 'baz', 'apple', 'banana', 'bandana', 'zebra', 'alt'];
a.quicksort();
var i;
var s = "";
for (i = 0; i < a.length; i++) {
s += a[i] + "\n";
}
txt.settext(s);
]]>
</method>
</view>
</canvas>
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.