# LSL MONO Loop Speeds

## Recommended Posts

So a little while back, while the forums were offline for the new site update, I had a question I wanted answered: what loops are fastest in mono, and what stipulations should be taken into account when scripting them?

As it turns out, this is quite an interesting topic that has a lot of commentators scratching their heads. Well, I'm not here to talk about efficiency, but I am here to definitively conclude which loops appear to be fastest (and when they usually are). Using a simple script that can be found below, I conducted some tests (in mono) to see what loops finish fastest with no arguments inside of them to test the true speed of the loop itself, from a practical approach without considering the theory behind it.

My results could be considered a little odd, as the answer of 'fastest' actually appears to vary based on how many iterations you happen to be looping through.

The results of my particular test are printed below, as concluded by the script found at the end of the page:

'for' loop time to 100: 0.000000
'for' loop time to 1,000: 0.024513
'for' loop time to 10,000: 0.044505
'for' loop time to 100,000: 0.596119
'for' loop time to 1,000,000: 6.318980

'do-while' loop time to 100: 0.000000
'do-while' loop time to 1,000: 0.030374
'do-while' loop time to 10,000: 0.114235
'do-while' loop time to 100,000: 1.198419
'do-while' loop time to 1,000,000: 12.739420

'while' loop time to 100: 0.000000
'while' loop time to 1,000: 0.014845
'while' loop time to 10,000: 0.116710
'while' loop time to 100,000: 1.200627
'while' loop time to 1,000,000: 12.317580

loop time to 100:

All loops evenly matched for practical purposes.

loop time to 1,000:

'while' loops beats all; 0.015529 seconds faster than 'do-while', and 0.009268 seconds faster than 'for'.

loop time to 10,000:

'for' loop beats all; 0.06973 seconds faster than 'do-while', and 0.072205 seconds faster than 'while'.

loop time to 100,000:

for loop beats all; 0.6023 seconds faster than 'do-while', and 0.604508 seconds faster than 'while'.

loop time to 1,000,000:

for loop beats all; 6.42044 seconds faster than 'do-while', and 5.9986 seconds faster than 'while'.

In simple conclusion, loops are a funny business and they can vary based on many probably factors. Please use the script to do the experiment yourself, as learning is always a personal endeavour!

I hope this was helpful at least to someone, and that it might lead to better scripting habits in the future for all. Have a good one!

```default
{

touch_start(integer total_number)
{

if(llDetectedKey(0) == llGetOwner())
{

llOwnerSay("Beginning loop test.\n ");

integer i = 0;

llResetTime();

for(i; i < 100; i += 1){}

llOwnerSay("'for' loop time to 100: "+(string)llGetTime());

i = 0;

llResetTime();

for(i; i < 1000; i += 1){}

llOwnerSay("'for' loop time to 1,000: "+(string)llGetTime());

i = 0;

llResetTime();

for(i; i < 10000; i += 1){}

llOwnerSay("'for' loop time to 10,000: "+(string)llGetTime());

i = 0;

llResetTime();

for(i; i < 100000; i += 1){}

llOwnerSay("'for' loop time to 100,000: "+(string)llGetTime());

i = 0;

llResetTime();

for(i; i < 1000000; i += 1){}

llOwnerSay("'for' loop time to 1,000,000: "+(string)llGetTime()+"\n ");

//-----------
llSleep(2.0);
//-----------

i = 0;

llResetTime();

do{i += 1;} while(i < 100);

llOwnerSay("'do-while' loop time to 100: "+(string)llGetTime());

i = 0;

llResetTime();

do{i += 1;} while(i < 1000);

llOwnerSay("'do-while' loop time to 1,000: "+(string)llGetTime());

i = 0;

llResetTime();

do{i += 1;} while(i < 10000);

llOwnerSay("'do-while' loop time to 10,000: "+(string)llGetTime());

i = 0;

llResetTime();

do{i += 1;} while(i < 100000);

llOwnerSay("'do-while' loop time to 100,000: "+(string)llGetTime());

i = 0;

llResetTime();

do{i += 1;} while(i < 1000000);

llOwnerSay("'do-while' loop time to 1,000,000: "+(string)llGetTime() + "\n ");

//-----------
llSleep(2.0);
//-----------

i = 0;

llResetTime();

while(i < 100){i += 1;}

llOwnerSay("'while' loop time to 100: "+(string)llGetTime());

i = 0;

llResetTime();

while(i < 1000){i += 1;}

llOwnerSay("'while' loop time to 1,000: "+(string)llGetTime());

i = 0;

llResetTime();

while(i < 10000){i += 1;}

llOwnerSay("'while' loop time to 10,000: "+(string)llGetTime());

i = 0;

llResetTime();

while(i < 100000){i += 1;}

llOwnerSay("'while' loop time to 100,000: "+(string)llGetTime());

i = 0;

llResetTime();

while(i < 1000000){i += 1;}

llOwnerSay("'while' loop time to 1,000,000: "+(string)llGetTime());

}

}
}```

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.