- Home /
Performance issue due to too many if statements.
Hi, I have a script that checks if a button pressed executes an event. For VR controllers whenever the user pressed a button a monobehaviour invokes an event and when the event executed everything that subscribed to that event gets called.
Problem is that this method forces me to create so many if statements about 100 - 150. I profiled the game and whenever the user presses a button Time ms jumps up to 1474.50 ms. When a button is not pressed game usually runs around 30 - 40 ms.
I'm not quite sure what is the issue here. Yes, I have so many if statements and they are called every frame in the update. But when buttons are not pressed, no issue. When a button is pressed there is a significant drop. It doesn't matter if the event gets called from that button has subscribers or not even an empty click causes drops.
Any idea why it might be happening? Here are some screenshots from profiler and script that causes those drops.
the ifs are not the problem then, even when there is nothing subscribed to all the buttonevents there are performance issues? there are a lot of gc calls is there somehting else you are doing in the method?
Can you show us a snippet of you if-statement hell? I can't imagine a world where if-statements are actually causing your problem, as you say it works fine when nothing is pressed -- aka when every if condition is checked.
Have you tried debugging? See what code runs when you press that button. There has to be code that is only ran when one of those bools becomes true.
Otherwise, I have no idea.
Try a "deep profile" to see where exactly that time goes. Note that deep profiling can cause extra performance issues and if too complex code runs the profiler may run out of memory and you get no results at all. Though it's worth a try.
Since we don't know what you're doing inside the XRInputBase.GetButton methods or inside your Event$$anonymous$$anager.XXX methods the second screenshot doesn't really help much.
Answer by tweakingtechnologies · May 24, 2019 at 07:36 AM
How about you make an enum for storing data like XRButton.One, XRBUtton.Two and then possibly use some switch statements instead of if's coz switch is faster
I will try to come up with a code sample.. but for now its like a puzzle for me too.. but hopefully you can see the approach
Answer by CruS · May 24, 2019 at 07:46 AM
I'm not a performance expert, I doubt that's the issue.
It looks like the GC.Alloc is becing called 5000 times in a single update loop?