added RcCylicBuffer<long> extensions

This commit is contained in:
ikpil 2024-02-19 00:36:44 +09:00
parent ad504fe217
commit 01b3bcf771
3 changed files with 62 additions and 22 deletions

View File

@ -0,0 +1,54 @@
namespace DotRecast.Core.Buffers
{
public static class RcCyclicBuffers
{
public static long Sum(this RcCyclicBuffer<long> source)
{
long sum = 0;
checked
{
source.ForEach(x => sum += x);
}
return sum;
}
public static double Average(this RcCyclicBuffer<long> source)
{
if (0 >= source.Size)
return 0;
return source.Sum() / (double)source.Size;
}
public static long Min(this RcCyclicBuffer<long> source)
{
if (0 >= source.Size)
return 0;
long minValue = long.MaxValue;
source.ForEach(x =>
{
if (x < minValue)
minValue = x;
});
return minValue;
}
public static long Max(this RcCyclicBuffer<long> source)
{
if (0 >= source.Size)
return 0;
long maxValue = long.MinValue;
source.ForEach(x =>
{
if (x > maxValue)
maxValue = x;
});
return maxValue;
}
}
}

View File

@ -90,18 +90,9 @@ namespace DotRecast.Detour.Crowd
} }
cb.PushBack(duration); cb.PushBack(duration);
_executionTimings[name] = CalculateAverage(cb); _executionTimings[name] = (long)cb.Average();
} }
private static long CalculateAverage(RcCyclicBuffer<long> buffer)
{
long sum = 0L;
buffer.ForEach(item =>
{
sum += item;
});
return sum / buffer.Size;
}
} }
} }

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Core.Buffers;
using DotRecast.Core.Collections; using DotRecast.Core.Collections;
using DotRecast.Core.Numerics; using DotRecast.Core.Numerics;
using DotRecast.Detour; using DotRecast.Detour;
@ -25,7 +27,7 @@ namespace DotRecast.Recast.Toolset.Tools
private const int SamplingCount = 500; private const int SamplingCount = 500;
private long _samplingUpdateTime; private long _samplingUpdateTime;
private readonly List<long> _updateTimes; private readonly RcCyclicBuffer<long> _updateTimes;
private long _curUpdateTime; private long _curUpdateTime;
private long _avgUpdateTime; private long _avgUpdateTime;
private long _minUpdateTime; private long _minUpdateTime;
@ -36,7 +38,7 @@ namespace DotRecast.Recast.Toolset.Tools
_cfg = new RcCrowdAgentProfilingToolConfig(); _cfg = new RcCrowdAgentProfilingToolConfig();
_agCfg = new DtCrowdAgentConfig(); _agCfg = new DtCrowdAgentConfig();
_polyPoints = new List<DtPolyPoint>(); _polyPoints = new List<DtPolyPoint>();
_updateTimes = new List<long>(); _updateTimes = new RcCyclicBuffer<long>(SamplingCount);
} }
public string GetName() public string GetName()
@ -264,20 +266,14 @@ namespace DotRecast.Recast.Toolset.Tools
} }
var currentTime = endTime - startTime; var currentTime = endTime - startTime;
_updateTimes.Add(currentTime); _updateTimes.PushBack(currentTime);
if ((int)(SamplingCount * 1.25f) < _updateTimes.Count)
{
_updateTimes.RemoveRange(0, _updateTimes.Count - SamplingCount);
}
// for benchmark // for benchmark
_samplingUpdateTime = _updateTimes.Sum() / TimeSpan.TicksPerMillisecond; _samplingUpdateTime = _updateTimes.Sum() / TimeSpan.TicksPerMillisecond;
_curUpdateTime = currentTime / TimeSpan.TicksPerMillisecond; _curUpdateTime = currentTime / TimeSpan.TicksPerMillisecond;
_avgUpdateTime = (long)(_updateTimes.Average() / TimeSpan.TicksPerMillisecond); _avgUpdateTime = (long)(_updateTimes.Average() / TimeSpan.TicksPerMillisecond);
_minUpdateTime = _updateTimes.Min() / TimeSpan.TicksPerMillisecond; _minUpdateTime = _updateTimes.Min() / TimeSpan.TicksPerMillisecond;
_maxUpdateTime = _updateTimes.Max() / TimeSpan.TicksPerMillisecond; _maxUpdateTime = _updateTimes.Max() / TimeSpan.TicksPerMillisecond;
} }
private void MoveMob(DtNavMeshQuery navquery, IDtQueryFilter filter, DtCrowdAgent ag, RcCrowdAgentData crowAgentData) private void MoveMob(DtNavMeshQuery navquery, IDtQueryFilter filter, DtCrowdAgent ag, RcCrowdAgentData crowAgentData)
@ -392,16 +388,15 @@ namespace DotRecast.Recast.Toolset.Tools
{ {
return _avgUpdateTime; return _avgUpdateTime;
} }
public long GetCrowdUpdateMinTime() public long GetCrowdUpdateMinTime()
{ {
return _minUpdateTime; return _minUpdateTime;
} }
public long GetCrowdUpdateMaxTime() public long GetCrowdUpdateMaxTime()
{ {
return _maxUpdateTime; return _maxUpdateTime;
} }
} }
} }