[New Wiki] How to Count CPU Cycles of Individual Lines of Code in Your Project, and Show in Profiler

Dear Community,

I’ve just created a wiki that shows you how you can count the CPU cycles of individual lines of code in your code base!

I show you how you can expose your own STAT groups and create your own named sections of code that the awesome UE4 Profiler will then add to its GUI !

This makes it super easy for you to monitor the perfromance of your game code throughout development!

This wiki will also help you find the code that is causing a performance hit without having to guess!

In programming its very important to be able to precisely know here the CPU is getting bogged down, without having to assume or guess, but know for sure via actual profiling!

This wiki I am sharing with you enables you to use the visual UE4 Profiler in the editor to track down individual lines of your code and count CPU cycles!

Enjoy!

Rama


**Wiki Link**

**How to Count CPU Cycles of Blocks of Code in Your Code Base**
https://wiki.unrealengine.com/Profiling,_How_To_Count_CPU_Cycles_Of_Specific_Blocks_Of_Your_Game_Code

Pic of What This Wiki Enables You To Do

In this picture you can see I’ve created my own custom STAT so that the UE4 Profiler can track a specific block of my game code that I called “Joy ~ PerformSphereMovement”.

I’ve successfully tracked the CPU cycles of a section of my own project-level code base and exposed this information to the very friendly GUI of the UE4 Profiler!

Yay!

This picture shows that the UE4 Profiler has confirmed my guess that a certain block of my code was causing almost 97% (96.6) of the performance hit for all the character tick code in my entire code base!

It saves me hours of time to be able to easily narrow down what block of code in my rather large character code base is causing literally 97% of the character-code performance hit!

95f353660bfde4828748ebcc1cabbfac8704395c.jpeg


**Further commentary on the pic**

In my own code base I had a 10 class inheritance hierarchy for my game character, and the UE4 profiler was simply telling me that the character "Self" was costing 37% of my total performance hit.

I created a SCOPE_CYCLE_COUNTER (as I am showing you how to do in this wiki) for the function that I thought was probably taking all the performance, and I was right! If I didnt guess right the first time, could have created several SCOPE_CYCLE_COUNTER until I found where the performance hit was, and measured how long the code was actually taking.

The most important thing is that I enabled the awesome UE4 Profiler to help me narrow down the performance hit in my game code to just a single function / block of code, and so with that info I can easily address the performance hit, knowing it is worth the effort to rewrite the code!

**The UE4 Profiler is your friend,** letting you know where time should justifiably be spent refining code and algorithms, and also letting you know when the code you thought might be slow was actually lightning fast!
	

UE4 Documentation on Profiler

I assume you are familiar with the basics of the UE4 profiler in this tutorial.

If you have not yet seen what the profiler can already do for you, I recommend reading the Epic Documentation and trynig it out!

Epic Documentation on the Amazing UE4 Profiler

	
Running The UE4 Profiler

Type in the in - game console to  Start Profile:



```


stat startfile


```



Type in the in-game console To Stop Profile



```


stat stopfile


```



Opening Your Profiled Game Session Data in the Editor

Go to Window->Developer Tools->Session Front End

Then click on the profiler button!

89b91dbf92b5d73fe955c15a7544e39149b15ad5.jpeg

Then you can load your file that you saved!

It will be under the Saved/Profiling directory :slight_smile:

Always check the date to make sure you are looking at the right file!


**Self**

You'll notice that in your game code there will be huge blocks called "Self" which indicates code that has not been divided up into cycle-counted sub-sections!

Well here is how you can sub-divide Self into your own chosen named categories, neatly organizing and cateloging your own code base!

Creating Your Own Stat Group

Let’s say you have class hierachy of classes for your in-game Character.

You want to subdivide the inner workings of your entire character code into CPU cycle-counted code blocks.

In the highest level of your class structure, in the .h, declare your category



//For UE4 Profiler ~ Stat Group
DECLARE_STATS_GROUP(TEXT("JoyBall"), STATGROUP_JoyBall, STATCAT_Advanced);



**Creating the Stat**

In the .cpp where you want to track a particular function body, put this at the top just below the #includes



```


/*

	By Rama

*/
#include "Joy.h"
#include "JoyBall.h"

//For UE4 Profiler ~ Stat
DECLARE_CYCLE_STAT(TEXT("Joy ~ PerformSphereMovement"), STAT_PerformSphereMovement, STATGROUP_JoyBall);


```



Please note you can create as many CYCLE_STAT's as you want for your particular STATGROUP !

And you should have one DECLARE_CYCLE_STAT for each function body/scope that you want to count cycles for

Using the Stat

At the very top of the scope of the function you want to track, put the SCOPE macro.
Everything within the brackets of the scope you put the SCOPE_CYCLE_COUNTER in will be cycle-counted by the profiler!



