0
0
Unityframework~10 mins

Performance profiling in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Performance profiling
Start Profiling
Run Game Code
Collect Performance Data
Analyze Data
Identify Bottlenecks
Optimize Code
Repeat Profiling
End Profiling
Start profiling, run the game, collect data, analyze to find slow parts, optimize, and repeat until performance improves.
Execution Sample
Unity
Profiler.BeginSample("MyFunction");
// Code to profile
Profiler.EndSample();
This code marks the start and end of a profiling sample to measure performance of 'MyFunction'.
Execution Table
StepActionProfiler StateData CollectedResult
1Profiler.BeginSample("MyFunction") calledProfiling started for MyFunctionNo data yetReady to measure
2Game code inside MyFunction runsProfiling activeExecution time accumulatingTracking performance
3Profiler.EndSample() calledProfiling stopped for MyFunctionTotal time recordedSample complete
4Analyze collected dataProfiler idleExecution time and call count availableIdentify slow parts
5Optimize code based on dataProfiler idleNo new data yetCode improved
6Repeat profiling to verifyProfiling restartedNew data collectedCheck improvements
7Profiling endsProfiler stoppedFinal performance dataPerformance optimized
💡 Profiling ends after collecting and analyzing performance data to optimize code.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
Profiler StateIdleActive (MyFunction)IdleActive (MyFunction)Stopped
Execution Time0 msAccumulatingRecorded (e.g. 15 ms)AccumulatingFinal (e.g. 12 ms)
Call Count0IncrementingRecorded (e.g. 1)IncrementingFinal (e.g. 1)
Key Moments - 3 Insights
Why do we need to call Profiler.BeginSample and Profiler.EndSample around the code?
These calls mark the start and end of the code section to measure. Without them, the profiler won't know which part to track, as shown in steps 1 and 3 of the execution_table.
What does the profiler measure during the active state?
During the active state (step 2), the profiler collects execution time and call count data for the marked code, which helps identify slow parts as seen in step 4.
Why do we repeat profiling after optimizing code?
Repeating profiling (step 6) verifies if the optimizations improved performance by comparing new data with previous results, ensuring changes had the desired effect.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the profiler do?
AStarts measuring performance
BAnalyzes the data collected
CStops measuring performance and records data
DOptimizes the code automatically
💡 Hint
Check the 'Profiler State' and 'Result' columns at step 3 in the execution_table.
According to variable_tracker, what is the 'Profiler State' after step 6?
AActive (MyFunction)
BIdle
CStopped
DNot started
💡 Hint
Look at the 'Profiler State' row under 'After Step 6' in variable_tracker.
If we forget to call Profiler.EndSample(), what will happen according to the flow?
AProfiler will never start
BProfiler will keep measuring indefinitely
CProfiler will stop automatically after some time
DProfiler will throw an error immediately
💡 Hint
Refer to the concept_flow where profiling starts and ends explicitly with BeginSample and EndSample.
Concept Snapshot
Performance Profiling in Unity:
- Use Profiler.BeginSample("Name") before code to measure
- Run the code section
- Use Profiler.EndSample() after code to stop measuring
- Analyze collected data to find slow parts
- Optimize code and repeat profiling
- Helps improve game performance step-by-step
Full Transcript
Performance profiling in Unity involves marking sections of code with Profiler.BeginSample and Profiler.EndSample to measure how long they take to run. The profiler collects data like execution time and call count while the code runs. After stopping the sample, you analyze the data to find slow parts or bottlenecks. Then you optimize the code and profile again to check if performance improved. This cycle repeats until the game runs smoothly. The execution table shows each step from starting profiling, running code, stopping profiling, analyzing data, optimizing, and repeating. The variable tracker shows how profiler state and data change during these steps. Key moments clarify why marking code sections is necessary and why repeating profiling is important. The quiz tests understanding of profiler states and the importance of proper start and stop calls.