Difference between revisions of "LSL Script Efficiency/es"

From Second Life Wiki
Jump to navigation Jump to search
Line 24: Line 24:
<div id="box">
<div id="box">


== Diseño eficaz ==
== Diseño eficiente ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">


Each idle script in an object adds 0.001 to 0.003 milliseconds per frame of script time usage.  Fewer scripts is better.
Cada script inactivo en un objecto anade de 0.001 a 0.003 milliseconds per frame of script time usage.  Fewer scripts is better.


* If you need to have a bunch of "buttons", don't put a script in each prim.  Use [[llDetectedLinkNumber]] to detect which button was touched from your main script instead.   
* If you need to have a bunch of "buttons", don't put a script in each prim.  Use [[llDetectedLinkNumber]] to detect which button was touched from your main script instead.   

Revision as of 09:16, 6 March 2008

Qué es la eficiencia

La eficiencia es la cantidad de recursos que un script específico usa para conseguir una meta. La eficiencia va de la mano con la velocidad de ejecución pero no siempre.

Este atento de los siguientes puntos, en orden de importancia aproximado:

  • Usar muchos scripts para lograr algo. Consulte Diseño Eficiente.
  • llSleep - Esta función usa de 0.2 a 0.3 ms por marco mientras que el script duerme, los temporizadores son mejores.
  • Temporizador de frequecia corta (<5 sec)
  • Escuchar - especialmente al canal 0
  • Cambiar texturas con frecuencia (Otros tienen que descargarlos)
  • Uso poco apropiado de rotaciones físicas/de servidor en vez de omega
  • Manejadores de eventos que no son necesarios (tocar, colisión, ...)
  • Cantidad grande de correo electrónico o mensajes instantáneos
  • Algoritmos ineficaces (como la búsqueda lineal)
  • Bucles de llamada selectiva ocupadas

Diseño eficiente

Cada script inactivo en un objecto anade de 0.001 a 0.003 milliseconds per frame of script time usage. Fewer scripts is better.

  • If you need to have a bunch of "buttons", don't put a script in each prim. Use llDetectedLinkNumber to detect which button was touched from your main script instead.
  • If you need to change color, alpha, or texture of child prims, do not put a script in each prim. Instead use llSetLinkAlpha, llSetLinkColor, llSetLinkTexture, or llSetLinkPrimitiveParams.
  • Never put a script in each prim (of a large linkset) that listens on channel 0 (or any other channel). This is probably the worst thing you can do for efficiency. Unfortunately it is all too common.
  • Consider using XyzzyText instead of XyText.

The bottom line is, if you find yourself tempted to put a script in each prim, stop and think about how you could do it differently with fewer scripts. There is almost always an alternative. It is up to you to decide whether the alternative is a viable one for your application.

Micro-optimización

There are many ways to speed up scripts, such as using ++a instead of a++, however, most of these micro optimizations might not hold true in the future.


Cuán Rápido Corre el Código

The following code will get the time for each function call in millis.

Please first propose changes to the discussion tab. Please do not change the code here without discussion, as any small change could reduce the accuracy of measurements. Please note this code and its comments should exactly match the code and comments presented by the Efficiency Tester page.

Thanks to Xaviar Czervik for the original code, thanks to Strife Onizuka for tuning the code to produce more accurate measurements, and thanks to the other contributors listed in the history of this article.

//IMPORTANT: Only perform tests in an empty region.
// To reduce contamination and be sure to wearing no attachments.
// Preferably do tests in a private sim with one on it.
// Don't move while performing the test.
// There is a margin of error so run the tests multiple times to determine it.

integer time() { // count milliseconds since the day began
    string stamp = llGetTimestamp(); // "YYYY-MM-DDThh:mm:ss.ff..fZ"
    return (integer) llGetSubString(stamp, 11, 12) * 3600000 + // hh
           (integer) llGetSubString(stamp, 14, 15) * 60000 +  // mm
           llRound((float)llGetSubString(stamp, 17, -2) * 1000000.0)/1000; // ss.ff..f
}

default {
  state_entry() {
    llOwnerSay((string) llGetFreeMemory());

    //test variables
    float counter;

    //framework variables
    float i = 0;
    float j = 0;
    float max = 10000; // 2ms of work takes 20 seconds to repeat 10,000 times, plus overhead

    float t0 = time();
    do {

      //test
      counter += 1;
      
    }while (++i < max);
    float t1 = time();
    do ; while (++j < max);
    float t2 = time();//remove the time required by the framework
    float elapsed = ((t1 - t0) - (t2 - t1))/max;
    llOwnerSay("The function in the loop took a total of " + (string)elapsed + " milliseconds.");
  }
}

Eficiencia

The following data was collected using the above function in a recently deployed empty (though not private) sim, with only that one script running in a HUD attachment. 20 tests of each were run and averaged.

  ++a:     0.173780 ms    std. dev.:  0.003393 ms
  a += 1:  0.181720 ms    std. dev.:  0.013267 ms
  a++:     0.243500 ms    std. dev.:  0.013816 ms

Thus a++ takes 40% longer to execute than ++a (rough estimate).

While the following data is correct, the function above was not used. A slightly less optimized version written by Xaviar Czervik was used. If someone could please repeat the tests with the function above.

  ++a:     0.364700 millis
  a += 1:  0.346900 millis
  a++:     0.413700 millis

Probando la misma funcion en bucles for (for loops):

  ++a:     0.358370 millis
  a += 1:  0.351200 millis
  a++:     0.424600 millis

llOwnerSay v. llSay v. llShout v. llWhisper (Canal 0 donde corresponda):

  llOwnerSay(): 4.359000 millis
  llWhisper():  5.201000 millis
  llSay():      5.226000 millis
  llShout():   14.877000 millis

Diferentes Canales (llSay() Usado para todo):

  -100000000: 1.226400 millis
  -100000:    1.254300 millis
  -100:       1.296100 millis
  -1:         1.292400 millis
  0:          5.226000 millis
  1:          1.242300 millis
  100:        1.249100 millis
  100000:     1.219700 millis
  100000000:  1.228700 millis

Cantidad de texto (llSay() y Canal 1 usado para todo):

   1 Character:    1.242300 millis
   10 Characters:  1.309700 millis
   100 Characters: 1.965600 millis