I have also outlined ways of using the more efficient types in your scripts.
The one thing to remember is that certain data types are a necessity at times and you don't really have a choice as to when to use them. By these I am referring to the vector and rotation types. The list type is also, at times, a necessity, but there are times when a list is not necessarily required. As such I only tested the integer, float, string and list types. The following list orders these types into their efficiency and the likely explanation why:
- Integer: Most efficient. This is due to the fact that integers use the smallest amount of memory per variable as well as the fact that calculating whole numbers is a lot quicker for a CPU than any other type.
- Float: Float types should be avoided where integers can do the job instead. Floats tend to require more memory than integers and calculation of floats is more CPU intensive than an integer type.
- String: Because a string is made up of characters, and each character consuming 16 bits, these can be large memory hogs. Also, when strings are concatenated (joining two strings together), the normal procedure for processing a concatenation is to create a new temp string variable, then add the two strings into that, then copy the value into the actual string variable you want it stored in and then the temp variable being deleted. This is very CPU intensive again.
- Lists: Lists are difficult to determine, as they can be filled by integers, floats, string, vectors and rotations, or a combination of them all. Generally the variables stored in the list affect its efficiency... BUT .... one thing to remember is that a list with one integer is more memory and CPU intensive than an integer alone as the creation and management of a list adds extra processing overhead.
So heres some tips to help you make more efficient scripts through the proper use of variable types:
- If you create a flag variable in your script, try to keep it as an integer. For example, if you want to record in a script which menu/sub-menu (llDialog) you are accessing, try using an integer with a specific number for each menu instead of a string. So, the first menu can be 0, the second one can be 1, the third one can be 2, instead of the first one being "Main", the second one being "Sub Menu 1" the third being "Sub Menu 2", etc.
- When using floats, look properly at the type of data that the float will contain. Do you expect there to ever be fractions? For example, if you are dealing with money, there are NEVER Linden cents. You cannot pay someone a fraction of a Linden Dollar, so anything to do with money calcualtions should always be kept as an integer. There are many situations like this.
- Use lists sparingly. Sometimes it might seem to be the best way to store data but often there is an alternative. One alternative is a string. Instead of storing, for example, the details of a purchase someone has made into a list like ["Name", "Date", "Item"], you can instead use a string like "Name*Date*Item" and then use the llParseString2List function later like this:
list_name = llParseString2List(combined_string, ["*"], [""]);
Again though you also need to weigh whether you will be doing this very often as this method could end up being worse if you would access llParseString2List too often.
Also, when you are done with a list, empty it. This is easily done by using list_name = [];. An empty list still consumes some resources, but far less than having one full of data you no longer need.
I hope that some find this useful. If not, feel free to comment and add your own experiences of variable efficiency as well
