CodeBlanks Logo Icon CodeBlanks

The Profiler tool

Learn per-frame time distribution with the Profiler.

What is the Profiler tool?

The Profiler is an analysis tool that lets you examine the CPU, GPU, and memory spent by your game in every frame. To monitor execution times of code blocks, Profiler.BeginSample and Profiler.EndSample are used.

using UnityEngine.Profiling;

Profiler.BeginSample("DusmanGuncelleme");
DusmanlariGuncelle();
Profiler.EndSample();

This allows pinpointing exactly which function is dropping your frame rate (FPS).

CPU and GPU bottlenecks

When a game stutters, you need to know where the bottleneck lies. If CPU times are high, there is excessive game logic, physics, or too many draw calls; if GPU times are high, there are complex shaders or heavy pixel overdraw.

// At a 60 FPS target, each frame must complete under 16.6 ms:
// CPU Time + GPU Time <= 16.6ms

Optimizing blindly without profiling usually results in wasted effort.

Practice Report frame time and bottleneck

Calculate the total time by adding the given cpuTime and gpuTime values. Determine the greater one as the bottleneck and print with Console.WriteLine.

Starter code

Player.cs
using System;

class ProfilerReport
{
    static void Main()
    {
        float cpuTime = 18.4f;
        float gpuTime = 9.2f;
        // CPU ve GPU surelerini topla, buyuk olani darbogaz olarak yazdir
        
    }
}

Sample solution

Player.cs
using System;

class ProfilerReport
{
    static void Main()
    {
        float cpuTime = 18.4f;
        float gpuTime = 9.2f;
        float total = cpuTime + gpuTime;
        string bottleneck = cpuTime > gpuTime ? "CPU" : "GPU";
        Console.WriteLine($"Kare: {total:F1}ms - Darbogaz: {bottleneck}");
    }
}

Expected output

Kare: 27.6ms - Darbogaz: CPU

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the primary function of Profiler.BeginSample and Profiler.EndSample methods?

  • Halve the polygon count within the camera view angle
  • Delete all active objects in the scene from memory
  • Label and measure the execution time of a specific code block on the CPU timeline Correct answer
  • Dynamically increase the screen resolution of the game

Explanation: The BeginSample and EndSample pair causes the enclosed code to appear under a custom label in the Profiler CPU hierarchy.

2 Begin sample

Question
UnityEngine.Profiling.Profiler.____("FizikHesaplama");
Answer
UnityEngine.Profiling.Profiler.BeginSample("FizikHesaplama");

Explanation: A custom timing block in the Profiler class is started with BeginSample.

3 Profiler concepts

Term Meaning
Profiler Monitors frame time and resource usage
CPU Usage Shows script logic and physics load
GPU Usage Shows pixel and shader workload
Profiler.EndSample() Ends the measured sample block

All lessons in Expert