Game Development
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.
Exercise 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.
Show example solution
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}");
}
}
Test yourself
-
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
- Dynamically increase the screen resolution of the game
Show answer
Label and measure the execution time of a specific code block on the CPU timeline — The BeginSample and EndSample pair causes the enclosed code to appear under a custom label in the Profiler CPU hierarchy.
-
Begin sample
UnityEngine.Profiling.Profiler.____("FizikHesaplama");
Show answer
UnityEngine.Profiling.Profiler.BeginSample("FizikHesaplama");
-
Profiler concepts
- 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
Next lesson: Batching