Skip to content

Instantly share code, notes, and snippets.

@SoftwareGuy
Created December 28, 2020 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SoftwareGuy/8ad60089f507239fb2e256399999c8d8 to your computer and use it in GitHub Desktop.
Save SoftwareGuy/8ad60089f507239fb2e256399999c8d8 to your computer and use it in GitHub Desktop.
Really really really crude NetStack.Buffers vs System.Buffers Shootout for Unity
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
// Microsoft NET Buffers
using System.Buffers;
// NX's NetStack Buffers.
using NetStack.Buffers;
public class Benchmark : MonoBehaviour
{
// How many times to run the benchmark.
public int Runs = 10;
// How many arrays to allocate.
public int ArrayCount = 1000;
// How big arrays to allocate. Default is ENet MTU (1200).
public int ArraySize = 1200;
private Stopwatch overallStopwatch = new Stopwatch();
private Stopwatch stopwatch = new Stopwatch();
private RunResult[] runResults;
private bool busy;
private System.Buffers.ArrayPool<byte> systemBufferArrayPool;
private NetStack.Buffers.ArrayPool<byte> netStackArrayPool;
private void Awake()
{
// Set up the results array.
runResults = new RunResult[Runs];
systemBufferArrayPool = System.Buffers.ArrayPool<byte>.Shared;
netStackArrayPool = NetStack.Buffers.ArrayPool<byte>.Shared;
}
private void Update()
{
// 'B' for Benchmark
if (!busy)
{
if (Input.GetKeyUp(KeyCode.B))
RunBenchmark();
}
}
private void OnGUI()
{
}
private void RunBenchmark()
{
print("Starting benchmark.");
overallStopwatch.Reset();
stopwatch.Reset();
// Start the overall stopwatch.
overallStopwatch.Start();
// Loop.
for (int run = 0; run < Runs; run++)
{
List<byte[]> sysBufByteRentList = new List<byte[]>(ArrayCount);
List<byte[]> netstackBufByteRentList = new List<byte[]>(ArrayCount);
runResults[run] = default;
runResults[run].RunCount = run;
// print($"Begin run {run + 1}");
// print("- System.Buffers -");
// Rent using System.Buffers...
stopwatch.Start();
for (int i = 0; i < ArrayCount; i++)
sysBufByteRentList.Add(systemBufferArrayPool.Rent(ArraySize));
stopwatch.Stop();
runResults[run].sysBuffRentalTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Reset();
// Return using System.Buffers...
stopwatch.Start();
for (int i = 0; i < sysBufByteRentList.Count; i++)
systemBufferArrayPool.Return(sysBufByteRentList[i]);
stopwatch.Stop();
runResults[run].sysBuffReturnTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Reset();
print($"Run {run+1}, System.Buffers: Rent {runResults[run].sysBuffRentalTime} ms, Return {runResults[run].sysBuffReturnTime} ms");
// print("- NetStack.Buffers -");
// Rent using NetStacks.Buffers...
stopwatch.Start();
for (int i = 0; i < ArrayCount; i++)
netstackBufByteRentList.Add(netStackArrayPool.Rent(ArraySize));
stopwatch.Stop();
runResults[run].netStackRentalTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Reset();
// Return using NetStacks.Buffers...
stopwatch.Start();
for (int i = 0; i < netstackBufByteRentList.Count; i++)
netStackArrayPool.Return(netstackBufByteRentList[i]);
stopwatch.Stop();
runResults[run].netStackReturnTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Reset();
print($"Run {run + 1}, NetStack.Buffers: Rent {runResults[run].netStackRentalTime} ms, Return {runResults[run].netStackReturnTime} ms");
// print($"End run {run + 1}");
}
print("Ending benchmark.");
overallStopwatch.Stop();
print($"Total benchmark time: {overallStopwatch.Elapsed}");
}
private struct RunResult
{
public int RunCount;
public double sysBuffRentalTime;
public double sysBuffReturnTime;
public double netStackRentalTime;
public double netStackReturnTime;
// public bool didNotFinish;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment