Difference between revisions of "Category talk:LSL List"
(On Concatenation) |
|||
Line 39: | Line 39: | ||
I would really just like some documentation on this page about how to index into a list at all. Thanks - [[User:Kendown Baroque|Kendown Baroque]] 00:48, 28 August 2007 (PDT) | I would really just like some documentation on this page about how to index into a list at all. Thanks - [[User:Kendown Baroque|Kendown Baroque]] 00:48, 28 August 2007 (PDT) | ||
== On Concatenation == | |||
I have read in many places that there are two ways to concatenate lists, the "standard" way and a memory-saving "voodoo" technique. The standard way looks like this: | |||
{{Box Code|"Standard" List Concatenation|<pre> | |||
list myList = ["element1"]; | |||
myList += ["element2"]; | |||
</pre>}} | |||
The "voodoo" technique looks like this: | |||
{{Box Code|"Voodoo" List Concatenation|<pre> | |||
list myList = ["element1"]; | |||
myList = (myList=[]) + mylist + ["element2"]; | |||
</pre>}} | |||
So, being a bit of a computer scientist, I decided to give it a test. Here's my test code: | |||
{{Box Code|Test Code - List Concat Standard|<pre> | |||
integer REPS = 30; | |||
list myList; | |||
integer i; | |||
default { // INIT | |||
state_entry() { | |||
llOwnerSay("Testing memory usage..."); | |||
myList = []; | |||
for (i = 0; i < REPS; ++i) { | |||
myList += ["test3.0", "test3.1", "test3.2", "test3.3", "test3.4", "test3.5", "test3.6", "test3.7", "test3.8", "test3.9"]; | |||
} | |||
llOwnerSay("List Standard Short Format Post-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); | |||
myList = []; | |||
for (i = 0; i < REPS; ++i) { | |||
myList = ["test3.0", "test3.1", "test3.2", "test3.3", "test3.4", "test3.5", "test3.6", "test3.7", "test3.8", "test3.9"] + myList; | |||
} | |||
llOwnerSay("List Standard Long Format Pre-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); | |||
} | |||
} | |||
</pre>}} | |||
Output: | |||
* Testing memory usage... | |||
* List Standard Short Format Post-Concat 10*30 compound: 4261 | |||
* List Standard Long Format Pre-Concat 10*30 compound: 4261 | |||
{{Box Code|Test Code - List Concat Voodoo|<pre> | |||
integer REPS = 30; | |||
list myList; | |||
integer i; | |||
default { // INIT | |||
state_entry() { | |||
llOwnerSay("Testing memory usage..."); | |||
myList = []; | |||
for (i = 0; i < REPS; ++i) { | |||
myList = (myList=[]) + myList + ["test4.0", "test4.1", "test4.2", "test4.3", "test4.4", "test4.5", "test4.6", "test4.7", "test4.8", "test4.9"]; | |||
} | |||
llOwnerSay("List Voodoo Post-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); | |||
myList = []; | |||
for (i = 0; i < REPS; ++i) { | |||
myList = (myList=[]) + ["test4.0", "test4.1", "test4.2", "test4.3", "test4.4", "test4.5", "test4.6", "test4.7", "test4.8", "test4.9"] + myList; | |||
} | |||
llOwnerSay("List Voodoo Pre-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); | |||
} | |||
} | |||
</pre>}} | |||
Output: | |||
* Testing memory usage... | |||
* List Voodoo Post-Concat 10*30 compound: 9795 | |||
* List Voodoo Pre-Concat 10*30 compound: 9739 | |||
=== Results === | |||
Interesting results... A gain of 5534 to 5478 bytes of memory for these tests. | |||
Remember, bigger numbers mean more free memory, therefore less memory being used. | |||
[[User:Cron Stardust|Cron Stardust]] 00:14, 31 December 2007 (PST) |
Revision as of 00:14, 31 December 2007
I want to document the fact that the following two expressions are NOT the same somehow.
[1] list_var += [(string)key_var]; [2] list_var += (string)key_var;
Why does this matter? why consider the following line of code:
index = llListFindList(list_var, [(string)key_var] );
If method [1] is used before the line above,you will find your value, however if you use method [2], you will not.
After much frustration I wanted to share this in hope someone else will not fall into the pit, or if it is a bug, it can be put in Jira. I'll let one of the Wiki gods decide what to do with this.
--Awsoonn Rawley
- I'll look into it, this sounds like a bug. Those two expressions should be equivalent. -- Strife Onizuka 18:26, 3 June 2007 (PDT)
- I've done some testing and I cannot replicate your problem. Are you aware that indexing starts at zero and not one? -- Strife Onizuka 15:56, 5 June 2007 (PDT)
- try this --Awsoonn Rawley 04:35, 6 June 2007 (PDT)
default { state_entry() { list lst; key uuid = llGetKey(); lst = ["bob", "tom", "jerry"]; lst += (string)uuid; llSay(0, (string)llListFindList(lst, [(string)uuid]) ); } }
I would really just like some documentation on this page about how to index into a list at all. Thanks - Kendown Baroque 00:48, 28 August 2007 (PDT)
On Concatenation
I have read in many places that there are two ways to concatenate lists, the "standard" way and a memory-saving "voodoo" technique. The standard way looks like this:
Code: "Standard" List Concatenation |
list myList = ["element1"]; myList += ["element2"]; |
The "voodoo" technique looks like this:
Code: "Voodoo" List Concatenation |
list myList = ["element1"]; myList = (myList=[]) + mylist + ["element2"]; |
So, being a bit of a computer scientist, I decided to give it a test. Here's my test code:
Code: Test Code - List Concat Standard |
integer REPS = 30; list myList; integer i; default { // INIT state_entry() { llOwnerSay("Testing memory usage..."); myList = []; for (i = 0; i < REPS; ++i) { myList += ["test3.0", "test3.1", "test3.2", "test3.3", "test3.4", "test3.5", "test3.6", "test3.7", "test3.8", "test3.9"]; } llOwnerSay("List Standard Short Format Post-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); myList = []; for (i = 0; i < REPS; ++i) { myList = ["test3.0", "test3.1", "test3.2", "test3.3", "test3.4", "test3.5", "test3.6", "test3.7", "test3.8", "test3.9"] + myList; } llOwnerSay("List Standard Long Format Pre-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); } } |
Output:
- Testing memory usage...
- List Standard Short Format Post-Concat 10*30 compound: 4261
- List Standard Long Format Pre-Concat 10*30 compound: 4261
Code: Test Code - List Concat Voodoo |
integer REPS = 30; list myList; integer i; default { // INIT state_entry() { llOwnerSay("Testing memory usage..."); myList = []; for (i = 0; i < REPS; ++i) { myList = (myList=[]) + myList + ["test4.0", "test4.1", "test4.2", "test4.3", "test4.4", "test4.5", "test4.6", "test4.7", "test4.8", "test4.9"]; } llOwnerSay("List Voodoo Post-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); myList = []; for (i = 0; i < REPS; ++i) { myList = (myList=[]) + ["test4.0", "test4.1", "test4.2", "test4.3", "test4.4", "test4.5", "test4.6", "test4.7", "test4.8", "test4.9"] + myList; } llOwnerSay("List Voodoo Pre-Concat 10*" + (string) REPS + " compound: " + (string) llGetFreeMemory()); } } |
Output:
- Testing memory usage...
- List Voodoo Post-Concat 10*30 compound: 9795
- List Voodoo Pre-Concat 10*30 compound: 9739
Results
Interesting results... A gain of 5534 to 5478 bytes of memory for these tests.
Remember, bigger numbers mean more free memory, therefore less memory being used.
Cron Stardust 00:14, 31 December 2007 (PST)