|
-
I have noticed that flash seems to be very slow at splitting a long string into an array. Anyone else noticed that?
I am pulling a list of files into flash using SWF Studio and into the flash varible called ssfilelist. ssFileList would then look something like this: ("myfile1.exe,myfile2.exe,myfile3.exe,etc...") for 80 files.
myfilearray = _parent.ssFileList.split(",");
The above code takes nearly 15 seconds to run with 80 files in it. Worse of all, it stops everything while is splits the string into an array. All movies stop.
Anyone know of a faster way to do it?
I am using a win 98SE on a p400.
BTW, i have split the code out into different frames and SWF studio grabs the files very fast, it is when it gets to splitting the string into an array that everything stops.
Jim
-
Lifetime Friend of Site Staff
This code was posted in the Flash 5 Actionscript forum by musicman (I think). It adds a new method called mysplit to the String class which is many times faster than the normal Split method.
Code:
String.prototype.mysplit = function(ch)
{
var k, s, l = this.length;
var res = new Array();
for(k = s = 1 ; k <= l ; k++) {
if(substring(this,k,1) == ch) {
res.push(substring(this, s, k-s));
s = k+1;
}
}
if(s <= l)
res.push(substring(this, s, l-s+1));
return res;
};
I wrote my own version of this function and it ended up looking VERY similar. I played around with some of the other string functions to see which ones were the fastest but ended up using the same ones as the original author.
Code:
function strsplit(s, d)
{
var i, n=1, l=s.length;
var a = new Array();
for (i=1; i<=l; i++) {
if (substring(s, i, 1) == d) {
a.push(substring(s, n, i-n));
n = i+1;
}
}
if (n<=l) {
a.push(substring(s, n, l-n+1));
}
return a;
}
The only real difference between these two functions is that one (mysplit) extends the string class and the other (strsplit) is a stand alone function. So how do they compare?
I've included some code (below) that you can use to compare these two approaches by measuring the amount of time required to split different numbers of strings. Here are the results on my PIII 1GHz 512MB on Win2K
250 strings
787 msec for mysplit
639 msec for strsplit
500 strings
4901 msec for mysplit
4559 msec for strsplit
Since the code is almost identical, it would appear that a little extra overhead is added by involving the string class. If you want maximum performance (and losing that extra 1/2 sec bugs you ) then don't extend the string class.
Code:
s = "";
c = "";
for (i=0; i<250; i++) {
s = s + c + i + " just a string";
c = ",";
}
t1 = getTimer();
var a = s.mysplit(",")
t2 = getTimer();
trace(a.length);
trace(t2-t1 add " msec for mysplit");
t1 = getTimer();
var a = strsplit(s, ",")
t2 = getTimer();
trace(a.length);
trace(t2-t1 add " msec for strsplit");
-
thanks Northcode!
And what is more important is that your code is at least 10 times faster than the split a string into an array function provided in flash.
-
Lifetime Friend of Site Staff
It turns out that manipulating the array directly is 2 or 3 times faster than using the push() method. Even though the use of the push method is minimized, this version of strsplit is a wee bit faster than the last one.
for 100 items...
200 msec for mysplit
146 msec for strsplit1
138 msec for strsplit2
Code:
function strsplit2(s, d)
{
var i, n=1, l=s.length;
var a = new Array();
for (i=1; i<=l; i++) {
if (substring(s, i, 1) == d) {
a[a.length] = substring(s, n, i-n);
n = i+1;
}
}
if (n<=l) {
a[a.length] = substring(s, n, l-n+1);
}
return a;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|