Why does having a Debug.log() make my script run twice as fast?
I have a script that runs A LOT slower when a debug line is removed. Consistently too. with debug line enabled, it runs at an average 4.5ms (230fps), but when I remove the debug line, it runs at 7.5-9ms (115-130fps) what the hell?
I'm using the FPSDisplay script from the wiki http://wiki.unity3d.com/index.php?title=FramesPerSecond
Answer by UnityCoach · Dec 29, 2016 at 05:29 PM
You can try the following : - remove/disable the FPS script - open the Profiler, and under CPU, uncheck everything but Scripts - start playing, pause, scrub through the timeline - in the Profiler window, look for BehaviourUpdate, and find your script in it - then look for the number of milliseconds it used to process that frame - do the same with and without the Debug.Log
For reference, 60FPS = 16ms/per frame You can't rely on Framerate estimation. There's usually a lot more going on in the background.
Thank you. I learned more about the Profiler from your comment than I ever have from any other source. I didn't even know you could un-check those little colour squares. :P
It does indeed appear to be a problem with the FPS script. The debug.log actually makes the frame ms jump from 0.01 to 1.42.
Now, how do I promote your comment to the answer?
Et voilà! :) Yes, the Profiler is a powerful tool. So, you say adding the Debug.Log takes 1.42 ins$$anonymous$$d of 0.01 without? That's a lot! I remember a tutorial from Prime31 showing how to remove all Debug instructions from final builds using Attributes on $$anonymous$$ethods. There it is.
Amazingly, analyzing 4 seperate audio sources, storing values in a 4x1024 array and making 4 cubes bounce only uses 0.01ms frametime (closer to 0), yet to print the results to log costs a whopping 1.4!
Thanks for the vid, I'll check it out.
Your answer
Follow this Question
Related Questions
void Update() problem 1 Answer
Debug.Log printing 2 values 1 Answer
How can I avoid using switch and for too much? 2 Answers