llSubStringIndex, parsing and efficiency
|
|
illovich Psaltery
Registered User
Join date: 30 Sep 2004
Posts: 10
|
04-04-2008 18:11
Hey all, I'm a bit of a LSL newb (but I have a lot of experience being a newb in lots of other languages!), and I had a quick question about scripting efficiency, more for later than for now, but anyway... For a script I'm writing, I needed to parse the first and last name of the avatar who touches a button. This was pretty easy to figure out: From: someone
integer nameSplitIndex;
string oneSpace = " "; string inString;
string avatarFirstName = " "; string avatarLastName = " ";
default {
touch_start(integer total_number) { inString = llDetectedName(0); nameSplitIndex = llSubStringIndex( inString, oneSpace ); avatarFirstName = llGetSubString( inString, 0, nameSplitIndex ); avatarLastName = llGetSubString( inString, nameSplitIndex, -1 ); llSay(0, "first name: " + avatarFirstName + " , " + "lastname: " + avatarLastName);
} }
But I was curious if that was the best way to do it. For example, I thought I could get rid of the inString step and that would be better. so now I have From: someone
nameSplitIndex = llSubStringIndex( llDetectedName(0), oneSpace ); avatarFirstName = llGetSubString( llDetectedName(0), 0, nameSplitIndex ); avatarLastName = llGetSubString( llDetectedName(0), nameSplitIndex, -1 );
which saves me a string. For efficiency's sake, is it better to use functions like that, and maybe literals ( " " vs. oneSpace )? I feel like it's better to use variables for more readable code, but I was curious how I should be planning to mold my scripting habits. Anyway, thanks for any input. Also, is there a bbCode for formatting the code output active on the forum? ill
|
|
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
|
04-04-2008 18:16
I always use literals for simple things like a space, for example, in 'llParseString2List(message, [" "], []);' I would also likely declare a string variable in the event to store the result of llDetectedName, because I believe it's a little faster that way, and should cost less memory. Lastly, don't you need to use the index of that space - 1 or + 1 for the first and last name respectively? At least, if you want to get rid of the space itself in those strings. You can use the or tags, and I recommend doing so, but it won't show up normally. To get it to format properly (since bbCode has been disabled here), I suggest installing the GreaseMonkey addon for FireFox and downloading the bbCode formatting script made by Strife Onizuka, I believe it's here: http://userscripts.org/scripts/show/9651.
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
04-04-2008 19:34
You save the space for a string, but you end up calling a function 3 times. Usually, that's not a great trade-off. In SL things get weird though, because the built-in functions sometimes run faster than a few lines of code in a script, so... who knows  You could try and benchmark it. Put that code in a loop and run it 100,000 times and measure how long it takes (add some calls before and after it to get timestamps, diff, print result). Then do it the other way and measure that. Repeat both several times and see if you get a consistent difference.
|
|
illovich Psaltery
Registered User
Join date: 30 Sep 2004
Posts: 10
|
correct
04-04-2008 20:23
From: Tyken Hightower ...don't you need to use the index of that space - 1 or + 1 for the first and last name respectively? At least, if you want to get rid of the space itself in those strings.
You are correct, thanks for the catch. I didn't notice the double space in the output. From: Ziggy Puff You could try and benchmark it. Put that code in a loop and run it 100,000 times and measure how long it takes (add some calls before and after it to get timestamps, diff, print result).
Sorry, I have a bad head for prefiguring how brutal code will be on a system -- is 100,000 times a bit excessive? I realize it might be less than a second to compute, but on the other hand I just don't want to be the one who brought down my sim. I'm already afraid the neighbors don't like me. Thanks for your replies!
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
04-05-2008 09:19
Heh, good point, maybe start with a smaller number of iterations  Ideally you'd want it to run for several seconds so your numbers are more meaningful, but you're right, it probably doesn't need to run for hours 
|
|
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
|
04-05-2008 10:04
From: illovich Psaltery You are correct, thanks for the catch. I didn't notice the double space in the output. For situations like this, in which I want to omit the delimiting character, I'd use llDeleteSubString, rather than llGetSubstring and the additional math to offset the index: avatarFirstName = llDeleteSubString( inString, nameSplitIndex, -1 ); avatarLastName = llDeleteSubString( inString, 0, nameSplitIndex );
|
|
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
|
04-05-2008 10:47
Personally, I'd just use llParseString2List. It's likely to be comparable in cost, and saves the headache of dealing with the off-by-1 errors.
|
|
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
|
04-07-2008 02:44
From: Kidd Krasner Personally, I'd just use llParseString2List. It's likely to be comparable in cost, and saves the headache of dealing with the off-by-1 errors. No doubt.
|