My question regards optimization and preformance.
Is there any advantage when using byte instead of int assuming you are dealing with numbers below 256?
I have read that ints are optimized, so if not under severe memory constraints, one should use ints for faster computation. Is that piece of knowledge correct? Are there any additional factors to be considered?
Hi @rocket350
Whenever I see a question like this I always jump straight to a quote from Donald Knuth. “Premature optimization is the root of all evil in programming.”
There are many questions asking for the benefits of using smaller data types to save incremental bits of memory when in all actuality the pitfalls of your game and program will be far more likely to fail due to bad arithmetic operations, poorly organised code, abundant use of static fields etc.
I’m not saying that paying attention to your memory usage and ensuring what your memory allocation is, is a bad thing. It’s just that people have a tendency to assume that using a float rather than a double or an int32 instead of an int64 is a quick easy fix to memory problems. An int is a defacto standard of many programs for good reasons.
Rant aside, this type of question is abundant of the SatckOverflow forums and the underlying architecture of a byte vs an int can be weighed by looking at the MSDN docs in some cases.
If you want a quick and short answer: stick to ints. If you’re focused on pure memory allocation then a byte is more optimised.
byte = 1 byte
int = 4 bytes.
But, it should be noted that in C# all arithmetic expressions are done on ints. This means that you’re bytes would have to be promoted to an int type to perform any calculations you may perform on them. So, in actuality, you will end up with an int as a result and you may be paying an extra cost for casting your bytes to perform arithmetic. Of course, I haven’t done any actual tests on this, it’s just more of an assumption.
TL;DR
Use ints, try to focus on different areas than the smallest memory type, search SO for the same question without making it Unity specific. I’m sure you’ll find a ton of threads detailing the benefits of both in full.
I hope this helps. ![]()
Reduce battery usage and increase game performance
If your doing some network programming, then sending a byte rather than an int is going to save you space and bandwidth. You could even package up 4 bytes into an int.
If you define your value as an int to start with, then you would have to cast it to byte and back on the other end, but if you just were working with bytes then you wouldn’t have to do that.
As @URGr8 said above, it can save you traffic or space.
But the following must be taken into account.
Since 90s, 16 and 32 bit architectures have been employed.
When you’re accessing an address in memory, it works faster, when aligned to 16 bits for 16-bit architecture, and 32 (or 64) for bigger ones, respectively.
In many cases, when going low-level, your byte will be turned to a 32-bit or 64-bit machine word by the compiler.
Therefore , this optimization could even slow down the things.