void AJoyBallMovement::PerformSphereMovement()
{
	SCOPE_CYCLE_COUNTER(STAT_PerformSphereMovement);

    //... your code that you want to test the performance of and have show up in the profiler
	
} //Cycle count scope ends here -Rama


Please note your scope can be within a single function, just make sure to give such a stat an appropriate name like YourFunction_Internal or something
Again, the SCOPE_CYCLE_COUNTER will cycle-count within its brackets



void AJoyBallMovement::PerformSphereMovement()
{
	//First part of this function, code that wont be cycle counted
	ConsoleCommand("Joy");
	//... etc

	//You can scope any lines of code you want by adding brackets!
	{
		SCOPE_CYCLE_COUNTER(STAT_PerformSphereMovement);
		int32 Parameter = 200;
		YourFunctionThatYouThinkMightBeSlow(Parameter);
		//other code to cycle count
		
	} //Cycle count scope ends here -Rama
	
	
	//More code that wont be cycle counted
	ConsoleCommand("~~~~~");
        //... etc
} 



**Wiki Link**

**How to Count CPU Cycles of Blocks of Code in Your Code Base**
https://wiki.unrealengine.com/Profiling,_How_To_Count_CPU_Cycles_Of_Specific_Blocks_Of_Your_Game_Code
	
	

Conclusion

You now know how you can CPU cycle-count individual lines of your game code base, and expose this information to UE4’s super awesome GUI Profiler!

Enjoy!

Rama

Awesome profiling work Rama. This will become very useful for optimizations =)

Or you can, you know, just run VTune … :slight_smile:

Hi Rama,

I don’t know where to begin when it comes to profiling a game. Do you provide this as a service for projects or provide an overview of how to go about it?

Thanks

Yes I can help with profiling, session requests / scheduling preferences can be sent to me via PM :slight_smile:

Rama

But, the UE4 Profiler is free and Beautiful, and an integrated solution for all UE4 Projects that has an amazingly detailed GUI!

And… VTune is $900 USD!

I think I will stick with the UE4 profiler :slight_smile:


**Thank You Epic**

**Thank you Epic for this free and powerful C++ Code profiler tool!**

:)

Rama

Wow this is awesome, I had always assumed there was a way to do this but couldn’t never see how from the documentation. Thanks!

Gotta dig into this this weekend. Thanks Rama!

True, that :slight_smile: VTune is great when you’re asking yourself “why am I missing L2 cache and where?”
Unreal Profiler seems quite handy for questions slightly higher level than that!

Great to hear from you Jwatte!

You’re welcome! Hee hee!

:slight_smile:

Rama

How to Count CPU Cycles of Individual Lines of Code in Your Project, and Show in Profile

See my original post to learn how you can label individual sections of your C++ code with your own chosen name and count the CPU cycles that UE4 is spending in that block of code for all of your instances of that class!

You can count CPU cycles of individual lines of code and expose this information to a friendly GUI in the UE4 Editor!

Enjoy!

:slight_smile:

Rama

95f353660bfde4828748ebcc1cabbfac8704395c.jpeg]([New Wiki] How to Count CPU Cycles of Individual Lines of Code in Your Project, and Show in Profiler - C++ Gameplay Programming - Unreal Engine Forums)

Oh the joy of counting the CPU cycles of custom-nameable blocks of my own code!


**How to Count CPU Cycles of Individual Lines of Code in Your Project, and Show in Profile**

See my original post to learn how you can label individual sections of your C++ code with your own chosen name and count the CPU cycles that UE4 is spending in that block of code for all of your instances of that class!

**You can count CPU cycles of individual lines of code and expose this information to a friendly GUI in the UE4 Editor!**

Enjoy!

:)

Rama

C++ Profiling Powers

Please see my original post to learn how you can count the CPU cycles of individual blocks of code within your code base, supplying custom names of your own choosing for each block!

Using In-Engine tools you can profile to your heart’s content!

:slight_smile:

Rama

Keep in mind that any stat based profiling can introduce an observer effect if used too heavily. If you were to put a cycle counter in to a tight loop, the cost of managing the stat data could well end up artificially inflating the perceived cost of that code.

An alternative to VTune for out of engine profiling is Visual Studio’s profiler (Beginners Guide to Performance Profiling - Visual Studio 2015 | Microsoft Learn). It is really quite good even if the UI has some quirks I’m not a huge fan of. Keep in mind that since it is a sampling profiler, you generally need to run over a longer period of time to get a representative sample of your performance. The in-editor tools (and things like vtune) are really good for analyzing hitches while the sampling profiler is better at finding more systemic costs.

Thank you for sharing Marc!

That’s great to know :slight_smile:

I’ve been loving using UE4’s profiler but I will keep in mind that I should not keep counting cycles for blocks of code where performance issues have already been resolved, as it sounds like the stat tracking itself is a high enough cost to warrant careful maintainence and removal.

Thanks again Marc!

Rama