Download

Measure time a function has taken


startTime = currentTime();
while (currentTime() - startTime < 16ms){
	doSomeWork();
}
return;

Currently I generate chunks in a fixed radius of the player.
Some of the chunks are empty (air), some require more or less procedural mesh calculations.

I am trying to keep each logic tic under X millisec regardless of how many chunks I have generated.
A simple time check would do the trick.

The problem is I am not yet comfortable with c++ and ue4 as implied by my post count :).

Basically I am asking how can I get system time with millisec precision with the built in api or if it is possible normal c++ libraries (<time>?).
The function use case and includes.

I tried “UGameplayStatics::GetRealTimeSeconds(GetWorld())”, but it seemed only be updated after every logic tic.

Maybe the time and date stuff will help you

this is a Formatted Time Stamp in an FString (also other examples for CurrentDateAndTime)

This seems to be it, but…
CurrentDateAndTime.GetMillisecond() seems to be from 0 to 999;
So when my start is at 990 and my end time at 6, it would report -884 instead of 16 ms.

I am used to have time in this format: 1438627036.337626.
EDIT: unix time + millisecs, that what this was…


FDateTime startT = FDateTime::UtcNow();
int32 startMs = startT.GetMillisecond();
int64 unixStart = startT.ToUnixTimestamp() * 1000 + startMs;

This seems to be it.
Hopefully “startT” won’t update during “GetMillisecond()” or “ToUnixTimestamp()” calls.

That was what I was looking for, made a simple utility to measure timings for certain inter-function periods:


class UtilityTimer
{
	int64 TickTime = 0;
	int64 TockTime = 0;
public:
	int64 unixTimeNow()
	{
		FDateTime timeUtc = FDateTime::UtcNow();
		return timeUtc.ToUnixTimestamp() * 1000 + timeUtc.GetMillisecond();
	}

	void tick()
	{
		TickTime = unixTimeNow();
	}

	int32 tock()
	{
		TockTime = unixTimeNow();
		return TockTime - TickTime;
	}
};

then you make a


UtilityTimer timer;

and call


timer.tick();

at the start and


int32 durationMs = timer.tock();

at the stop.

For measuring time for profiling I suggest using FPlatformTime::Seconds();
Internally it uses the highest resolution timer available to whatever platform you’re running on. (e.g. for Windows that’s QueryHighPerformanceCounter())

here’s an example of how i use it.




	double start = FPlatformTime::Seconds();

	// put code you want to time here.

	double end = FPlatformTime::Seconds();
	UE_LOG(LogTemp, Warning, TEXT("code executed in %f seconds."), end-start);


Excellent! Also: How did you manage to find this function? Did you just stumble upon it randomly while looking through the engine source?