Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by rocket350 · Jul 28, 2016 at 07:13 PM · optimizationspeedintbytepreformance

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 28, 2016 at 07:38 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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. :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rocket350 · Jul 28, 2016 at 08:23 PM 0
Share

Great answer, thank you! I'll check out StackOverflow on language-specific questions next time

avatar image Owen-Reynolds · Jul 28, 2016 at 08:57 PM 1
Share

Someone on stackoverflow even beat you to that quote (with an attribution going further back, if you're interested.)

avatar image eskivor · Dec 23, 2017 at 01:03 AM 0
Share

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).

avatar image hlw · Jan 25, 2019 at 03:49 PM 0
Share

"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.

avatar image Owen-Reynolds hlw · Jan 26, 2019 at 01:28 AM 0
Share

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.

avatar image
0

Answer by hari2015 · Feb 17, 2018 at 03:11 AM

Reduce battery usage and increase game performance

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges