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?
3 out of 3 found this helpful
Have more questions? Submit a request


  • 2
    Martin Tilo Schmitz

    Using ProfilerMarker instead of Profiler.BeginSample/EndSample comes with less overhead for profiling and can be used in a using scope to avoid missing an EndSample call when returning earlier in the block or an exception getting thrown.

Please sign in to leave a comment.