3 test cases varied the upper bounds index of the loop.
The idea was to compare how loop times differ between using different upper bounds types - constant, integer, and function.
In the case I tested, I used a list of 100 items, and did a simple if/else comparison on one member of the list as an iteration of the loop.
my output:
Loop Length Tester: Time test start.
Loop Length Tester: constant test:4.517586
Loop Length Tester: function time:9.194504
Loop Length Tester: integer time:4.697107
As you see, it is significantly longer to be checking llGetListLength() every time for the bounds check on the loop. It's much more efficient to either hard-code the loop length, or to set a variable to the llGetListLength() before the loop, and then use that integer variable as the bounds check.
CODE
list test = [];
default
{
state_entry()
{
integer c;
for(c=0;c<100;++c)
{
test += "blah blah";
}
}
touch_start(integer total_number)
{
llOwnerSay("Time test start.");
integer c;
float startTime;
float stopTime;
float dif;
startTime = llGetTime();
for(c=0;c<100;++c)
{
if("testing" == llList2String(test,c)) {llOwnerSay("This will never happen.");}
else{ }
}
stopTime = llGetTime();
dif = stopTime - startTime;
llOwnerSay("constant test:"+(string)dif);
startTime = llGetTime();
for(c=0;c<llGetListLength(test);++c)
{
if("testing" == llList2String(test,c)) {llOwnerSay("This will never happen.");}
else{ }
}
stopTime = llGetTime();
dif = stopTime - startTime;
llOwnerSay("function time:"+(string)dif);
startTime = llGetTime();
integer len = llGetListLength(test);
for(c=0;c<len;++c)
{
if("testing" == llList2String(test,c)) {llOwnerSay("This will never happen.");}
else{ }
}
stopTime = llGetTime();
dif = stopTime - startTime;
llOwnerSay("integer time:"+(string)dif);
}
}