So sometimes I like to put debug output in small tight loops and sample values to make sure something is working correctly. Well, when I use debug.log, I noticed that not only does it take fffoooorrreeeevvveeerrr, but it also completely hangs the app. I took the liberty of getting some metrics.
The printf, for 3 runs, took 2747ms, 2800ms, and 2748ms. The OutputDebugString was slower, it took 20777ms, 22000ms, and 25115ms. (It was also less consistent).
Now, the same logic in Unity (C#):
float curTime = Time.realtimeSinceStartup;
int loop = 0;
while (loop < 40000)
{
loop++;
Debug.Log(""+loop);
}
float et = Time.realtimeSinceStartup - curTime;
et *= 1000.0f;
Debug.Log("Elapsed time "+et+"ms");
And the results:
97561ms
98282ms
97478ms
A whole minute and a half! What is that?!? Not only that, I should point out that Unity is completely locked up at that point. If I don’t have the patience to wait for everything to filter thru, there’s nothing I can do but kill the whole editor, which sucks if I didn’t save a map change.
Is there a way to make that faster? Considering that debug output is pretty much the ONLY way to debug scripts, it’s kinda important…
In the first example you don’t write anything to a console and file (which is what Debug.Log does), you only write to a memory block
And then to compare “the speed” you use Debug.Log in unity?
also you tell it to hang the application, so please blame it on you that it does not do anything anymore until its done with the 40k writes to the log file and log stream. That code was just a very good example of how code in unity (or generally outside of minimalistic benchmarks).
you know, comparing speed of things and claiming one is faster makes really little to no sense if you don’t know what you are actually using and measuring at all.
correct Unity code for this comparision would be:
float curTime = Time.realtimeSinceStartup;
int loop = 0;
string debugLine = ""
while (loop < 40000)
{
loop++;
loop += loop + "\n";
}
float et = Time.realtimeSinceStartup - curTime;
et *= 1000.0f;
Debug.Log("Elapsed time "+et+"ms");
Additionally if you test it in the editor, your C++ code would require further interception code etc similar to how the editor actually is gathering those information from the unity player runtime.
I am measuring the speed at which I, the script programmer, get information about what my scripts are doing. I don’t care about the low level specs, all I care about is how quickly I can see (with my eyes, mind you) the values I need to debug.
Please educate me, if there is something other than Debug.Log in Unity that is faster, what is it?
Debug.Log is by far fast enough when used correctly and realistically.
Doing a 40k loop is no realistic usage, thats just crap to tell the truth. You will never have such a case in your program (or if you have you have other problems as your code then will be your major problem to solve, not the visual output).
Another way for direct feedbacks are GUIText / OnGUI + GUI.Label / GUILayout.Label
So, I have this terrain, with autogenerating pathnodes. I’m doing pathfinding. I found that if I try to calculate a path that won’t work (because of slope restrictions, for example), the system will evaluate every possible node before returning a failed path, thereby taking a lot of time. I’ve found an optimization in precalculating every possible neighbor and assigning a set value that is the same for each neighbor. Basically, if you can imagine telling the path manager where a spawn point is, then the path manager sets a “set index” for every node that the spawn point can reach, if it’s unset. Then, when someone selects an endpoint, if the set indices of both the start point and the end point are different, the path is invalid, and I know from the outset.
I wanted to make sure my neighbor crawl was coded correctly, because it seemed to be stuck in an infinite loop. How could I do that? My god, I put a Debug.Log in a loop! Impossible! But because it was so slow, I had to actually keep a counter of the number of logs and stop logging after a certain point to make sure I got any information at all. Fortunately, I got enough information to find the bug and fix it.
Debug.Log in a loop is not impossible, you just have to realize that your harddrive is the problem here (if you use it in the editor, additionally the handling of the large amount of entries you will generate) as it is the by far slowest aspect when writting files and until it has been written, the app has to wait.
Your code likely even writes for the same node multiple times.
so you would write all lines into a string and store it in a file once your are done to process it then.
Or you use System.IO to do it, should you want more control over the write process.
What you would use in such a case likely is storing it in a string and presenting that through an editor expansion if it is for the editor or a guitext / ongui label in the client or as mentioned above a file.
In the editor you have additionally the possibility to draw lines from within Debug to give you straight visual feedback for calculations that are related to the world and not pure “code things”.
As for pathing itself: Would recommend to check out AngryAnt path or the navmesh based path finding system, both available for free.