Adding profiler markers to your code
Add profiler markers to mark up areas of your code.
Read time 4 minutesLast updated 12 days ago
Add profiler markers to your code to view the samples that , , or generates in the Timeline View and Hierarchy View of the CPU Usage module in the Profiler window:
ProfilerMarker.BeginProfilerMarker.EndProfilerMarker.Auto
Profiler sample with metadata in Timeline View.

Profiler sample with metadata in Hierarchy View.
Prerequisites
Some examples use the Profiling Core package, which you must install before you start. The Unity Profiling Core package isn't discoverable in the Package Manager UI because it's a core package. To install the package, add it by its name, which is .
com.unity.profiling.coreMarking up your code
To use the API, place the code you want to profile between calls to and . For example:
ProfilerMarkerProfilerMarker.BeginProfilerMarker.Endusing UnityEngine;using Unity.Profiling;public class ProfilerMarkerExample{ static readonly ProfilerMarker k_MyCodeMarker = new ProfilerMarker("My Code"); void Update() { k_MyCodeMarker.Begin(); Debug.Log("This code is being profiled"); k_MyCodeMarker.End(); }}
Make sure that the code in between the and calls doesn't exit the scope before is called. If the code exits the scope before is called, an error message is logged to the Console. To avoid having to call before every return, use so that the sample is ended automatically on leaving the scope. For more information, refer to the section in this documentation on Automatically close profiler marker code blocks.
BeginEndEndEndEndAutoUnity records and reports the profiled code block's execution time to the Profiler, and displays it in the CPU Profiler module without the need to use Deep Profiling. It displays it as a new entry in the Hierarchy View of the CPU Profiler module, as follows:

Profiler sample in the Profiler Window.
Automatically close profiler marker code blocks
Use to ensure that is automatically called at the end of the code block. The following calls are equivalent:
ProfilerMarker.AutoProfilerMarker.Endusing Unity.Profiling;public class MySystemClass{ static readonly ProfilerMarker k_UpdatePerfMarker = new ProfilerMarker("MySystem.Update"); public void Update() { k_UpdatePerfMarker.Begin(); // ... k_UpdatePerfMarker.End(); using (k_UpdatePerfMarker.Auto()) { // ... } }}
Unlike the and calls, Unity can't compile out the call in non-development (Release) builds. It will however instead return null, which only adds minimal overhead.
Begin()End()ProfilerMarker.AutoYou can also use with a and the call happens automatically once the current scope ends. This approach minimizes the amount of code you need to change when you add a instance to your code:
ProfilerMarker.Autousing varEndProfilerMarkerusing Unity.Profiling;public class MySystemClass{ static readonly ProfilerMarker k_UpdatePerfMarker = new ProfilerMarker("MySystem.Update"); public void Update() { using var _ = k_UpdatePerfMarker.Auto(); // ... }}
Adding integer or floating point parameters to samples
Sometimes you might want to add context to your code samples to identify specific conditions in which the code runs for a long time.
For example, if your system carries out simulations of objects, you can pass the number of objects with a Profiler sample. If the Profiler returns an abnormal number along with a long sample duration, that might mean you have to use another thread for simulation, split the CPU work across multiple frames (timeslicing), or adjust your application's design to prevent frame drops.
ProfilerMarkerProfilerMarker<TP1>ProfilerMarker<TP1, TP2>ProfilerMarker<TP1, TP2, TP3>using Unity.Profiling;public class MySystemClass{ static readonly ProfilerMarker<int> k_PreparePerfMarker = new ProfilerMarker<int>("MySystem.Prepare", "Objects Count"); static readonly ProfilerMarker<float> k_SimulatePerfMarker = new ProfilerMarker<float>(ProfilerCategory.Scripts, "MySystem.Simulate", "Objects Density"); public void Update(int objectsCount) { k_PreparePerfMarker.Begin(objectsCount); // ... k_PreparePerfMarker.End(); using (k_SimulatePerfMarker.Auto(objectsCount * 1.0f)) { // ... } }}
Adding string parameters to samples
The API supports adding string parameters to your profiler markers. A string parameter can be useful if you want to display the name of the level or file when your application loads level or data files. Use methods to pass a string parameter along with a Profiler sample:
ProfilerMarkerProfilerMarkerExtensionusing Unity.Profiling;public class MySystemClass{ static readonly ProfilerMarker k_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare"); public void Prepare(string path) { k_PreparePerfMarker.Begin(path); // ... k_PreparePerfMarker.End(); }}