Jump to content
Aquablue

General Question about Basic Data Types and Memory Efficiency

Recommended Posts

In short, my question is: When using temporary variables such as for program flow, would the usage of the smallest data type possible lead to a noticeable improvement?

 

In almost every script, one has to use temporary variables for program flow, such as the famous integer i for controlling loops (for i := 0 to x do ...). Usually, integers such as this one do not attain a wide range. In one of my scripts I use "i" many times for many different loops, and in each of those i usually varies between 0 and 3, maximum of 100. However, out of laziness I always declared i as 'integer', meaning it can potentially go from -2147483648 to 2147483647 (http://wiki.scar-divi.com/The_Official_SCAR_Scripting_Guide#Types).

 

In the example above (i between 0 and 3) one could have easily used a smallint, or even a byte instead of an integer. The official guide also recommends using the smallest data type possible. I'm very interested in the opinion of experienced programmers about if this would result in a noticeable performance improvement.

Link to comment
Share on other sites

Well, that line in the tutorial is there for a good reason. Generally you really should always use the smallest datatype that suits your needs, with a few exceptions depending on the situation. But it's stated there more to learn people good practices, as in SCAR the difference will likely not make much of an impact, as the engine executes code considerably slower than a compiled executable would.

 

If you use a programming language such as Delphi or C++, you really do want to do this for sure, the memory considerations really aren't all that important for simple variables (more so for arrays), but it takes more time to read/write a variable from/to the memory if it is larger in size. Arithmetic or other operations on larger datatypes also takes additional processing power. On that note, an integer type variable such as Integer, Byte, SmallInt, ... will also be a lot faster with arithmetic operations than a floating point variable such as Single, Double or Extended.

 

Still, I would recommend also adopting these practices in SCAR, it will likely not be noticeable, but for larger scripts it will eventually create a measurable impact.

Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...