Results 1 to 5 of 5

Thread: LScript execution speed question...

  1. #1
    Registered User sami's Avatar
    Join Date
    Jul 2003
    Location
    TopLeft Viewport
    Posts
    994

    Cool LScript execution speed question...

    Please forgive me if this is another of my fringe/esoteric questions to some LScripters here, but in the event I can learn more from others, I don't mind asking.

    How much of a penalty processing/time-wise is it to access an element from a nested array versus a flat global? So in this example I have an array of Thingy objects which one of its members is a collection of Widgets and then each Widget has its own properties - such as color. So the value I might want to access could look like this (or it might even be deeper nested):

    Code:
    myThingy[whatIndex][WIDGETS][whatWidgetIndex][WIDGET_COLOR] = <100,10,10>;
    Does my logical assembly of objects in this manner cause any significant [read: noticeable] slow-down to access the value? I have many "objects" like this and don't notice any major problems, I think the slow areas I find are mostly in drawing and requpdate()'s. But I was wondering if anyone with deeper programming or LScript knowledge might know if this is a bad idea performance-wise?



    Also, lets say I operate on something this deep in a loop, is it better to assign it to a temp flat variable first or is it ok to access it repetitively in a loop?

    Like
    Code:
    for (i=1; i<myThingy[whatIndex][WIDGETS].size();i++)
    {
        myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR].x =  min(myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR].x + 50, 255);
        myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR].y =  min(myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR].y + 50, 255);
        myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR].z =  min(myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR].z + 50, 255);
    }

    versus


    Code:
    var totalThingCount = myThingy[whatIndex][WIDGETS].size();
    var oldColor, redBoost, greenBoost, blueBoost;
    
    for (i=1; i<totalThingCount; i++)
    {
        oldColor = myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR];
    
        redBoost = oldColor.x;
        redBoost = min(redBoost + 50, 255);
    
        greenBoost = oldColor.y;
        greenBoost = min(greenBoost + 50, 255);
    
        blueBoost = oldColor.z;
        blueBoost = min(blueBoost+ 50, 255);
    
        myThingy[whatIndex][WIDGETS][i][WIDGET_COLOR] = <redBoost, greenBoost, blueBoost>;
    }


    I'm wondering if either of these is significantly faster? And while I'm at it, is there any hit to declare a variable inside a loop (for scoping reasons) like in this case if I put the var redBoost inside the loop?
    thx

  2. #2
    Hi,

    I've noticed some performance hit with variables declarations, the less you have, the faster it will run

  3. #3
    Registered User sami's Avatar
    Join Date
    Jul 2003
    Location
    TopLeft Viewport
    Posts
    994
    Quote Originally Posted by Alexx View Post
    Hi,

    I've noticed some performance hit with variables declarations, the less you have, the faster it will run
    Thanks - but could you clarify? Do you mean don't use var somevariable; inside loops? Or are you also talking about nested arrays like I mention?

  4. #4
    Hi,

    I don't know about nested arrays, but for variables declaration, it's clearly a slowdown source, avoid their repetition as much as possible.
    For functions called in tight loops, use global variables.
    For loops inside functions, put the declarations outside of the loop.

  5. #5
    Registered User sami's Avatar
    Join Date
    Jul 2003
    Location
    TopLeft Viewport
    Posts
    994
    thanks - good to know, I'll keep an eye out when I optimize...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •