# CustomSampler

> Custom CPU Profiler label used for profiling arbitrary code blocks.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.Profiling](/engine/6000.5/script-reference/unityengine/profiling.md)
* **Assembly:** UnityEngine.CoreModule
* **Inherits from:** [Sampler](/engine/6000.5/script-reference/unityengine/profiling/sampler.md)

```csharp
public sealed class CustomSampler : Sampler
```

## Remarks

Use CustomSampler to measure execution time of script code blocks. Produced information is displayed in the CPU Profiler and can be captured with [Recorder](/engine/6000.5/script-reference/unityengine/profiling/recorder.md).

Using CustomSampler is more efficient than using [Profiler.BeginSample](/engine/6000.5/script-reference/unityengine/profiling/profiler/beginsample.md) to profile your code. This is because CustomSamplers that have been created in advance have very low [CustomSampler.Begin](/engine/6000.5/script-reference/unityengine/profiling/customsampler/begin.md) call overhead compared to [Profiler.BeginSample](/engine/6000.5/script-reference/unityengine/profiling/profiler/beginsample.md).

```csharp
using UnityEngine;
using UnityEngine.Profiling;

public class ExampleClass : MonoBehaviour
{
    CustomSampler sampler;
    void Start()
    {
        sampler = CustomSampler.Create("MyCustomSampler");
    }

    void Update()
    {
        sampler.Begin();
        // do something that takes a lot of time
        sampler.End();
    }
}
```

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

Additional Resources: [Sampler](/engine/6000.5/script-reference/unityengine/profiling/sampler.md), [CustomSampler.Create](/engine/6000.5/script-reference/unityengine/profiling/customsampler/create.md), [CustomSampler.Begin](/engine/6000.5/script-reference/unityengine/profiling/customsampler/begin.md).

## Methods

| Method                                                                                | Description                                                                                                                                                              |
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [Begin](/engine/6000.5/script-reference/unityengine/profiling/customsampler/begin.md) | Begin profiling a piece of code with a custom label defined by this instance of [CustomSampler](/engine/6000.5/script-reference/unityengine/profiling/customsampler.md). |
| [End](/engine/6000.5/script-reference/unityengine/profiling/customsampler/end.md)     | End profiling a piece of code with a custom label.                                                                                                                       |

## Static Methods

| Method                                                                                  | Description                                                   |
| --------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| [Create](/engine/6000.5/script-reference/unityengine/profiling/customsampler/create.md) | Creates a new CustomSampler for profiling parts of your code. |

## Inheritance

**Inherited Members:**

* [Sampler.GetRecorder()](/engine/6000.5/script-reference/unityengine/profiling/sampler/getrecorder.md)
* [Sampler.Get(string)](/engine/6000.5/script-reference/unityengine/profiling/sampler/get.md)
* [Sampler.GetNames(List\<string>)](/engine/6000.5/script-reference/unityengine/profiling/sampler/getnames.md)
* [Sampler.isValid](/engine/6000.5/script-reference/unityengine/profiling/sampler/isvalid.md)
* [Sampler.name](/engine/6000.5/script-reference/unityengine/profiling/sampler/name.md)
