While our game may be running without any issues in the editor or even in…
Profiling Code Blocks
In this post I’m going to show you how you can profile your code blocks in order to identify potential issues and make your game run smoother!
In order expose your code’s performance in the Unreal Engine profiler tool you really need two things:
- A stat group, which is the “category” of the code you’re profiling in Unreal Engine profiler
- A cycle stat, which is the name of the code block you’re profiling.
A stat group can contain several different cycle stats. Both stat groups and cycle stats are used to group up statistics in the profiler .
Profiling code
For the sake of this post let’s imagine that we want to profile a function named FindPrimeNumbers. I have created a dummy actor component and inside its header file I added the following property and function:
1 2 3 4 5 6 7 8 |
protected: UPROPERTY(EditAnywhere) int32 PrimeNumbersToFind=5000; public: UFUNCTION(BlueprintCallable) TArray<int32> FindPrimeNumbers(); |
Inside its source file, right after I’ve included the required header files for my class I declared a stat group and a cycle stat that belongs to the newly created stat group:
1 2 3 4 |
/* Declaring a stat group that will be visible to the profiler, named Orfeas*/ DECLARE_STATS_GROUP(TEXT("Orfeas_Game"), STATGROUP_Orfeas, STATCAT_Advanced); /* Declaring a cycle stat that belongs to "Orfeas", named Orfeas-FindPrimeNumbers*/ DECLARE_CYCLE_STAT(TEXT("Orfeas - FindPrimeNumbers"),STAT_FindPrimeNumbers,STATGROUP_Orfeas); |
Then, I created the following implementation for the FindPrimeNumbers function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
TArray<int32> UHeavyComp::FindPrimeNumbers() { {//Used to declare the "start" of the block we're profiling //Tell the engine that we're profiling the currect block using the STAT_FindPrimeNumbers stat SCOPE_CYCLE_COUNTER(STAT_FindPrimeNumbers); //A really bad way to find prime numbers //Don't use this in an actual application TArray<int32> Primes; Primes.Add(1); Primes.Add(2); Primes.Add(3); int32 CurrentNumber = 4; while (Primes.Num() <= PrimeNumbersToFind) { bool IsPrime = true; for (int32 index = 2; (index < CurrentNumber / 2) && IsPrime; index++) { if (CurrentNumber % index == 0) { IsPrime = false; } } if (IsPrime) { Primes.Add(CurrentNumber); } CurrentNumber++; } }//Closing the block of code we're profiling //This means that the following two lines won't be profiled and as a result they won't have an impact in the STAT_FindPrimeNumbers used above int32 DummyAction=0; DummyAction++; return Primes; } |
Once I compiled the project, I’ve added that component in the main character and call the FindPrimeNumbers function in each Tick just to showcase the profiler’s result. So, while running the game with the profiler on for a couple of seconds this is the result once I load up the recorded file:
Moreover, when I’m running the game, I can type in the console the command: stat Orfeas and see the realtime impact of that Orfeas-FindPrimeNumbers stat:
And that’s an easy way to profile your code using the Unreal Engine profiler tool. Thanks for reading!
Thank you, this has helped me a ton!
Does stuff need to be added like preprocessor statements so it is not used in shipping builds?