|
1 | | -# UltraTimerWindows |
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
2 | 9 |
|
| 10 | + |
| 11 | +# 🔥 UltraTimerWindows |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 🚀 Overview |
| 16 | + |
| 17 | +**UltraTimerWindows** is a zero-allocation, native high-resolution timer for Unity, built on top of Windows ``QueryPerformanceCounter``. |
| 18 | +Suitable for micro-benchmarks and hot-path measurements. Provides sub-millisecond and microsecond-level timing precision. |
| 19 | + |
| 20 | +The overhead of the timer itself is negligible compared to typical Unity workloads. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## ✨ Features |
| 25 | + |
| 26 | +- ⚡ **High-resolution native timing in pure C** (WinAPI `QueryPerformanceCounter`) |
| 27 | +- 🧹 **Zero GC allocations during measurement** |
| 28 | +- 🔒 **Deterministic and low-overhead** |
| 29 | +- 🔥 **Suitable for micro-benchmarks and hot-path measurements** |
| 30 | +- ⚙️ **IL2CPP & Mono compatible** |
| 31 | +- 🧩 **Scoped measurement via `using`** |
| 32 | +- 🎞️ **Profile sampling for each scope** |
| 33 | +- 🔬 **Independent from Unity Profiler** |
| 34 | + |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## 📦 Installation |
| 39 | + |
| 40 | +Via UPM: `https://github.com/HardCodeDev777/UltraTimerWindows.git?path=Unity` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## 💻 Usage |
| 45 | + |
| 46 | +**UltraTimerWindows** provides very elegant and simple API: |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +### 💡 Basic example of usage |
| 51 | + |
| 52 | +```csharp |
| 53 | +using UnityEngine; |
| 54 | +// Namespace |
| 55 | +using HardCodeDev.UltraTimerWindows.Runtime; |
| 56 | + |
| 57 | +public class TestTimer : MonoBehaviour |
| 58 | +{ |
| 59 | + // Timer instance |
| 60 | + private UltraTimerWindows _timer; |
| 61 | + |
| 62 | + private void Start() => _timer = new(); |
| 63 | + |
| 64 | + private void Update() |
| 65 | + { |
| 66 | + using (_timer.Measure()) |
| 67 | + { |
| 68 | + // Here you can call any methods you want to measure |
| 69 | + HeavyMethod(); |
| 70 | + } |
| 71 | + |
| 72 | + // See results in the console |
| 73 | + Debug.Log($"Last elapsed: {_timer.State.lastElapsedMs} ms, " + |
| 74 | + $"total elapsed: {_timer.State.totalElapsedMs} ms, " + |
| 75 | + $"average: {_timer.State.averageMs} ms, " + |
| 76 | + $"called: {_timer.State.countOfCalling} times"); |
| 77 | + } |
| 78 | + |
| 79 | + private void HeavyMethod() |
| 80 | + { |
| 81 | + for (int i = 0; i < 100000; i++) |
| 82 | + { |
| 83 | + float x = Mathf.Sqrt(i); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Don't forget about disposing! |
| 88 | + private void OnDestroy() => _timer?.Dispose(); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +### 🎞️ Profile Sampling |
| 95 | + |
| 96 | +```csharp |
| 97 | +using (_timer.Measure("Name of Sample")) |
| 98 | +{ |
| 99 | + // Here you can call any methods you want to measure |
| 100 | + HeavyMethod(); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +And then after running with enabled Profile you'll see: |
| 105 | + |
| 106 | +<img src="media/profilesampling.png" height="400"> |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +### 🫡 Reset Timer |
| 111 | + |
| 112 | +```csharp |
| 113 | +using (_timer.Measure("Name of Sample")) HeavyMethod(); |
| 114 | + |
| 115 | +if (_timer.State.countOfCalling == 1000) _timer.Reset(); |
| 116 | +``` |
| 117 | + |
| 118 | +Result: |
| 119 | + |
| 120 | +<img src="media/resettimer.png" height="400"> |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +### That's it! Enjoy measuring your heavy stuff 😎 |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## 📄 License |
| 129 | + |
| 130 | +This project is licensed under the **MIT License**. |
| 131 | +See the [LICENSE.md](LICENSE.md) file for full terms. |
0 commit comments