# BeginSample

> Begin profiling a piece of code with a custom label.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.Profiling](/engine/6000.0/script-reference/unityengine/profiling.md)
* **Assembly:** UnityEngine.CoreModule

## BeginSample(string)

Begin profiling a piece of code with a custom label.

```csharp
[Conditional("ENABLE_PROFILER")]
public static void BeginSample(string name)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): A string to identify the sample in the Profiler window.

### Remarks

The Profiler displays the sample in the Hierarchy and Timeline views. The sample is nested under the events or functional calls that lead to the execution of the sampled code. For example, a sample placed in Update displays under `Update.ScriptRunBehaviourUpdate` in the Profiler Hierarchy and Timeline views. If you supply a `targetObject`, then you can click on the sample in the Profiler Timeline to select that object in the Editor (when profiling from the Editor Play mode).

Profiler.BeginSample is conditionally compiled away using ConditionalAttribute. Thus it will have zero overhead, when it is deployed in non-Development Build.

```csharp
using UnityEngine;
using System.Collections;
using UnityEngine.Profiling;

public class ExampleClass : MonoBehaviour
{
    void Example()
    {
        Profiler.BeginSample("MyPieceOfCode");
        // Code to measure...
        Profiler.EndSample();
    }
}
```

Additional Resources: [Profiler.EndSample](/engine/6000.0/script-reference/unityengine/profiling/profiler/endsample.md), [ProfilerCPU](/engine/6000.0/manual/analysis/profiler/visualizing-data/cpu.md).

## BeginSample(string, Object)

Begin profiling a piece of code with a custom label.

```csharp
[Conditional("ENABLE_PROFILER")]
public static void BeginSample(string name, Object targetObject)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): A string to identify the sample in the Profiler window.**** (\[Object]\(/engine/6000.0/script-reference/unityengine/object)): An object that provides context to the sample,.

### Remarks

The Profiler displays the sample in the Hierarchy and Timeline views. The sample is nested under the events or functional calls that lead to the execution of the sampled code. For example, a sample placed in Update displays under `Update.ScriptRunBehaviourUpdate` in the Profiler Hierarchy and Timeline views. If you supply a `targetObject`, then you can click on the sample in the Profiler Timeline to select that object in the Editor (when profiling from the Editor Play mode).

Profiler.BeginSample is conditionally compiled away using ConditionalAttribute. Thus it will have zero overhead, when it is deployed in non-Development Build.

```csharp
using UnityEngine;
using System.Collections;
using UnityEngine.Profiling;

public class ExampleClass : MonoBehaviour
{
    void Example()
    {
        Profiler.BeginSample("MyPieceOfCode");
        // Code to measure...
        Profiler.EndSample();
    }
}
```

Additional Resources: [Profiler.EndSample](/engine/6000.0/script-reference/unityengine/profiling/profiler/endsample.md), [ProfilerCPU](/engine/6000.0/manual/analysis/profiler/visualizing-data/cpu.md).
