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.

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.

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

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?