Why is debug.log so slow? When one of my scripts used Debug.Log only once each frame, it slowed the entire game down to a crawl and clipped sound effects so they sounded tinny and distorted. I noticed this after releasing the build, of course.
Most likely because it is pulling a stack trace each time, including line numbers.
Debug.Log can be super slow, using it on many instances and if it concats a lot of strings or something like blizzy said then yeah, it will be your crap down.
While it is probably slow, i’ve never seen it actually slow the game down as you mentioned…
Isn’t it stripped out of the build anyway?
It clearly isn’t, because when I took it out manually the build version sped up enormously. Besides, there are cases in which the build needs it, such as when you have to pass data thru the console (e.g. id.net requires data be passed thru the console if your game needs to use their API).
Debug.Log calls aren’t stripped, hence this forum post
We have our own logger class that internally calls Debug.Log but all methods are decorated with the Conditional attribute.
This way we can strip those calls if needed.
An example of the logger class we use:
/// <summary>
/// A logger that wraps Unity's internal logger.
/// Calls to its methods are stripped in case the LOGGER_SYMBOL is not defined.
/// </summary>
public sealed class Logger
{
public const string LOGGER_SYMBOL = "ENABLE_LOG";
[System.Diagnostics.Conditional(LOGGER_SYMBOL)]
public static void Log(object message)
{
Debug.Log(message);
}
}
Calls to Logger.Log are stripped away (not even compiled into the final binary) in case “ENABLE_LOG” symbol is not defined.