Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by _Petroz · Jul 31, 2010 at 01:34 AM · performancefloatintcast

C# Int to Float conversion performance

What is the performance of int to float casts in C#. I'm looking at the lightning example from Unity Procedural Examples, and there are two int -> float casts inside the tight loop. My understanding is these casts are quite slow in C, What is the performance of these casts in C#?

I'm curious about both int->float and float->int.

Here is the code from the lightning example:

for (int i=0; i < particles.Length; i++) { Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i); Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z), noise.Noise(timey + position.x, timey + position.y, timey + position.z), noise.Noise(timez + position.x, timez + position.y, timez + position.z)); position += (offset * scale * ((float)i * oneOverZigs));

 particles[i].position = position;
 particles[i].color = Color.white;
 particles[i].energy = 1f;

}

Comment
Add comment · Show 2
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 Wolfram · Aug 23, 2010 at 12:15 PM 0
Share

The article you are referring to is explicitly about float->int casting, not int->float casting; there is no rounding problem when doing the latter. Also, it is very old - I wouldn't infer that what's said in that artice still to be valid with current CPUs and/or compilers.

avatar image _Petroz · Aug 24, 2010 at 08:57 AM 0
Share

Thanks for the feedback, I realize the article is referring specifically to float->int conversions. I doubt that performance has anything at all to do with rounding. Since a 32-bit float cannot accurately represent all 32-bit integer values, there will be rounding occurring regardless of the direction of the cast.

3 Replies

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

Answer by Eric5h5 · Jul 31, 2010 at 01:40 AM

Put this in front of the code:

float timer = Time.realtimeSinceStartup;

and this after it:

Debug.Log (Time.realtimeSinceStartup - timer);

Then compare times with and without the cast.

Comment
Add comment · Show 4 · 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 _Petroz · Jul 31, 2010 at 05:48 AM 0
Share

Thanks Eric, your suggestion was useful. The resolution of the timer is not high enough to test the code as is. I changed it to repeat that code block 10,000 times and recorded 5% boost when changing it to a constant float. Combining that with storing some of the look-ups in local variables I was able to get the code 10% faster. Nothing break-through but also not insignificant.

avatar image _Petroz · Jul 31, 2010 at 06:31 AM 0
Share

I got it up to 18% in the end, code posted below.

avatar image qJake · Jul 31, 2010 at 09:52 AM 0
Share

Premature optimization is a sin. ;)

avatar image _Petroz · Jul 31, 2010 at 12:11 PM 0
Share

I completely agree, fortunately in this case it is not premature. This is an example, it is final. I think a more important axiom in this case is 'examples should be exemplary'. Sloppy code like tight loops with unnecessary look-ups should not be encouraged. If this is an example of procedural effects, performance of code is relevant.

avatar image
2

Answer by Tetrad · Jul 31, 2010 at 02:03 AM

Not that I disagree with Eric's suggestion, but there's a built-in profiler option that I think more people should know about.

http://unity3d.com/support/documentation/ScriptReference/Profiler.BeginSample.html

Profiler.BeginSample ("MyPieceOfCode");
// do something that takes a lot of time
Profiler.EndSample ();

Then in the profiler you can see it referenced by name.

Of course this doesn't work on Unity iPhone as there's no profiler.

Comment
Add comment · Show 2 · 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 Eric5h5 · Jul 31, 2010 at 02:36 AM 0
Share

Aside from Unity iPhone, which will cease to be an issue with Unity 3, the other problem with that is it only works in Unity Pro. Although indeed it's nice if you have Pro, and is worth mentioning.

avatar image _Petroz · Jul 31, 2010 at 05:45 AM 0
Share

That's excellent, I will make good use of this when I get the pro version.

avatar image
1

Answer by _Petroz · Jul 31, 2010 at 06:21 AM

I fixed a few things in this code:

  • reduced the two int to float casts to one
  • I moved the color and energy initialization into Start()
  • reduced the amount of lookups by storing target.position and trasform.position in local variables
  • I removed one float multiply by storing the product of i and oneOverZigs in a local variable

The final code which is roughly 18% faster is as follows:

Vector3 targetPos = target.position; Vector3 myPos = transform.position; for (int i=0; i < particles.Length; i++) { float f_i = (float)i; float dist = oneOverZigs * f_i; Vector3 position = Vector3.Lerp(myPos, targetPos, dist); Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z), noise.Noise(timey + position.x, timey + position.y, timey + position.z), noise.Noise(timez + position.x, timez + position.y, timez + position.z)); position += (offset * scale * dist);

 particles[i].position = position;

}

Comment
Add comment · Show 3 · 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 Mike 3 · Jul 31, 2010 at 06:54 AM 0
Share

Out of interest, why even cast i to a float? The compiler would do it implicitly if you did float dist = oneOverZigs * i; without the need for the intermediary variable

avatar image _Petroz · Jul 31, 2010 at 07:58 AM 0
Share

I wrote the two lines separately to illustrate that there were two separate optimizations being performed; it also made it easier to time both separately. You are right, they could be combined into a single line. AFAI$$anonymous$$ it's going on the stack either way, in which case the performance would be the same.

avatar image Nidre · Dec 04, 2013 at 08:34 AM 0
Share

You can also increase performance further if you rearrange the code as following;

     Vector3 targetPos = target.position;
     Vector3 myPos = transform.position;
      float f_i;
      float dist;
      Vector3 position;
       Vector3 offset;
     for (int i=0; i < particles.Length; i++)
     {
     f_i = (float)i;
     dist = oneOverZigs * f_i;
     position = Vector3.Lerp(myPos, targetPos, dist);
     offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
     noise.Noise(timey + position.x, timey + position.y, timey + position.z),
     noise.Noise(timez + position.x, timez + position.y, timez + position.z));
     position += (offset * scale * dist);
      
     particles[i].position = position;
     } 

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

1 Person is following this question.

avatar image

Related Questions

Cannot implicitly convert type `float' to `int 1 Answer

Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers

What is wrong with my throttle script? 2 Answers

Floats and int performance difference? 2 Answers

Possible to convert a Float into an Int? 3 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