Skip to content

Commit

Permalink
Started using more efficient methods of calculating time in performan…
Browse files Browse the repository at this point in the history
…ce oriented fields (https://www.youtube.com/watch?v=Lvdyi5DWNm4)

Signed-off-by: Kristian Gergov <[email protected]>
  • Loading branch information
t1stm committed Oct 29, 2024
1 parent 47a4070 commit b9ee7be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
7 changes: 4 additions & 3 deletions ThirtyDollarConverter/PCMEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
Expand Down Expand Up @@ -261,11 +262,11 @@ await Parallel.ForAsync(1, working_threads + 1, (i, _) =>

if (start > length) return ValueTask.CompletedTask;

var start_time = DateTime.Now;
var start_time = Stopwatch.GetTimestamp();
ProcessChunk(start, end, mixer, channel, events, processed_events, biggest_event_length);
var end_time = DateTime.Now;
var delta = Stopwatch.GetElapsedTime(start_time);
Log(
$@"Processed chunk i: {i} in {end_time - start_time:ss\.ffff} s. Start: {start}, End: {end}, ChunkSize: {chunk_size}, Length: {length}");
$@"Processed chunk i: {i} in {delta:ss\.ffff} s. Start: {start}, End: {end}, ChunkSize: {chunk_size}, Length: {length}");

return ValueTask.CompletedTask;
});
Expand Down
18 changes: 10 additions & 8 deletions ThirtyDollarVisualizer/Helpers/Timing/SeekableStopwatch.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics;

namespace ThirtyDollarVisualizer.Helpers.Timing;

public class SeekableStopwatch
Expand All @@ -6,8 +8,8 @@ public class SeekableStopwatch
protected TimeSpan LastValue = TimeSpan.Zero;

protected bool Running;
protected DateTime StartTime = DateTime.MinValue;
protected DateTime? StopTime;
protected long StartTime = long.MinValue;
protected long? StopTime;

public TimeSpan Elapsed
{
Expand All @@ -28,7 +30,7 @@ public TimeSpan Elapsed
StopTime = null;
}

var val = LastValue = current_time - StartTime;
var val = LastValue = Stopwatch.GetElapsedTime(StartTime, current_time);

Lock.Release();
return val;
Expand All @@ -38,15 +40,15 @@ public TimeSpan Elapsed
public long ElapsedMilliseconds => (long)Elapsed.TotalMilliseconds;
public bool IsRunning => Running;

protected static DateTime GetCurrentTime()
protected static long GetCurrentTime()
{
return DateTime.Now;
return Stopwatch.GetTimestamp();
}

public void Start()
{
if (Running) return;
if (StartTime == DateTime.MinValue)
if (StartTime == long.MinValue)
Restart();
Running = true;
}
Expand Down Expand Up @@ -80,7 +82,7 @@ public void Stop()
Lock.Wait();

Running = false;
StopTime = DateTime.Now;
StopTime = Stopwatch.GetTimestamp();

Lock.Release();
}
Expand All @@ -93,7 +95,7 @@ public void Seek(long delta)
LastValue = wanted_time;

var current = GetCurrentTime();
var delta_time = current - wanted_time;
var delta_time = current - wanted_time.Ticks * Stopwatch.Frequency / 10000000;
StartTime = delta_time;

if (StopTime != null) StopTime = current;
Expand Down
4 changes: 2 additions & 2 deletions ThirtyDollarVisualizer/Objects/DollarStoreCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public sealed class DollarStoreCamera : Camera
private readonly float _scrollSpeed;
private Vector3 _offset = (0, 0, 0);
private Vector3 _virtualPosition;
private DateTime LastScaleUpdate = DateTime.Now;
private long LastScaleUpdate = Stopwatch.GetTimestamp();

public DollarStoreCamera(Vector3 VirtualPosition, Vector2i viewport, float scroll_speed = 7.5f) : base(
VirtualPosition, viewport)
Expand Down Expand Up @@ -52,7 +52,7 @@ private void BlockingPulse(int times, float delay_ms)
stopwatch.Start();

const float max_add_scale = .05f;
var now = LastScaleUpdate = DateTime.Now;
var now = LastScaleUpdate = Stopwatch.GetTimestamp();
do
{
if (Disposing) return;
Expand Down

0 comments on commit b9ee7be

Please sign in to comment.