# Is casting expensive?

**URL:** <https://forums.unrealengine.com/t/is-casting-expensive/13589>\
**Category:** Blueprint\
**Tags:** unreal-engine\
**Created:** [October 19, 2014, 8:41pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589 "2014-10-19T20:41:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_2dc8725d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2dc8725d](https://forums.unrealengine.com/u/anonymous_user_2dc8725d)\
**Post date:** [October 19, 2014, 8:41pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/1 "2014-10-19T20:41:42Z")

</div>

Was wondering if casting is more expensive performance wise than using interfaces?

---

<div class="post-metadata">

**Author:** ![Zeustiak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeustiak](https://forums.unrealengine.com/u/Zeustiak)\
**Post date:** [October 19, 2014, 10:28pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/2 "2014-10-19T22:28:03Z")

</div>

I have heard people say casting is slow, but not in reference to interfaces. I don’t think it matters unless you are casting many times a second(tick) or something though.

---

<div class="post-metadata">

**Author:** ![Tom\_Looman](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tom_looman/32/1215549_2.png) [@Tom\_Looman](https://forums.unrealengine.com/u/Tom_Looman)\
**Post date:** [October 19, 2014, 11:09pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/3 "2014-10-19T23:09:16Z")

</div>

I don’t know about the cost. But I wouldn’t worry about it too much. The benefit of polymorphism outweighs the performance cost of such things I’d argue.

would be relevant if you’re running Blueprint in a tight loop and casting every iteration on a performance critical section. In which case I would argue that C++ is better option.

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [October 19, 2014, 11:17pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/4 "2014-10-19T23:17:13Z")

</div>

Based on my experience, I would not expect casting to be particularly slow compared to any other operation done in blueprints.  
If you’re doing hard-core parallel data processing in C++, then yes, avoid dynamic casting in the inner loop. But that’s not something you do in blueprints 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dede2580](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dede2580](https://forums.unrealengine.com/u/anonymous_user_dede2580)\
**Post date:** [October 21, 2014, 8:42am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/5 "2014-10-21T08:42:31Z")

</div>

> [@;166302](#):
>
> Was wondering if casting is more expensive performance wise than using interfaces?

I was curious about too, so I went ahead and did some actual tests to find out. The results were that the difference is minimal, but that casting is in fact slightly faster than interfaces. In a real-world scenario however, there is no reason to worry about it, and it’s probably safe to say that is at the very bottom of the list of things to consider for optimization. One might argue it’s not even on that list at all.

**DATA**

On an empty scene playing in the editor, eyeballing the FPS stats read-out, it looks like :

Running 10 000 casts + function calls per frame takes 79 - 85 milliseconds/frame  
Running 10 000 interface calls per frame takes 81 - 89 milliseconds/frame

So, there’s some overlap in those time intervals, but on average the cast approach is marginally faster. test was run on a laptop with a 460M and 6 GB of RAM, so _the absolute numbers are not interesting_ here, only the relative difference. There’s also the rendering overhead to take into account, which on system was around 20 ms.

**TEST SETUP**

The level blueprint uses OnTick to run a for-loop that either calls an interface function, or performs a cast followed by a function call. The execution wire is moved manually to one or the other cases between tests:

 ![cast_vs_interface_levelbp.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/d/d/dd7da18f4cd8ddec4b73525d648d0c987dbf6605.png)

The functions do _something_, but as little as possible:

 ![cast_vs_interface_levelbp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/7/d/77d50ac1d17167e946f27b763c78274285fab3b9.png)

If you want to try it out yourself, the project can be downloaded here: [https://dl.dropboxusercontent.com/u/2888286/CastVSInterface.zip](https://dl.dropboxusercontent.com/u/2888286/CastVSInterface.zip)

---

<div class="post-metadata">

**Author:** ![Zeustiak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeustiak](https://forums.unrealengine.com/u/Zeustiak)\
**Post date:** [October 21, 2014, 9:45am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/6 "2014-10-21T09:45:01Z")

</div>

Nice . Is that interface implemented only twice? Can you run the test again with some arbitrarily large number of blueprints implementing that interface?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dede2580](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dede2580](https://forums.unrealengine.com/u/anonymous_user_dede2580)\
**Post date:** [October 21, 2014, 10:11am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/7 "2014-10-21T10:11:15Z")

</div>

> [@Zeustiak;167021](#):
>
> Can you run the test again with some arbitrarily large number of blueprints implementing that interface?

I see what you’re getting at…

But, running with 20 different blueprints all implementing that same interface gives pretty much the same result, casting remains about 3 ms faster on average:

Cast+call: 87 - 92 ms (i.e. ~0.0089 ms per single cast-and-call)  
Interface: 89 - 95 ms (~0.0092 ms per single call)

The overall difference compared to the previous test is due to different viewport size and the additional 19 actors in the scene graph. So, again, only the relative difference between the two methods is of interest.

EDIT: As a sidenote, these results make me happy, because I very much prefer using interfaces. The numbers make it clear that the cost is negligible, so the choice is a matter of preference rather than performance.

EDIT.AGAIN: To clarify, “20 different blueprints” means 20 different BP classes, each one with its own implementation of the interface, and each one represented by a single instance in the scene. None of these classes have any mesh components attached, so it’s essentially just a bunch of null objects being “rendered”.

---

<div class="post-metadata">

**Author:** ![Fen](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fen/32/635651_2.png) [@Fen](https://forums.unrealengine.com/u/Fen)\
**Post date:** [October 21, 2014, 10:31am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/8 "2014-10-21T10:31:07Z")

</div>

Thanks for so cool test. I thought i was using a bit too much cast and would change some of them in an interface system but now i know not a trouble. A big thanks.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dede2580](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dede2580](https://forums.unrealengine.com/u/anonymous_user_dede2580)\
**Post date:** [October 21, 2014, 10:35am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/9 "2014-10-21T10:35:16Z")

</div>

You’re welcome, . It’s good to go Full Geek every now and then 🙂

---

<div class="post-metadata">

**Author:** ![Zeustiak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeustiak](https://forums.unrealengine.com/u/Zeustiak)\
**Post date:** [October 21, 2014, 10:42am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/10 "2014-10-21T10:42:29Z")

</div>

> [@;167026](#):
>
> I see what you’re getting at…
> 
> But, running with 20 different blueprints all implementing that same interface gives pretty much the same result, casting remains about 3 ms faster on average:
> 
> Cast+call: 87 - 92 ms (i.e. ~0.0089 ms per single cast-and-call)  
> Interface: 89 - 95 ms (~0.0092 ms per single call)
> 
> The overall difference compared to the previous test is due to different viewport size and the additional 19 actors in the scene graph. So, again, only the relative difference between the two methods is of interest.
> 
> EDIT: As a sidenote, these results make me happy, because I very much prefer using interfaces. The numbers make it clear that the cost is negligible, so the choice is a matter of preference rather than performance.
> 
> EDIT.AGAIN: To clarify, “20 different blueprints” means 20 different BP classes, each one with its own implementation of the interface, and each one represented by a single instance in the scene. None of these classes have any mesh components attached, so it’s essentially just a bunch of null objects being “rendered”.

Great! 🙂

I just wish we could visualize the interfaces/casts/etc with an event graph, blueprints as nodes, lines going everywhere, etc.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dede2580](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dede2580](https://forums.unrealengine.com/u/anonymous_user_dede2580)\
**Post date:** [October 21, 2014, 10:51am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/11 "2014-10-21T10:51:40Z")

</div>

> [@Zeustiak;167041](#):
>
> I just wish we could visualize the interfaces/casts/etc with an event graph, blueprints as nodes, lines going everywhere, etc.

You mean like an automatically generated über-graph showing _everything_ that’s going on in a single view?

Yeah… that would be pretty cool and probably a useful tool.

---

<div class="post-metadata">

**Author:** ![TheSpaceMan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSpaceMan](https://forums.unrealengine.com/u/TheSpaceMan)\
**Post date:** [October 21, 2014, 1:05pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/12 "2014-10-21T13:05:24Z")

</div>

should then of course be compared to a cast, interface, direct access in a C++ implementation. 😉  
The overhead would be a lot higher with a long hierarchy as well.  
A from B from C from D from E, run function interface on A, implemented in B, C, D, E.  
Compared to casting to E using a non public/shared function in E.

But as people say in 9999 cases out of 10000 will not be your problem or why your code is slow.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dede2580](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dede2580](https://forums.unrealengine.com/u/anonymous_user_dede2580)\
**Post date:** [October 21, 2014, 1:41pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/13 "2014-10-21T13:41:04Z")

</div>

Yeah, your mileage will vary of course, and there’s no point comparing C++ to BP in terms of raw performance.

So, for the 9999 normal use-cases for blueprints, the answer to the original question is “don’t worry about it”. If you’re dealing with that single remaining performance-critical case, then C++ is the real answer.

I just ran some final tests with an automated frame counter and accumulating deltatimes to get a more accurate average, and the result was 90 ms for interface calls, and 86 ms for cast+call. That’s for 10 000 loops per frame, and I let it run for a total of 1000 frames per test to get the average to stabilize properly at a “true” value.

gives a difference per single call of a mere 0.4 **micro** seconds (a.k.a. nothing).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0df12f74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0df12f74](https://forums.unrealengine.com/u/anonymous_user_0df12f74)\
**Post date:** [October 21, 2014, 3:33pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/14 "2014-10-21T15:33:25Z")

</div>

Thank you for your efforts ! is very valuable info, should be added it into the documentation (or the wiki) for future reference! 🙂

---

<div class="post-metadata">

**Author:** ![Dig\_Squid](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dig_squid/32/1145423_2.png) [@Dig\_Squid](https://forums.unrealengine.com/u/Dig_Squid)\
**Post date:** [October 22, 2014, 4:00pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/15 "2014-10-22T16:00:34Z")

</div>

Fantastic to get some hard numbers on that! That is what I would expect - the decision to cast vs use an interface is not really about performance, but about architecture. I would only use an interface when you need one - when you have multiple ‘unrelated’ classes that should share some behavior or event. The example I always give is if you want a flame thrower that can ‘burn’ things, you probably want a ‘burnable’ interface that lots of different BPs can implement. If you only care about one thing (e.g. see if it is the player that i hit, and if so, give them a key), then casting is a bit simpler because you don’t have the hassle of making an interface.

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [October 22, 2014, 6:09pm UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/16 "2014-10-22T18:09:32Z")

</div>

A good thing when optimizing stuff is to cache those objects you need a lot, will safe you those casting that you do over and over again. For example casting to a specific weapon type from the base engine type, you could hold a casted cached value and change the cache when you switch the weapon or drop it. Same applies for the typical Pawn \<-\> PlayerController relationship.

But I would only doing it if its totally logic in a specific point or easy to do and to maintain or if you are really seeing some peaks when profiling. Take into account that caching gives you also headaches when the cache is not invalidated properly resulting nice bugs 😃

---

<div class="post-metadata">

**Author:** ![President](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@President](https://forums.unrealengine.com/u/President)\
**Post date:** [October 23, 2014, 6:38am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/17 "2014-10-23T06:38:07Z")

</div>

,  
You are awesome…thanks…

we need a Mythbusters thread started for running tests like these… 😛

---

<div class="post-metadata">

**Author:** ![StevePeters](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@StevePeters](https://forums.unrealengine.com/u/StevePeters)\
**Post date:** [October 27, 2014, 1:03am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/18 "2014-10-27T01:03:18Z")

</div>

> [@Moss;167858](#):
>
> A good thing when optimizing stuff is to cache those objects you need a lot, will safe you those casting that you do over and over again. For example casting to a specific weapon type from the base engine type, you could hold a casted cached value and change the cache when you switch the weapon or drop it. Same applies for the typical Pawn \<-\> PlayerController relationship.
> 
> But I would only doing it if its totally logic in a specific point or easy to do and to maintain or if you are really seeing some peaks when profiling. Take into account that caching gives you also headaches when the cache is not invalidated properly resulting nice bugs 😃

I was going to ask about caching in general in UE. I’m coming from Unity where it’s pretty common to cache everything you plan to reference in a script. Does it make a significant difference in Unreal or should I not worry about it?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_49a519741](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_49a519741](https://forums.unrealengine.com/u/anonymous_user_49a519741)\
**Post date:** [March 23, 2017, 5:41am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/19 "2017-03-23T05:41:13Z")

</div>

I’ll add my thanks, . Well done. After that wall of appreciation I’m surprised I was the first to upvote your post.

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [March 23, 2017, 5:50am UTC](https://forums.unrealengine.com/t/is-casting-expensive/13589/20 "2017-03-23T05:50:38Z")

</div>

> [@Jin\_VE;684611](#):
>
> I’ll add my thanks, . Well done. After that wall of appreciation I’m surprised I was the first to upvote your post.

is topic from 2014;  
Votes didn’t even exist back then and we should avoid bringing back old threads like for no reason…

[Next page](https://forums.unrealengine.com/t/is-casting-expensive/13589.md?page=2)
