Bird
Raised Fist0
Unityframework~20 mins

Performance profiling in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Unity Performance Profiler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Understanding Unity Profiler Output
What will be the main visible effect in the Unity Profiler when the following code runs inside Update() method of a MonoBehaviour?
Unity
void Update() {
    for (int i = 0; i < 100000; i++) {
        float x = Mathf.Sqrt(i);
    }
}
AHigh GPU usage shown under 'Rendering' section with increased draw calls
BHigh CPU usage shown under 'Scripts' section with increased time in 'Update' method
CMemory spike shown under 'Memory' section due to many allocations
DNo significant change in profiler because Mathf.Sqrt is optimized
Attempts:
2 left
💡 Hint
Think about what part of the game loop the code runs in and what kind of resource it uses.
🧠 Conceptual
intermediate
1:30remaining
Purpose of Unity Profiler Markers
What is the main purpose of adding custom profiler markers in Unity code using Profiler.BeginSample and Profiler.EndSample?
ATo measure and visualize the time spent in specific code blocks during profiling
BTo reduce memory usage by marking unused code
CTo automatically optimize the marked code sections at runtime
DTo enable multi-threading for the marked code sections
Attempts:
2 left
💡 Hint
Think about what profiling means and what you want to learn from it.
🔧 Debug
advanced
2:00remaining
Identifying Cause of Garbage Collection Spikes
You notice frequent spikes in the Unity Profiler's GC Alloc column during gameplay. Which code snippet below is most likely causing these spikes?
Aint total = playerScore + enemyScore;
BVector3 pos = transform.position;
Cstring s = "Score: " + playerScore.ToString();
Dfloat speed = 5.0f * Time.deltaTime;
Attempts:
2 left
💡 Hint
Think about which operations create new objects in memory.
📝 Syntax
advanced
1:30remaining
Correct Use of Profiler API in Unity
Which of the following code snippets correctly uses Unity's Profiler API to measure a code block's performance?
A
Profiler.Begin("MyCode");
// code to measure
Profiler.End();
B
Profiler.StartSample("MyCode");
// code to measure
Profiler.StopSample();
C
Profiler.MarkSample("MyCode");
// code to measure
Profiler.EndSample();
D
Profiler.BeginSample("MyCode");
// code to measure
Profiler.EndSample();
Attempts:
2 left
💡 Hint
Check the exact method names in Unity's Profiler class.
🚀 Application
expert
3:00remaining
Optimizing a Frame Rate Drop Using Profiler Data
You profile your Unity game and see a large spike in the 'Rendering' section caused by many dynamic shadows. Which approach will most effectively reduce this spike?
AReduce the number of dynamic lights casting shadows or switch some shadows to baked lighting
BIncrease the shadow resolution to make shadows clearer
CAdd more scripts to optimize shadow calculations
DDisable VSync to allow higher frame rates
Attempts:
2 left
💡 Hint
Think about what causes rendering spikes related to shadows.

Practice

(1/5)
1. What is the main purpose of using performance profiling in Unity?
easy
A. To write story scripts for the game
B. To add new game features quickly
C. To find slow or heavy parts in your game code
D. To create game art and animations

Solution

  1. Step 1: Understand performance profiling

    Performance profiling is used to check which parts of the game code take the most time or resources.
  2. Step 2: Match purpose with options

    Only To find slow or heavy parts in your game code talks about finding slow or heavy parts, which matches the purpose of profiling.
  3. Final Answer:

    To find slow or heavy parts in your game code -> Option C
  4. Quick Check:

    Profiling purpose = find slow code [OK]
Hint: Profiling finds slow code parts to improve performance [OK]
Common Mistakes:
  • Confusing profiling with adding features
  • Thinking profiling creates art or story
  • Mixing profiling with gameplay design
2. Which of the following is the correct way to mark a code section for profiling in Unity?
easy
A. Profiler.BeginSample("MyCode") and Profiler.EndSample()
B. Profiler.MarkStart("MyCode") and Profiler.MarkEnd()
C. Profiler.StartSample("MyCode") and Profiler.StopSample()
D. Profiler.Profile("MyCode")

Solution

  1. Step 1: Recall Unity profiling API

    Unity uses Profiler.BeginSample and Profiler.EndSample to mark code sections.
  2. Step 2: Compare options with correct API

    Only Profiler.BeginSample("MyCode") and Profiler.EndSample() matches the exact method names used in Unity.
  3. Final Answer:

    Profiler.BeginSample("MyCode") and Profiler.EndSample() -> Option A
  4. Quick Check:

    Correct API = BeginSample and EndSample [OK]
