How can I profile a specific part of my code?

  • Not all of the user code is shown in the Profiler and Deep Profiling slows down my application.
The built-in profiler is not profiling all method calls. Also, Deep Profiling causes large overhead that will significantly slow down your application execution and it even may not be possible to perform the profiling activity at all (Unity can run out of memory).
Profiler Sample is a block of code that starts with Profiler.BeginSample() and ends with Profiler.EndSample() calls. Just like this:
using UnityEngine;

public class NeedsProfiling : MonoBehaviour {
    void Update() {
        Profiling.Profiler.BeginSample("My Sample");
        Debug.Log("This code is being profiled");

You would put it before and after the chunk of code that you want to be profiled. Profiler Sample will record the execution time for you and it will be displayed in the Profiler window without the need to use Deep Profiling.

As you can see, there is a new entry in the Profiler Hierarchy.

Of course you can add as many Profiler Samples as you want. Don’t worry about adding too many samples. These calls have zero overhead when they are deployed in non-development build.

More Information
Was this article helpful?
2 out of 2 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.