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 tannaku · Apr 06, 2017 at 01:24 AM · optimizationfloatint

Optimization Question - Floats or Ints?

Hello world! Yes, I know that was corny, please don't go away... :(

I was wondering when, in terms of optimization, it makes more sense to use floats over ints?

I am creating a game that runs calculations every second, using fixed update, for multiple status effects. In my current situation, I have a fullness stat that decreases with each calculation by a fixed amount. For simplicity, since calculations are run every second, it decreases by 1, and the fullness stat is initialized at the value in seconds I want it to last. That ends up being about 1000 seconds. Of course, I could get the same effect if I set fullness to 100, and decreased it by .1 every second, if I used float values instead of ints. Theoretically, with this kind of implementation, since these variables don't affect object positions and rotations, etc, if I found myself needing finer values I could just add digits to them in order to keep them as int variables (eg 10000 fullness decreasing by 10 each second, etc etc). This thought made me want to ask:

In terms of program speed from a calculation standpoint, how large do int values have to be before it is not at all justifiable in using them, and they should be converted into floats with decimal values?

Thanks for your answers!

Comment
Add comment · Show 3
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 hexagonius · Apr 06, 2017 at 05:23 AM 1
Share

run a for loop with your code a bazillion times and measure the difference in time on both. if you ask me, it's the wrong place to optimize speed at all. there are plenty other places where it really gets in your way, not floats or ints.

avatar image tannaku hexagonius · Apr 06, 2017 at 11:43 AM 0
Share

Thanks for the feedback! <3 What suggestions for documentation do you have, that covers the other places you are talking about?

avatar image SohailBukhari · Apr 06, 2017 at 12:29 PM 0
Share

There are many, many variables to performance.Especially between integer and floating point math. It varies strongly from processor to processor (even within the same family such as x86) because different processors have different pipeline lengths. Also, some operations are generally very simple (such as addition) and have an accelerated route through the processor, and others (such as division) take much, much longer.

The other big variable is where the data reside. If you only have a few values to add, then all of the data can reside in cache, where they can be quickly sent to the CPU. A very, very slow floating point operation that already has the data in cache will be many times faster than an integer operation where an integer needs to be copied from system memory.

0 Replies

· Add your reply
  • Sort: 

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

102 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 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

Adding Int's from different and the same script 1 Answer

How to make a number higher than Quintillion? (1000000000000000000) 1 Answer

ToString ("f0") is rounding my float 1 Answer

Error CS0266 with my clicker game 1 Answer

Really need help please/A switch expression of type `float' cannot be converted to an integral type, bool, char, string, enum or nullable type 2 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