How much Casting is Expensive?

Hi , i want to know how much casting is expensive and by that i don’t mean in Tick event at all . for example after killing enemy having a cast to something . or 5 times per second , any information that you have is help full for me .

Thank in advance

1 Like

Generally speaking, if you know you’re going to need to access an object many times, it’s good practice to cast it once and store a variable to the casted version of the object. That’s just about the most efficient way you can do it.

Now, with that out of the way, here’s the whole story: casting isn’t a big deal, performance-wise.

Basically, you don’t need to worry about it. From what I’ve been able to learn, a single Cast operation is not very computationally expensive, compared to other things you might need to do often, like normalizing vectors or updating an actor’s navigation/pathfinding.

If you’re curious for some actual numbers, have a look at this forum post, in which actual tests were carried out to discover how fast Cast operations are compared to using interfaces.

Bottom line, performance shouldn’t be a concern unless you’re casting more than a few thousand times per frame. (In other words, if you’re getting bad performance, it’s probably something else!)

Hope this helps!

3 Likes

That being said, you still want to minimize it as much as possible.

SOMETIMES it can cause issues, because UE4 still seems to be buggy sometimes. Like the previous comment said, it’s better to store your cast into a variable for later use if you know you need to use it multiple times. But much like traces, they’re pretty cheap CPU wise.

It’s always best practice to try to avoid multiple Branches, Event Tick, and Casting as much as possible.

After-all, pennies add up to dollars.

Thank you all guys it was the exact information that i wanted really thanks for the time