Skip to content

Instantly share code, notes, and snippets.

@ophura
Created September 5, 2025 03:14
Show Gist options
  • Select an option

  • Save ophura/1a22f4f1dbba142723ee914ac35f11c9 to your computer and use it in GitHub Desktop.

Select an option

Save ophura/1a22f4f1dbba142723ee914ac35f11c9 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using UdonSharp;
using UnityEngine;
namespace Ophura
{
using Debug = UnityEngine.Debug;
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
internal sealed class Statistics : UdonSharpBehaviour
{
private const string LoggerLabel = "[<color=#aa33f1>Logger Label</color>]";
/// <summary>
/// The number of <see cref="TimeSpan"/> ticks per <see cref="Stopwatch"/> tick.
/// </summary>
private readonly double _tickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency;
public string _pattern = "VRCWorld"; // change it through the inspector & don't leave it null nor empty.
private void Start()
{
Debug.Log($"{LoggerLabel} is high resolution counter: {Stopwatch.IsHighResolution}");
Debug.Log($"{LoggerLabel} tick frequency: {_tickFrequency}");
// warmup; important step for improved performance.
for (int _ = -1; _ < byte.MaxValue; ++_)
{
GameObject.Find(_pattern);
Stopwatch.GetTimestamp();
}
// calculate the time taken to execute GameObject.Find().
long commence = Stopwatch.GetTimestamp();
var target = GameObject.Find(_pattern);
long conclude = Stopwatch.GetTimestamp();
if (null == target)
{
Debug.Log($"{LoggerLabel} could not find a GameObject matching {_pattern} pattern");
return;
}
var latency = new TimeSpan((long)((conclude - commence) * _tickFrequency));
Debug.Log($"{LoggerLabel} found {target.name} with a latency of {latency}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment