UAnimInstance Performance

Hi,

I’m not super certain on the efficiency of Timers so I wanted to know for NativeUpdateAnimation in a UAnimInstance, which of the following are more efficient for updating a variable that can cannot be updated more frequently than every 0.1 seconds (in this case, bReceivedInitDir)?

A: Using DeltaSeconds

void UHumanoidAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
    Timer += DeltaSeconds;

        ...(several other variable updates)...

	TimeStarted = Timer;

	if (!Executing)
	{
		bReceivedInitDir = true;
		Executing = true;
	}

	if (Timer - TimeStarted >= 0.1f)
	{
		Timer = 0.f;
		Executing = false;
	}
}

B: Using Timers

void UHumanoidAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	if (!GetWorld()->GetTimerManager().IsTimerActive(UpdateReceivedHandle))
	{
		GetWorld()->GetTimerManager().SetTimer(UpdateReceivedHandle, this,
			&UHumanoidAnimInstance::SetReceivedDirTrue, 0.1f, false);
	}
}

Thank you!

don’t be much sensitive on performance and optimization :slight_smile:
you surely have bigger bottlenecks. for instance a high poly mesh without LOD drops the FPS too bad.

but the first one is better in my opinion. (though it depends and I feel like you didn’t implement it right).
because it has less computation and the memory that ‘this’ refers to is more likely in the CPU cache since you are already changing the member variables.

btw the timer was not used well. GetWorld()->GetTimerManager().IsTimerActive does lots of check every frame. you can simply start the timer in NativeInitilize with loop enabled.

if you want spacial interval in your loop maybe this one could help you.

void UHumanoidAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	const float interval = 0.1f;
	
	//several other variable updates
	
	this->AccumulatedTime += DeltaSeconds;

	if(this->AccumulatedTime >= interval)
	{
		this->AccumulatedTime -= interval;
		
		//ding ding execute your code here
		
	}
}

I’d guess technically the first approach could be faster (some really detailed reading on pointer vs variable access speed if you’re interested) but I doubt it would matter in any significant way.