Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

benchmark: loop test - integer vs. constant vs. llGetListLength()

Hiro Pendragon
bye bye f0rums!
Join date: 22 Jan 2004
Posts: 5,905
03-06-2006 09:00
I ran a benchmark on FOR loops.

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:
From: 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);

}
}
_____________________
Hiro Pendragon
------------------
http://www.involve3d.com - Involve - Metaverse / Emerging Media Studio

Visit my SL blog: http://secondtense.blogspot.com
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
03-06-2006 23:57
I hadn't mentioned this in my optimization thread... i sorta thought it went without saying. But good to point out.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey