Jump to content

nested if? or logical operators?


Xander Lopez
 Share

You are about to reply to a thread that has been inactive for 1388 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Which is faster and more efficient? I have tons of the following code, so I thought I would make up my mind

case1

        if(press ==  nametag1) llSay(0,"I found " + (string)press);
        else if(press ==  nametag2) llSay(0,"I found " + (string)press);
        else if(press ==  nametag3) llSay(0,"I found " + (string)press);
 

vs...

case2

if (press == nametag1 || press == nametag2 || press == nametag3) llSay(0,"I found " + (string)press);

Link to comment
Share on other sites

Theoretically speaking, both could be faster. It depends on how the compiler translates it.

Case 2 could be slower because we know for a fact that conditions are fully evaluated in LSL. if "press == nametag1" is true, the other two are still checked.

The if-else chain could create a "jump table" or even optimize the order of the conditions so that the "most common case" is assumed before any checks, but I don't think we know how the LSL compiler handles these. I don't think we even have any way to access the compiler either, since scripts are compiled server-side.

I'm of the opinion that things like this is not something you should focus on, because the decision to use case 1 or case 2 is not going to be noticeable at all, compared to the readability and what the code implies. If you have 3 different cases that all lead to the same result, you shouldn't separate them like in case 1. That's one way to set yourself up for bugs and duplicated code. (And since we're on the topic of micro-optimizations, that duplicate code in case 1 is going to cost you some memory.)

But it would be interesting to know from a technical standpoint.

Edited by Wulfie Reanimator
  • Like 1
Link to comment
Share on other sites

One way to handle logical large conditions would be compare to a  list and avoid the long if else statements (or case switch using #define USE_SWITCHES in FS) and avoid the duplicated code, like this sample:

string press= "Nerd";

list listNameTags= ["Sweet", "Beauty", "Nerd", "Fun"];

integer bPositive= FALSE;
integer i= 0;        
while(!bPositive && i<llGetListLength( listNameTags))
{
    if ( press==llList2String( listNameTags, i))
        bPositive= TRUE;
    i++;
}

if (bPositive)
    llSay( 0, "I found "+ press);
else
    llSay( 0, "I found nothing");

 

In case of more complex lists, usage of strided list and optional JSON can be clever solutions using the above. In this case though, your attention should be on the memory usage.

Edited by Rachel1206
Link to comment
Share on other sites

This works too:

searchfor = ["nametag1","nametag2","nametag3"];

if (~llListFindList(searchfor,[press])) llSay(0,"found "+press); else llSay(0,"not found");

I stumble about the question about speed. How many times per second you want to do this? 😁
However, enclose the solutions in a loop from 1 to 100000, get time b4 loop and after loop and show the difference. Then you get the answer about speed.

Link to comment
Share on other sites

In terms of speed, llListFindList() is on avg. 6x - 7x faster than llJsonGetValue() with the same sized/dimension list and array.

Hardcoded lists use a slightly higher memory footprint on query than the unwrapping/expanding caused by llJsonGetValue() reading a hardcoded json string.

Pick your poison.

Edited by Lucia Nightfire
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1388 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...