Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

geckoo

Mathf.PI

Recommended Posts

Hello friends. I have a question about Mathf.PI - The infamous 3.14159265358979 as said Unity main page information :

Please login or register to see this link.

I read somewhere that using long float numbers wastes some CPU ressources. I don't know if it is a bullshit or a fact which has been proved. In my project which is essentially a procedural shapes generation I use often Mathf.PI - an infinite float number. Do you think that I have to change it for something shorter like 3.14 or there is no real incidence? Thank you for your advice. I really appreciate it. Have a nice weekend ++

Share this post


Link to post
Share on other sites

A 32-bit float will be a 32-bit float no matter the value, and Mathf.PI is just a constant so no worries about performance.

Share this post


Link to post
Share on other sites

Thank you for your answer. Do you mean that each float number use the same 32-bit system?

3.14f and 3.14159265359f have no difference for CPU ressource?

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

@Gaze is totally right about there being no difference in performance since its a fixed size data type of 32 bits. What about 64 bit double? I tested it out with a mono compiler and got an unexpected result: double performs x3 faster than float. My test uses the Mono C# compiler which is the same one used in Unity.

 

image.png

 

Please check it out Please login or register to see this link.

 

I then did a test on my own computer with an Intel i5 6th gen processor and found that there was no difference. This is probably due to CPU level optimizations:

image.png

 

Probably anyone who plays videogames has a modern CPU with these kinds of optimizations so floats vs double really don't matter. This answer puts more detail into optimizations Please login or register to see this link.

Share this post


Link to post
Share on other sites

Thank you Isaak for your interesting answer. I was a little bit disappointed by the first answer which didn't give any solid explanation - excepted a single line. Yours is more detailled and convinced me. I am trying to reach the best optimization coding my project STR!PES on which I am working. Actually it runs using a few batches (between 35/45 maximum) but I continue to find a way to go down. Finally I can reach a really low execution - enough to be playable on a old mobile device. Procedural generation and geometry in games are just fascinating. When the game is really speed with many stripes rushing on the center, I have those values in inspector ++

 

CPU : main 13.0 ms (at start 11.9)

render thread 1.7 ms (at start 1.1)

batches : 43 (at start 25 with an easy level)

 

I noticed that I totally forgot to talk about game itself - just expanding some explanation about code. At the link below you can find my game STR!PES :)

Please login or register to see this link.

 

  • Like 1

Share this post


Link to post
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...