Byte vs Int
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?
I don't think Unity handles this any different than anyone else. Searching "byte vs int" (with no "Unity" keyword) should tell you all you need to know.
Answer by Cepheid · Jul 28, 2016 at 08:10 PM
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. :)
Great answer, thank you! I'll check out StackOverflow on language-specific questions next time
Someone on stackoverflow even beat you to that quote (with an attribution going further back, if you're interested.)
So if I understand well, even if you don't use bytes for calculations, bytes can still be useful to storage save data (and take a little bit less space on the disk), even if time to save that data will be a little bit higher (due to conversion).
"Premature optimization is the root of all evil in program$$anonymous$$g." . This is somehow wrong in my opinion. For "quick knowledge" like this one, it just doesn't apply. This kind of quote is also used toward poeple asking if you should modify Vector3 values or create a new Vector3 ins$$anonymous$$d. In a lot of cases yes, it won't create a pitfall, but imagine it in a bigger scale, with a lot of different objects calling a lot of differents function with a lot of "$$anonymous$$or unoptimizations". You didn't care about premature optimization because "meh, Donald $$anonymous$$nuth said not to". Now you have to modify your entire game from A to Z because there isn't only one or two bad arithmetic operations, but hundreds of little mistakes stacking on each other. You could have avoided that by typing a question on google and getting the answer instantly on unity answers or stack exchange, too bad, everybody everywhere tells you not to care ins$$anonymous$$d of answering the question. When you have a doubt about the one thing you are about to do thousands of times in your code, it's better to know the optimized way of doing it, ins$$anonymous$$d of just doing it without thoughts and potentially have to fix months of work.
You should answer to the question first, then explain on a sidenote why premature optimization is, most of the time, overkill, not the opposite.
Note that it is only my opinion, and that you did answer the question, i just post that here because it is one of the google first results, and because lot of poeple got used to not even answer the question and skip to the Donald $$anonymous$$nuth part.
Here on the Unity forum, complete beginners ask these Q's (if they weren't beginners, they'd know how to look it up). We try to give the most useful answer for them. Once someone learns how to program, they can easily read from the many, many places explaining why and how to use bytes.
Answer by URGr8 · Mar 22, 2019 at 08:13 AM
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.
Your answer
Follow this Question
Related Questions
How to optimize fixed point numbers? 0 Answers
Increase FPS 0 Answers
Optimization Question - Floats or Ints? 0 Answers
Removing Tris/Verts/Polygons from an merged mesh? (C#) 0 Answers
reuse texture between quad 0 Answers