velocity.magnitude causes heavy lag

This is more of a question to Unity team.

A little help script I use to observe object velocity and see if my other coding works:

function Update () {

Debug.Log (gameObject.transform.rigidbody.velocity.magnitude);

}

Currently it’s assigned to a simple projectile prefab.
When it’s active, my framerate drops from over 300 fps to about 30 fps. When I remove it, framerate remains almost intact.

Is this calculation so ‘heavy’ it causes such a drastic lag and why is it affecting the build version too (since it’s used only in a Debug.Log)?

Debug.Log itself is very slow. If you’ve got that assigned to a projectile and you’re firing a bunch of them that’s probably what’s slowing things down.

It happens in the build version as well because Debug.Log is still called normally in a build version, it is not specific to the editor environment. The log messages all go into a log file in your games data directory.

Is print a better solution, then ?

no, not using any text output if not required is best.

and magnitude itself is also costly, yeah, it does a squareroot calculation which after logarithm is the second slowest operation

Thanx Eagle32, this is a very useful information.

It’s good to know not to leave any of these help scripts/lines active when making a build version.

Cheers!

for what i experienced with it , not really …

at this point you better make you a custom debug message on screen using some guitext that you can toggle on and off , then get rid of it and it script once you build your final game.

by the way U3 now allow you to use mono debugger break points…

There’s slow and then there is slow. Yes the square root operation is one of the slower mathematical operations. But slower is a relative term.

Here’s the results of a quick test I just did.

Total execution time for 10,000 Debug.Log calls = ~5 seconds
Total execution time for 10,000 magnitude calls = ~5 ms

var magntude = gameObject.transform.rigidbody.velocity.magnitude;
function Update () { 

magntude = gameObject.transform.rigidbody.velocity.magnitude;
Debug.Log (magntude); 

}

Try this instead, it may solve the heavy lag :smile:.

if you go with it to the real player the difference will change, cause the major point of slowness is the list that has to output all the debug trashing in realtime, which is stuttering massively if you update it the whole time

Sorry, I don’t understand what you’ve written there. The real player?

That’s no different from the original code, except for unnecessarily declaring a variable, so it won’t have any effect.

As far as print goes, that is Debug.Log.

–Eric

I completely agree, but there was a scenario close to this where it improved performance MANY times over.

Unlikely; 99.9% of the time spent is because of Debug.Log itself; nothing you can do with the script will affect that.

–Eric

Perhaps it improved speed in your “MANY times over” because by assigning an initial value to the untyped variables allowed the compiler to infer the desired type and strongly-type/compile your code instead of resorting to run-time dynamic duck typing.

As pointed out, the Debug.Log statements are the culprit.

I think what you wanted was:

var rigid= gameObject.transform.rigidbody;
function Update () { 

Debug.Log (rigid.velocity.magnitude); 

}

This will have noticeable improvements on weaker devices, however in this case it is 99%+ the Logging that is causing the slowdown.

Also in C# you can wrap the Debug Logs in a function using the Conditional attribute and have the only exist within the Editor and have the calls themselves stripped out in the final build

[System.Diagnostics.Conditional("UNITY_EDITOR")]
	public static void Throw(string message)
	{
		Debug.Log(message);
	}

This will help prevent you from missing Debugs in the final build.