But I wanted to know whether Unreal has any nice convenience functions or macros. PLEASE, nothing heavy-handed that involves toggling things in the console, writing, reading, and locating trace files, or complicated GUIs that load said files and display 10,000 things I’m not interested in.
I just want to measure execution time for a block of C++ code. Thank you.
You can create stats groups in your C++ files, and create add scoped blocks in your code to measure time just like you would see if you typed for example “stat gpu” in the console. In your .h or .cpp file first create a stats group using DECLARE_STATS_GROUP(TEXT("MyStatsGroup"), STATGROUP_MyStatsGroup, STATCAT_Advanced);
Then also create a stat using DECLARE_CYCLE_STAT(TEXT("MyExpensiveFunction"), STAT_MyExpensiveFunction, STATGROUP_MyStatsGroup);
Finally, surround the code that you want to measure in a scope, like so: { SCOPE_CYCLE_COUNTER(STAT_MyExpensiveFunction); ExpensiveFunction(); }
Then in the editor, you can press the “`” key to open the console and if you type stat mystatsgroup, you should see the familiar stats overlay
Thank you for the clear explanation, and for confirming this is the way to go. However, I had tried this approach before, and couldn’t get it to work. And still can’t. I followed your instructions exactly. I get a table in the viewport showing the names of my stats. When I run the game, I get some numbers in the table for less than a second, then they vanish. The ‘call count’ never goes above zero.
So my next question is, does this method only work on functions that get called repeatedly in the game loop, in the same session? Because the functions I’m interested in only occur once, at initialization time. I was hoping this would keep an average across runs.
I should just write my own profiling functions and be done with it. I’ve been on this for days.
Yeah, I’ll make a lot more progress if I go the low-tech route. But it’s good to know how to use those macros - I didn’t really know how to view them with stat. When I get to it, I’ll try putting one in a function that gets called a lot, and see if it becomes useful. Thanks for your help.
I’m trying to measure the performance impact of a heavy, one-off function. I was able to create my stat group fine and see it in the editor, however like sequence_w I was unable to get the execution time to show persistently, it seems to disappear after less than a second after executing the function. I can only assume this is because the stat groups are intended to be used with code that will be executed continuously, IE many times during the game, not once.
I was able to get the info I needed by using the SCOPED_NAMED_EVENT_FSTRING macro. Ex:
In the Insights tool I can easily see how long the function took to execute by searching for the Scope name and selecting the portion of the timeline where the scope was initiated. This works fine, but I would prefer to use stat groups as it’s simpler than starting a trace. If anyone knows how stat group results can be shown persistently and not disappear after executing the function, I’d really appreciate an answer