Hint: Use BeginSample and EndSample to mark profiling code [OK]
Common Mistakes:
  • Using StartSample/StopSample instead of BeginSample/EndSample
  • Using non-existent Profiler methods
  • Forgetting to call EndSample after BeginSample
3. What will be the output in the Unity Profiler if you run this code snippet?
Profiler.BeginSample("HeavyTask");
for (int i = 0; i < 1000000; i++) { var x = Mathf.Sqrt(i); }
Profiler.EndSample();
medium
A. The Profiler shows a sample named 'HeavyTask' with time taken for the loop
B. The Profiler shows an error because of missing Profiler.StopSample()
C. No profiling data is recorded because Profiler.BeginSample is inside a loop
D. The Profiler shows a sample named 'HeavyTask' but with zero time

Solution

  1. Step 1: Understand Profiler.BeginSample and EndSample usage

    The code marks the start and end of a heavy task loop, so the Profiler records the time spent inside.
  2. Step 2: Analyze Profiler output

    The Profiler will show a sample named 'HeavyTask' with the time taken to run the loop, which is significant.
  3. Final Answer:

    The Profiler shows a sample named 'HeavyTask' with time taken for the loop -> Option A
  4. Quick Check:

    Profiler records marked code time = The Profiler shows a sample named 'HeavyTask' with time taken for the loop [OK]
Hint: Profiler shows time for code between BeginSample and EndSample [OK]
Common Mistakes:
  • Confusing EndSample with StopSample
  • Thinking profiler needs to be inside the loop
  • Assuming profiler shows zero time for heavy code
4. Identify the error in this profiling code snippet:
Profiler.BeginSample("UpdateLoop");
UpdateGameObjects();
// Missing Profiler.EndSample()
medium
A. Profiler.BeginSample() should be called after UpdateGameObjects()
B. No error, the code is correct
C. Profiler.BeginSample() and EndSample() cannot be used in Update()
D. Profiler.EndSample() is missing, causing incorrect profiling data

Solution

  1. Step 1: Check profiling code structure

    Profiler.BeginSample must be paired with Profiler.EndSample to mark the code section properly.
  2. Step 2: Identify missing EndSample call

    The code is missing Profiler.EndSample(), which causes the Profiler to not close the sample and gives wrong data.
  3. Final Answer:

    Profiler.EndSample() is missing, causing incorrect profiling data -> Option D
  4. Quick Check:

    Missing EndSample causes profiling errors [OK]
Hint: Always pair BeginSample with EndSample to avoid errors [OK]
Common Mistakes:
  • Forgetting to call EndSample after BeginSample
  • Calling BeginSample after the code instead of before
  • Thinking profiling can't be used in Update()
5. You want to profile two different parts of your game code separately: physics calculations and AI updates. How should you use the Profiler API to get clear timing for both?
hard
A. Call Profiler.BeginSample("Physics") once at game start and Profiler.EndSample() at game end, same for AI
B. Call Profiler.BeginSample("Physics") before physics code and Profiler.EndSample(), then Profiler.BeginSample("AI") before AI code and Profiler.EndSample()
C. Use Profiler.BeginSample("Physics") and Profiler.BeginSample("AI") at the same time without EndSample()
D. Only use Profiler.EndSample() after both physics and AI code without BeginSample()

Solution

  1. Step 1: Understand separate profiling sections

    To profile two parts separately, you must mark each section with its own BeginSample and EndSample calls.
  2. Step 2: Apply correct usage for both parts

    Call Profiler.BeginSample("Physics") before physics code and Profiler.EndSample(), then Profiler.BeginSample("AI") before AI code and Profiler.EndSample() correctly calls BeginSample and EndSample around physics code, then again around AI code, giving clear timing for each.
  3. Final Answer:

    Call Profiler.BeginSample("Physics") before physics code and Profiler.EndSample(), then Profiler.BeginSample("AI") before AI code and Profiler.EndSample() -> Option B
  4. Quick Check:

    Separate samples need separate BeginSample/EndSample pairs [OK]
Hint: Use separate BeginSample/EndSample pairs for each code section [OK]
Common Mistakes:
  • Calling BeginSample once for whole game session
  • Starting two samples without ending them
  • Using EndSample without matching BeginSample
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard