Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
5
Question by Sycobob · Mar 26, 2014 at 09:58 PM · c#performanceoptimizationinitialization

First time a method is executed is ~4 times slower

I've noticed a strange issue with the game I'm working on. It's sort of a match-3 style game and the first time the player clears a match from the board, there's a huge performance hiccup. I'm in the process of trying to find the source. According to the profiler, the first time most methods are called, they take about 4 times longer to execute than subsequent calls. For example, the constructor of a fairly simple class CardClearData takes 3.21 ms the first time one is created. The second time it takes 0.97 ms. This class has no static constructor that is getting called in the background and does not inherit from any other classes.

Is there some sort of magic being done under the hood by Unity? Some sort of caching or optimization? It's been extremely hard to track this down because it doesn't come from any one method or object. Across the entire hierarchy reported by the profiler essentially every function call takes longer the first time. As far as I can tell, this is something fundamental to either Unity or C# that I don't know about.

Here is what I know so far:

  • It doesn't seem to be from texture loading, as far as I can tell. I'm not seeing any texture loading entries in the profiler. I'm also using the two big 2048 textures before this happens and there's only a couple of smaller 512ish textures that might be getting loaded.

  • There's not any caching or object initialization going on the first time. All of that sort of setup is done before entering the game.

  • No GameObjects are being created. They're just activated from a pool of inactive objects.

  • I'm seeing the issue on regular objects created purely in C#, not just GameObjects.

Here are a couple pics to illustrate the issue.

First call Second call

performance (1).png (280.3 kB)
performance (2).png (279.0 kB)
Comment
Add comment · Show 3
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 poolts · Apr 04, 2014 at 02:18 PM 0
Share

Did you ever work out why this was happening? I guess Unity is allocating resources and recycling them after the initial execution?

avatar image fafase · Apr 04, 2014 at 02:37 PM 0
Share

Is the method calling some static members or static methods? Those are created on first call so that would explain why it gets slower. Also, the used variables may not be cached until first call so you get some cache miss that needs to be resolved, later calls do not have those cache misses since the variables are still in the cache memory.

avatar image Sycobob · Apr 10, 2014 at 02:56 PM 0
Share

@poolts, Unfortunately, no, I did not find the root cause. I found out the majority of the performance issues I was seeing was from the GPU loading textures for the first time (it was only a problem on mobile which is why I didn't immediately find the problem. Turns out, mobile devices are really slow at loading textures). Once I fixed that problem, this because a much lower priority.

@fafase, Not in my code, but in the .Net calls, yes. I tried to rule that out as much as I could. I initialized my lists to be the correct size so I know it was wasn't growing. I made sure there was no additional memory allocations in the first call. $$anonymous$$oving code into cache memory is actually what I'm leaning towards at the moment. $$anonymous$$ain memory is still really fast, tens of nanoseconds, so it'd have to be doing a lot of accesses to add up to the time I'm seeing, but it's possible. I don't have a better idea right now.

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Need help optimizing grid initialization. 1 Answer

Colliders vs Callbacks: Which is better for performance in my case? 0 Answers

Distribute terrain in zones 3 Answers


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