The Cost of Unity's Debug.Log and the Depth of the Call Stack
Hello. I'm Hirayama from the Technology Department.
This post is about how the time Debug.Log takes depends on the depth of the call stack. Here's the whole conclusion:
Debug.Log gets heavy when you call it from somewhere deep.
It's nothing earth-shattering, so if you're busy, feel free to stop here.
What is this load? This can't be normal
The other day I was looking into a performance problem and found that the culprit was Debug.Log.
"Well, of course it's heavy."
Anyone would think that, but this was abnormal: the profiler showed over 5ms, with GCAlloc close to 100KB. If it takes that long in the editor, it could reach about 20ms on an actual smartphone.
Wondering "was it always this heavy?", I took a closer look, and noticed something particular about that program.
"The call stack is deep."
Deep Profiling showed about 40 levels. In other words, Debug.Log was being called 40 layers down a chain of functions calling functions calling functions. The profiler also showed a function called StackTraceUtility.ExtractStackTrace() eating quite a lot of time, which led me to guess "maybe the heavy part is building the call stack string that appears in the log?"
From there it was a matter of experimenting to check whether that was true.
The experiment
public class Main : MonoBehaviour
{
int callDepth;
TimeSpan time;
int count;
void OnGUI()
{
var us = (double)time.Ticks / (double)(TimeSpan.TicksPerMillisecond * count);
GUI.Label(new Rect(0f, 0f, 400f, 50f), us.ToString("F3"));
var newCallDepth = (int)GUI.HorizontalSlider(new Rect(0f, 50f, 400f, 50f), (float)callDepth, 0, 100);
if (newCallDepth != callDepth)
{
callDepth = newCallDepth;
count = 0;
time = TimeSpan.FromTicks(0);
}
}
private void Update()
{
var t0 = DateTime.Now;
LogAtDepth(callDepth, 0);
var t1 = DateTime.Now;
time += (t1 - t0);
count++;
}
void LogAtDepth(int targetDepth, int currentDepth)
{
if (currentDepth >= targetDepth)
{
Debug.Log("Log() called " + targetDepth);
}
else
{
LogAtDepth(targetDepth, currentDepth + 1);
}
}
}
I threw together a recursive function that calls Debug.Log() at a given depth. You set the depth with a slider, and the time taken is shown on screen in milliseconds.
Now for the results.

The top is depth 0 and the bottom is depth 100. Both GCAlloc and processing time have shot up spectacularly.
I'd rather not be scolded with "profiling without building is for amateurs", but I didn't measure in a build this time. I actually did try, but when you build with IL2CPP, the recursive function above gets optimized into a non-recursive form, so the call stack ends up shallow. It's tail recursion optimization. In real code that optimization usually isn't possible, so I expect the situation would be the same even in an IL2CPP build.
...So?
Quite a few people probably think "debug features are naturally heavy, and that's not a problem". What's wrong with something heavy if it never ships in the product? That's a perfectly natural way to think.
But keeping the speed gap between debug and release builds small has various benefits, because you can playtest with more powerful debug features enabled without hurting the frame rate.
If the debug build performs too poorly, nobody plays it any more, and bugs that should have been caught can even end up in the product.
People will reach different conclusions about "how fast should a debug build be?", but it's fair to say faster is better. And if so, it's also fair to say "I want to avoid implementations that make the debug build unnecessarily slow".
So what do we do?
First, ask "is that log actually meaningful?" Meaningless logs just make things heavier for nothing.
Next, if we're acting on this result, it's worth asking "does that log need the call stack?" The options include:
- Prepare a separate lightweight log function that doesn't look at the call stack, and have it write to a file
- Change the setting in PlayerSettings so the call stack isn't output
For the latter, there's an item called Logging under "Other" in PlayerSettings, with a row of checkboxes like this:

If you set, for example, "None for Log only", Debug.Log stops outputting the call stack. For what Script, Full and so on mean, see the official manual.
And finally, ask "why is the call stack so deep?" In this case, the biggest factor was nested StartCoroutine calls.
void A(){ StartCoroutine(CoA()); }
IEnumerator CoA(){ ... }
Imagine something like this. When you want to run some asynchronous process CoA and don't need to wait for the result, you'd want to just toss it to StartCoroutine and be done. If you do that a lot, writing StartCoroutine every time gets tedious, so you'd write a function like A() above to kick it off easily.
The problem is when there are several functions like this.
void A(){ StartCoroutine(CoA()); }
IEnumerator CoA(){ ... }
void B(){ StartCoroutine(CoB()); }
IEnumerator CoB()
{
A();
...
}
Say you have A and B, which are really just wrappers around the asynchronous processes CoA and CoB. The trouble starts when CoB needs A's processing and calls A() rather than CoA(). Apparently, nesting StartCoroutine like this makes the call stack deep.
IEnumerator CoB()
{
yield return CoA();
...
}
Written like this, the problem goes away, but the flow of processing is different. Before, the rest of CoB and A ran in parallel; here, the rest of CoB doesn't run until CoA finishes. Still, if waiting for it to finish is fine, I think this is the safer way to write it.
In the code I saw the other day, StartCoroutine was nested three deep, and Debug.Log was being called at the very bottom.
Closing
Realistically, I don't think there's a clean solution, especially once you've already built a fair amount.
- Accept that the debug build is slow to some extent. Play with the release build
- Personally I can't agree with this...
- Give up logging in places where spikes matter.
- I'd like to avoid this, since it weakens bug detection...
- Fix only the easy-to-fix places where StartCoroutine is nested.
I feel stopgap measures like these are all you can do. Declaring "from now on, Debug.Log won't output the call stack" once development is well under way would probably be hard too.
If possible, I'd recommend:
- Wherever spikes matter in the first place (such as when running at 60fps), use a lightweight log that writes to a file without the call stack instead of Debug.Log
- If you can decide at the start of the project that "Warning outputs the call stack but Log doesn't", that's fine too.
- Be careful not to nest StartCoroutine
Debug.Log doesn't necessarily write to the log right after the line executes, but if you roll your own, you can guarantee that "once execution passes that line, it's definitely in the file", which can be handy when debugging. You often want to know "whether it got as far as a certain line", don't you?
As for nesting StartCoroutine, it makes the flow harder to follow and increases GCAlloc too. Personally, I'd want to avoid it even without this logging issue.