Skip to content

Commit

Permalink
perf: use timespan timer
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Aug 31, 2023
1 parent 9ab8271 commit 0766420
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions MHFZ_Overlay/Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public static string TestTimerMethods(decimal timeDefInt)
string timer2Result = string.Empty;
string timer3Result = string.Empty;

for (decimal i = timeInt; i >= 0M; i--)
for (decimal i = timeInt; i >= -timeDefInt; i--)
{
timer1Result = StringBuilderTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, true, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer2Result = TimeSpanTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer3Result = SimpleTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer2Result = TimeSpanTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, true, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);
timer3Result = SimpleTimer(timeInt, TimerFormat.MinutesSecondsMilliseconds, true, timeDefInt, true, GetTimeLeftPercent(timeDefInt, timeInt, true), TimerMode.Elapsed);

if (timer1Result != timer2Result || timer3Result != timer1Result || timer3Result != timer2Result)
{
Expand All @@ -100,11 +100,12 @@ public static string TestTimerMethods(decimal timeDefInt)
Simple: {timer3Result}";
}

private static string SimpleTimer(decimal timeInt, TimerFormat timerFormat, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string SimpleTimer(decimal timeInt, TimerFormat timerFormat, bool isFrames = true, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
// TODO wrong conditionals for timeint >= timedefint?
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal milliseconds = time / 30 * 1000;
decimal framesPerSecond = isFrames ? Numbers.FramesPerSecond : 1;
decimal milliseconds = time / framesPerSecond * 1000;
decimal totalMinutes = Math.Floor(milliseconds / 60000);
decimal minutes = totalMinutes >= 60 ? totalMinutes : Math.Floor(milliseconds / 60000);
decimal seconds = Math.Floor((milliseconds - (minutes * 60000)) / 1000);
Expand Down Expand Up @@ -148,10 +149,11 @@ private static string StringBuilderTimer(decimal timeInt, TimerFormat timerForma
return sb.ToString();
}

private static string TimeSpanTimer(decimal timeInt, TimerFormat timerFormat, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
private static string TimeSpanTimer(decimal timeInt, TimerFormat timerFormat, bool isFrames = true, decimal timeDefInt = 0, bool timeLeftPercentShown = false, string timeLeftPercentNumber = "", TimerMode timerMode = TimerMode.Elapsed)
{
decimal time = timerMode == TimerMode.Elapsed && timeInt <= timeDefInt ? time = timeDefInt - timeInt : time = timeInt;
decimal timeInSeconds = time / Numbers.FramesPerSecond;
decimal framesPerSecond = isFrames ? Numbers.FramesPerSecond : 1;
decimal timeInSeconds = time / framesPerSecond;
TimeSpan timeInSecondsSpan = TimeSpan.FromSeconds((double)timeInSeconds);
int roundedMilliseconds = (int)(Math.Round(timeInSecondsSpan.TotalMilliseconds) % 1000);
var totalMinutes = Math.Floor(timeInSecondsSpan.TotalSeconds / 60);
Expand All @@ -172,26 +174,26 @@ private static string TimeSpanTimer(decimal timeInt, TimerFormat timerFormat, de
/// </summary>
/// <param name="frames"></param>
/// <returns></returns>
public static string GetMinutesSecondsFromSeconds(double seconds) => StringBuilderTimer((long)seconds, TimerFormat.MinutesSeconds, false);
public static string GetMinutesSecondsFromSeconds(double seconds) => TimeSpanTimer((long)seconds, TimerFormat.MinutesSeconds, false);

/// <summary>
/// Gets the elapsed time in the desired format.
/// </summary>
/// <param name="frames"></param>
/// <returns></returns>
public static string GetMinutesSecondsFromFrames(double frames) => StringBuilderTimer((long)frames, TimerFormat.MinutesSeconds);
public static string GetMinutesSecondsFromFrames(double frames) => TimeSpanTimer((long)frames, TimerFormat.MinutesSeconds);

/// <summary>
/// Gets the elapsed time in the desired format.
/// </summary>
/// <param name="frames"></param>
/// <returns></returns>
public static string GetMinutesSecondsMillisecondsFromFrames(double frames) => StringBuilderTimer((long)frames, TimerFormat.MinutesSecondsMilliseconds);
public static string GetMinutesSecondsMillisecondsFromFrames(double frames) => TimeSpanTimer((long)frames, TimerFormat.MinutesSecondsMilliseconds);

/// <summary>
/// Gets the elapsed time in the desired format.
/// </summary>
/// <param name="frames"></param>
/// <returns></returns>
public static string GetMinutesSecondsMillisecondsFromFrames(long frames) => StringBuilderTimer(frames, TimerFormat.MinutesSecondsMilliseconds);
public static string GetMinutesSecondsMillisecondsFromFrames(long frames) => TimeSpanTimer(frames, TimerFormat.MinutesSecondsMilliseconds);
}

0 comments on commit 0766420

Please sign in to comment.