[performanсe] SetTransform vs SetLoc+SetRot

Hey, i would like to hear your opinions, on which approach will yield better performance result

case 1: Set Transform

case 2: Set Location + Set Rotation

in both cases, the Scale is not affecting anything, and you can assume the operation happens in a big loop or tick event, so every bit of performance is important.

Thank you in advance :slight_smile:

Why don´t you simply messure it? But my gut feeling tells me Transform will be faster (and no I won´t look at source, or messure it to figure it out for you ;P)

two reasons:

  1. if somebody have tested it before me, it will be shorter before i do it myself
  2. it will serve as a google search result for these who care about the topic, and they will get an answer (because i will post it anyway if nobody does in some time)

:slight_smile:

I took a quick look in to the sources
it seems like setting the transform is much faster.

Transform just copies and sets the values.

SetActorLocation alone does more things - It seems like it tries to simulate movement does various overlap and collision checks.

it sounds very strange. i will soon make a test and post it here

Can’t wait for it - very interesting what would be the result -
just don’t forget to make a large enough loop so the gap will be noticible and more precise.
And don’t use constant lications - since CPU may optimize it in L caches.

Do something like index multiply by something constant (don’t use randoms as well it may kill all the test by it’s own complexity)

Hmm… then make sure that nativization is off :slight_smile:
Hard to say - it surely not optimizes the BPs but it could optimize the C++ core code under the hood - which is not a big deal so you seem to be right.

thanks for the tips.


are you sure however that CPU does optimizations even on blueprints? they seem pretty unoptimized by nature as long as they don’t get nativized in the build

So, do you get any results?

i am not available in the next few days, but i will test is asap :]

SetTransform() is faster in blueprints.

I have run for loops of 8000 iterations per tick:

case 1: GetTransform -> SetTransform           [-50 FPS]                               
case 2: GetLoc + GetRot -> SetTransform (break pin / make transform to combine loc+rot)    [-55 FPS]
case 3: GetLoc -> SetLoc + GetRot -> SetRot   [-56 FPS]

The First case clearly won. The difference is nearly 10% more performance when using GetTransform, rather than GetLoc + GetRot, even though it contains one more vector in it (scale)

**I did not test it in C++**

, but i think it will be the **opposite** there. i would love to hear if someone does it.

finally did it, you can see the results in my answer here :]

No, the result will be roughly the same… SetLocation has much more code and branches inside it (dealing woth physics/sweeps and what not). SetTransform is roughly straight forward.

interesting, SetTransform has sweep and teleport too though, are you sure?

I took a look in to the source again
SetLocation() will call a lot of functions and branches (where it takes current and relative transform in each and does some math on it),
and in the end it will call this function:

bool USceneComponent::InternalSetWorldLocationAndRotation(FVector NewLocation, const FQuat& RotationQuat, bool bNoPhysics, ETeleportType Teleport)

Which is quite big and also it recomputes the rotation again.
So:: even without continuing to look further in code:

SetLocation setting both location and rotation (While SetActorTransform is much much much narrower function)

bool AActor::SetActorTransform(const FTransform& NewTransform, bool bSweep, FHitResult* OutSweepHitResult, ETeleportType Teleport)
{
	// we have seen this gets NAN from kismet, and would like to see if this
	// happens, and if so, something else is giving NAN as output
	if (RootComponent)
	{
		if (ensureMsgf(!NewTransform.ContainsNaN(), TEXT("SetActorTransform: Get NAN Transform data for %s: %s"), *GetNameSafe(this), *NewTransform.ToString()))
		{
			RootComponent->SetWorldTransform(NewTransform, bSweep, OutSweepHitResult, Teleport);
		}
		else
		{
			if (OutSweepHitResult)
			{
				*OutSweepHitResult = FHitResult();
			}
		}
		return true;
	}

	if (OutSweepHitResult)
	{
		*OutSweepHitResult = FHitResult();
	}
	return false;
}

So basically from inspecting the Source:

SetLocation is intended for movement of the object
While SetTramsform intended to just copy the Transform without too much things inside.

BTW FPS is not applicable to a measuring!
The only thing cappable is a time.

Of course. The FPS were given just for relative measuring rather than benchmarking. clearly the one with the best FPS is the one with the best benchmark