0
\$\begingroup\$

I am learning C++ and Unreal Engine. I'm wondering if creating local variables inside functions purely to help me read the function later will cost my game/code in performance (assuming if I carry on this practise over hundreds/thousands of functions/variables).

Eg:

void APostie::MoveForward(float Value)
{
FVector Delta = GetActorForwardVector() * BaseWalkSpeed * Value * GetWorld()->GetDeltaSeconds();
FRotator NewRotation = GetActorRotation();
FHitResult Hit;
GetCharacterMovement()->SafeMoveUpdatedComponent(Delta, NewRotation, true, Hit);
}

Here the 'NewRotation' variable is completely useless. But I am intending to copy-paste this function and use it in similar input functions that will require the NewRotation to have a calculation on it.

So is it much better peformance-wise to have the function like this instead (again, imagine there are several hundred similar situations in a code solution)

void APostie::MoveForward(float Value)
{
FVector Delta = GetActorForwardVector() * BaseWalkSpeed * Value * GetWorld()->GetDeltaSeconds();
FHitResult Hit;
GetCharacterMovement()->SafeMoveUpdatedComponent(Delta, GetActorRotation(), true, Hit);
}

As you see in the second version the Rotation is set directly. Does this make any difference to game performance (including Networked games) ? Thanks

\$\endgroup\$
3
  • 6
    \$\begingroup\$ What does your profiler say? \$\endgroup\$ – Vaillancourt Feb 3 '20 at 18:18
  • \$\begingroup\$ Well, I haven't learnt to use that yet. Perhaps I will do that now, thank you \$\endgroup\$ – Krangogram Feb 3 '20 at 18:27
  • 1
    \$\begingroup\$ I can't stress this enough. Do NOT prioritise performance over readability, unless you reach a point where performance will take a big hit, which will never happen with local variables. There are countless projects out there abandoned because they are unreadable and unmaintainable, and way fewer ones abandoned because of performance. Premature optimisation can easily demotivate you and make a project unpleasant to work on. \$\endgroup\$ – TomTsagk Feb 4 '20 at 12:17
5
\$\begingroup\$

This depends on how good the compiler is at optimizing the code. I'm inclined to say it is good if not very good.

However, if you want to be sure, you got to do the experiment. Measure both versions with a profiler and see what comes up on top.


Your job is not to optimize code, it is to make a system (a game in this case). If you go ahead and do that, and it turns out that it is slow... well, you are going to get the most performance improvements per effort if you focus on the bottlenecks and the hot paths.

Those bottlenecks and hot paths are probably going to be some computation or some interaction with an external system, not some local variable.

Yes, learn to use the profiler, it will tell you where the hot paths are.


What I'm saying that you are probably worrying too much about this. Let the good people who work in creating compilers figure where inlining a local variable is a good idea, and you can figure out how to make a good game.

\$\endgroup\$
3
  • \$\begingroup\$ Ok thanks. It was just something i had been wondering about since I use a lot of these types of variables and as my code solutions get larger it could be a nightmare having to go through all of them one day to change them. I'm glad to hear it may be nothing to worry about right now. And I am certainly going to have some fun trying random things in the profiler. (Eg. I will make a program that does thousands of these types of local variables and see how this affects the system). Thanks all \$\endgroup\$ – Krangogram Feb 3 '20 at 18:54
  • 2
    \$\begingroup\$ @gadgettradersw No problem. By the way, there is a saying that goes "Premature Optimization Is the Root of All Evil" (it is an out of context quote of Donald Knuth), however don't let that prevent you from picking good algorithms and data structures from the start. \$\endgroup\$ – Theraot Feb 3 '20 at 19:04
  • 2
    \$\begingroup\$ @Krangogram you may also be interested in Is defining a variable to name a method argument a good practice?. \$\endgroup\$ – Theraot Feb 3 '20 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.