Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by marpione · May 21, 2019 at 12:39 PM · scripting problemperformanceperformance optimizationgcgc.collect

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.

alt text

xrinputexecuter.png (93.7 kB)
profiler-issue.png (157.7 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · May 21, 2019 at 01:45 PM 1
Share

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?

avatar image TreyH · May 21, 2019 at 02:35 PM 1
Share

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.

avatar image SunnyPalmSprings · May 21, 2019 at 02:50 PM 0
Share

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.

avatar image Bunny83 · May 21, 2019 at 04:13 PM 1
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

210 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is calling GetComponent multiple times bad? 2 Answers

Why is GC.Collect() very slow? 2 Answers

How to improve the Performance of Removing Trees during runtime? 1 Answer

Explain the use for Occlusion Area? How is this different from regular Occlusion Culling? 2 Answers

Changing shader at runtime vs multi-purpose shader 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges