Floats vs Ints

Hey guys, I was wondering what’s better for performance regarding ints and float.

In the current game I’m working on I’m using both ints and float to represent stuff like damage, rpm, health, range, etc.
now, some are ints some are floats, they obviously can’t be both, so when they interact they need to be converter because apparently Unity and C# can’t handle that sort of thing.

The question is, am I better off using float for everything so that i don’t need to cast a float to an int every time they interact, or does using int instead of float reduce enough CPU usage(it does, doesn’t it?) so that the added action of casting is actually more efficient?

sorry if this topic has been a newbie plague for you guys, google doesn’t help much, the forum search results with a connection time out for some reason and I’m only 2 weeks back to C#(been away for like 5 years)

Honestly the performance won’t make a difference.

The reason to use an int is if you want to force the value to be a whole number. Use a float if you want decimal. That’s about it.

you don’t choose int against float for performance, you choose for how you want the data to be represented. int is great for distinct and countable values. like how much ammo you have in a gun. how many enemies you’ve killed and how many lives you have left. you can’t have three half lives left, you can only have a distinct number of lives (1,2,3). floats are for “floating-point” numbers. values that can be real or represented in fractions. traveling 5.23 miles. having 4.25 gallons of fuel left, played for 15.35 hours

neither is “better” in performance. (more accurately put, better enough to notice). Your game’s performance should never be affected due to int vs floats. programs can easily perform millions of casts per frame. so if performance is an issue the last thing you have to check would be number casting.

Boxing is different (casting a value type to and from a reference type). if you were casting an int to object and then back again, thats a performance red flag (due to extra allocations that need to be done) but thats not what you are talking about here.

5 Likes

Also, if you use floats to keep track of whole numbers, you’re going to run into precision errors where you think you’re getting 1, but you actually get 0.999999999999997, or something.

3 Likes

Huh…I never knew that…

So watch out with the math when a character’s lvl needs to be single digit…

Yeah, if you want to test for equality of floats you usually have to do something like this, which is way more expensive than comparing two ints:

if(Math.abs(float1 - float2) < epsilon)
2 Likes

0.30000000000000004.com/ (yes that’s the real website name) has a lot of nice information about floating-point numbers and how they’re weird.

4 Likes

Thank you everyone who replied, I’m aware that using ints is practically mandatory when counting live, ammo, etc.
thank you for the added clarification, one other thing i’m wondering…
Lets say i have an in game build, suppose it’s oil drill and it give you 100 moneys every 10 seconds, should i be using a float and lower the delta time in the update method to get exactly 10 seconds, or can i just use InvokeRepeating to save on cpu when a lot of this guys will be in play, or something like this so we can speedup the drilling in-game?

public float time
public float money

Start()
{
Invoke (“AddMoneys”, time)
}

AddMoneys()
{
player.money += money
Invoke (“AddMoneys”, time)
}

(ignore any spelling error in the code, i wrote it on the fly)

To my basic logic this should work just as fine and a lot more efficient then handling the timer every update and calculating the timer with the delta time, am I missing something with the Invoke command?
Didn’t find a single tutorial that even hypothesize it, but I’m sure I’m not the first to come up with the idea.

I mean, if you’re worried about micro-optimizing away the cpu cost of the timer, know that using Invoke is probably the slowest thing you can do.

A coroutine is better, less code, does not use strings to invoke code, and easier to read:

IEnumerator Start() {
    while(true) {
        yield return new WaitForSeconds(time);
        player.money += money;
    }
}

You should be using floats when what you’re doing would be appropriate for a decimal number, and using ints when you’re dealing with only whole numbers. If you’re often casting floats to ints you are losing information whenever you do that, which unless you’re doing that on purpose probably means you should be using ints instead of those floats.

For example, stuff like how many items you have, what level you’re on, etc, are typical int uses. Tracking Time.deltaTime since something happened, manipulating vector2/3’s, etc, are typical float uses.

As far as performance, back in the day int math was considerably faster than floating point math, but modern CPU’s are generally pretty good at floats, so you shouldn’t be dealing with performance issues unless you’re doing pretty much unthinkable numbers of floating point operations.

If you have to ask about “floats vs ints” then you’re at the level where the only good response is “use the right type for the job”.

3 Likes

I’d strongly recommend Update in this situation.

First of all, like @Baste has mentioned, the string argument is prone to errors. It does not refactor nicely, it’s easy to mess everything up with typos.
But that’s not the most important issue.

More importantly, you’re timing is inherently dependent on the implementation details of Invoke / InvokeRepeating.
There’s no guarantee the inaccuracy of the deltaTimes are compensated and I’m quite sure it’s not the case.

I’m not sure what kind of game you’re developing, but that small offset can easily grow to an offset of seconds within a few minutes and in some cases this can be annoying or even completely inacceptable.
With Update instead, you’ve got much more control for your timer.

Other than that, if you’re going to have lots of these entities, an actual and important optimization may be to write a manager that handles the updating process.
Having each instance running either their own timer in update, or their own chain of Invoke (or InvokeRepeating instead), or coroutines is surely something you can avoid.

When you’re talking about subtracting a number every frame, this is not going to matter at ALL in terms of performance. Even if it did, bear in mind that you don’t know what kinds of math Invoke() implies behind the scenes, so even the assumption that it’s more efficient is not certain. (Indeed, the very act of looking up a function by name, as Invoke does, would certainly wipe out any performance benefit from subtraction. Still on the “infinitesimal” end of performance considerations, though.)

1 Like

Ahhh I see, I did start looking into co-routines but then i saw a post(i think on this forum) saying that begginers should just be using Invoke/Repeating instead of co-routines because it’s a lot simpler(I hate it when they oversimplify) , didn’t take into account the fact that it needs to do a search every time, thank you guys.

And i suppose you’re using the “the wrong type for the job”?
Thanks for all the information and effort you put into you post.