[Unreal Engine] UE monitors the performance of C++ functions in each frame cycle, function time consumption, function efficiency, and function execution time

When using C++ to develop the UE engine, sometimes it is necessary to monitor the execution efficiency of the function execution. At this time, there are two ways to use it.

1. It takes time to execute the code

        double ThisTime = 0;
        {
           SCOPE_SECONDS_COUNTER(ThisTime);
            // ...
            // 一串代码
            // ...
        }
        UE_LOG(LogTemp, Log, TEXT("Stats::Broadcast %.2f"), ThisTime);

This will output the elapsed time of this piece of code, this method captures the time (passed in seconds) and adds the delta time to the variable passed in.

2. Function monitoring per frame

When some functions are executed every frame, it can be displayed by UE's statistical system, which is more convenient.

First define a custom listening group in CPP

DECLARE_STATS_GROUP(TEXT("DTActor"), STATGROUP_DTActor, STATCAT_Test);

In this way, when you start the program, you can see the group you defined

Then you need to define the function monitoring module

DECLARE_CYCLE_STAT(TEXT("DTActor Tick"), STAT_Tick, STATGROUP_DTActor);
DECLARE_CYCLE_STAT(TEXT("DTActor Call"), STAT_Call, STATGROUP_DTActor);

And start the corresponding module in the corresponding function

void ADTActor::Tick(float DeltaTime)
{
	SCOPE_CYCLE_COUNTER(STAT_Tick);
	Super::Tick(DeltaTime);
}
void ADTActor::Call()
{
	SCOPE_CYCLE_COUNTER(STAT_Call);
}

In this way, the specific execution time of the function will be displayed when the level is run

The following is the complete code of this Actor, you can test it yourself.

DTActor.h

// Copyright 2023 Dexter.Wan. All Rights Reserved. 
// EMail: [email protected]
// Website: https://dt.cq.cn/

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "DTActor.generated.h"

UCLASS()
class DTMOVIE_API ADTActor : public AActor
{
	GENERATED_BODY()

public:
	ADTActor();

protected:
	virtual void BeginPlay() override;

public:
	virtual void Tick(float DeltaTime) override;

	void Call();
};

DTActor.cpp8

// Copyright 2023 Dexter.Wan. All Rights Reserved. 
// EMail: [email protected]
// Website: https://dt.cq.cn/

#include "DTActor.h"

DECLARE_STATS_GROUP(TEXT("DTActor"), STATGROUP_DTActor, STATCAT_Test);
DECLARE_CYCLE_STAT(TEXT("DTActor Tick"), STAT_Tick, STATGROUP_DTActor); 
DECLARE_CYCLE_STAT(TEXT("DTActor Call"), STAT_Call, STATGROUP_DTActor);

ADTActor::ADTActor()
{
	PrimaryActorTick.bCanEverTick = true;
}

void ADTActor::BeginPlay()
{
	Super::BeginPlay();
}

void ADTActor::Tick(float DeltaTime)
{
	SCOPE_CYCLE_COUNTER(STAT_Tick);
	Super::Tick(DeltaTime);

	FString S;
	for ( int n = 0; n < 50000; ++n )
	{
		S += TEXT("1");
	}

	Call();
	Call();
}

void ADTActor::Call()
{
	SCOPE_CYCLE_COUNTER(STAT_Call);

	FString S;
	for ( int n = 0; n < 50000; ++n )
	{
		S += TEXT("1");
	}
}

Post-80s programmer icon-default.png?t=N6B9https://dt.cq.cn/archives/357

Guess you like

Origin blog.csdn.net/wmy19890322/article/details/131944569