Bueno, por si te sirve para algo, los bucles "for" y "while" suelen andar
bastante parejos de velocidad. El bucle "for...in", sin embargo, te permite
ganar algo, entre un 15% y un 30%, por las pruebas que he hecho. Pero esto
es algo bastante relativo, porque tipando las variables en los otros dos
bucles puedes reducir las diferencias.
Otra cosa:
for(var i=0; i < this.fields.length ; i++){
...
}
En cada iteración del bucle tiene que calcular el valor de
this.fields.length, y ahí pierde tiempo. Iría algo mejor con:
var cantidad:Number=this.fields.length;
for(var i:Number=0;i<cantidad;i++){
...
}
Venga, un saludo,
Carlos
-----Mensaje original-----
De: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] En nombre
de Ale Muñoz
Enviado el: miércoles, 13 de septiembre de 2006 22:03
Para: Lista dedicada a Actionscript
Asunto: Re: [ASNativos] Fast Array Search...
On 13/09/2006, at 19:52, Fede Rivas wrote:
> Buenas Ale !!
> Imagino que la estas recorriendo con un for, podrias almacenar el "i"
> facilmente en un array temporal y luego lo tiras con el return del
> metodo (
> se me ocurre asi a bote pronto ), devolviendote justo un array con las
> posiciones exactas donde tienes los strings.
Es justo lo que estoy haciendo :D
> Posteanos el code si quieres y vemos como tunearlo.
A verrrrrr...
Este es el código de la clase (en AS2 garrapatero : ), pero lo que
me interesa está en el método 'update':
class Searcher implements Observer extends Observable {
var fields:Array;
var results:Array;
var cache;
function Searcher(fields:Array){
this.fields = fields;
clearResults();
this.cache = fields.join(",").toUpperCase();
}
function toString(){
return "Searcher Object";
}
function update(w:Observable,o:Object){
var what = o.txt.toUpperCase();
trace( "Will search for " + what);
clearResults();
// First check if it is there...
var matched = this.match(what,this.cache);
if(matched.status){
trace("The string is there, beginning at " +
matched.position);
// ...then search for the item that contains it...
for(var i=0; i < this.fields.length ; i++){
var current_item =
this.fields[i].join(",").toUpperCase();
trace("Searching for " + what + " in " +
current_item);
if(this.match(what,current_item).status){
this.addResult(i);
}
}
} else {
trace("The string was not found...");
}
setChanged();
notifyObservers(this.results);
}
function match(what,where){
var isThere:Number = where.indexOf(what);
if(isThere == -1){
return {status: false, position: isThere};
} else {
return {status: true, position: isThere};
}
}
function addResult(r){
trace("Adding result " + r + " to list");
this.results.push(r);
}
function clearResults(){
this.results = [];
}
function getData(row:Number){
return fields[row];
}
}
Todavía no está tuneada, pero me interesa más saber si hay
alternativas más rápidas al clásico 'for' (o alguna idea feliz, como
la de convertir el Array en un String, cachearlo, y hacer búsquedas
con indexOf, que es bastante más rápido que recorrer el Array...)
> ¿ Es mu tocho el array en el que buscas ?
Es una mierdecilla de Array (una matriz de 2x12 o así). El problema
es que se ejecuta en una plataforma (Symbian S60) que no es que
vaya muy sobrada de potencia, precisamente :D
Gracias por los comentarios!
Ale Muñoz
[EMAIL PROTECTED]
blog » http://sofanaranja.com
-----------------------------------------------------
ASNativos
www.5dms.com
subscripciones/desubscripciones
http://asnativos.5dms.com
-----------------------------------------------------
-----------------------------------------------------
ASNativos
www.5dms.com
subscripciones/desubscripciones
http://asnativos.5dms.com
-----------------------------------------------------