Skip to content

Instantly share code, notes, and snippets.

@webcoyote
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save webcoyote/a0e11799a91a2d967083 to your computer and use it in GitHub Desktop.

Select an option

Save webcoyote/a0e11799a91a2d967083 to your computer and use it in GitHub Desktop.

Revisions

  1. webcoyote revised this gist Jun 11, 2015. 1 changed file with 22 additions and 19 deletions.
    41 changes: 22 additions & 19 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -29,27 +29,26 @@ public float Delta () {
    private float LastValue;
    };

    private readonly TimeFunc[] m_times = new TimeFunc[] {
    // Time functions
    new TimeFunc("tick", () => Environment.TickCount / 1000f),
    new TimeFunc("stop", () => Stopwatch.GetTimestamp() / 1000f / 10000f),
    new TimeFunc("real", () => Time.realtimeSinceStartup),
    new TimeFunc("time", () => Time.time),
    new TimeFunc("levl", () => Time.timeSinceLevelLoad),
    new TimeFunc("fixd", () => Time.fixedTime),
    new TimeFunc("unsc", () => Time.unscaledTime),
    private TimeFunc[] m_times;

    // Value functions
    new TimeFunc("*delta", () => Time.deltaTime, false),
    new TimeFunc("*smoot", () => Time.smoothDeltaTime, false),
    new TimeFunc("*scale", () => Time.timeScale, false),
    new TimeFunc("*unsca", () => Time.unscaledDeltaTime, false),
    };
    void Awake () {
    m_times = new TimeFunc[] {
    // Time functions
    new TimeFunc("tick", () => Environment.TickCount / 1000f),
    new TimeFunc("stop", () => Stopwatch.GetTimestamp() / 1000f / 10000f),
    new TimeFunc("real", () => Time.realtimeSinceStartup),
    new TimeFunc("time", () => Time.time),
    new TimeFunc("levl", () => Time.timeSinceLevelLoad),
    new TimeFunc("fixd", () => Time.fixedTime),
    new TimeFunc("unsc", () => Time.unscaledTime),

    const int TargetFrameRate = 10;
    int m_frameCount;
    // Value functions
    new TimeFunc("*scale", () => Time.timeScale, false),
    new TimeFunc("*delta", () => Time.deltaTime * 1000f, false),
    new TimeFunc("*smoot", () => Time.smoothDeltaTime * 1000f, false),
    new TimeFunc("*unsca", () => Time.unscaledDeltaTime * 1000f, false),
    };

    void Start () {
    Application.targetFrameRate = TargetFrameRate;
    m_frameCount = 0;
    }
    @@ -60,9 +59,13 @@ void Update () {
    m_frameCount = 0;

    var sb = new StringBuilder(256);
    sb.Append(DateTime.UtcNow.ToShortTimeString());
    sb.Append(DateTime.UtcNow.ToLongTimeString());
    foreach (var time in m_times)
    sb.AppendFormat(" {0}:{1:0.0}", time.Name, time.Delta());
    UnityEngine.Debug.Log(sb.ToString());
    }


    const int TargetFrameRate = 60;
    int m_frameCount;
    }
  2. webcoyote revised this gist Jun 11, 2015. 1 changed file with 53 additions and 25 deletions.
    78 changes: 53 additions & 25 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -3,38 +3,66 @@
    using System.Threading;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Main : MonoBehaviour {
    int lastTicks;
    long lastStop;
    float lastRun;
    float lastReal;
    private class TimeFunc {
    public TimeFunc (string name, Func<float> time, bool applyDelta = true) {
    Name = name;
    Time = time;
    ApplyDelta = applyDelta;
    LastValue = time();
    }

    public float Delta () {
    var curr = Time();
    if (!ApplyDelta)
    return curr;
    var delta = curr - LastValue;
    LastValue = curr;
    return delta;
    }

    public readonly string Name;
    private readonly Func<float> Time;
    private readonly bool ApplyDelta;
    private float LastValue;
    };

    private readonly TimeFunc[] m_times = new TimeFunc[] {
    // Time functions
    new TimeFunc("tick", () => Environment.TickCount / 1000f),
    new TimeFunc("stop", () => Stopwatch.GetTimestamp() / 1000f / 10000f),
    new TimeFunc("real", () => Time.realtimeSinceStartup),
    new TimeFunc("time", () => Time.time),
    new TimeFunc("levl", () => Time.timeSinceLevelLoad),
    new TimeFunc("fixd", () => Time.fixedTime),
    new TimeFunc("unsc", () => Time.unscaledTime),

    // Value functions
    new TimeFunc("*delta", () => Time.deltaTime, false),
    new TimeFunc("*smoot", () => Time.smoothDeltaTime, false),
    new TimeFunc("*scale", () => Time.timeScale, false),
    new TimeFunc("*unsca", () => Time.unscaledDeltaTime, false),
    };

    const int TargetFrameRate = 10;
    int m_frameCount;

    void Start () {
    lastTicks = Environment.TickCount;
    lastStop = Stopwatch.GetTimestamp();
    lastRun = Time.realtimeSinceStartup;
    lastReal = Time.realtimeSinceStartup;
    Application.targetFrameRate = TargetFrameRate;
    m_frameCount = 0;
    }

    void Update () {
    var time = Time.realtimeSinceStartup;
    if (time - lastRun < 1)
    if (++m_frameCount < TargetFrameRate)
    return;
    lastRun = time;

    var currTicks = Environment.TickCount;
    var currStop = Stopwatch.GetTimestamp();
    var currReal = Time.realtimeSinceStartup;

    UnityEngine.Debug.Log(string.Format("{0} TICKS={1:0.0} WATCH={2:0.0} REAL={3:0.0}",
    DateTime.UtcNow,
    (currTicks - lastTicks) / 1000f,
    (currStop - lastStop) / 1000f / 10000f,
    (currReal - lastReal)
    ));
    lastTicks = currTicks;
    lastStop = currStop;
    lastReal = currReal;
    m_frameCount = 0;

    var sb = new StringBuilder(256);
    sb.Append(DateTime.UtcNow.ToShortTimeString());
    foreach (var time in m_times)
    sb.AppendFormat(" {0}:{1:0.0}", time.Name, time.Delta());
    UnityEngine.Debug.Log(sb.ToString());
    }
    }
  3. webcoyote revised this gist Jun 10, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -7,27 +7,27 @@
    public class Main : MonoBehaviour {
    int lastTicks;
    long lastStop;
    long lastRun;
    float lastRun;
    float lastReal;

    void Start () {
    lastTicks = Environment.TickCount;
    lastStop = Stopwatch.GetTimestamp();
    lastRun = Stopwatch.GetTimestamp();
    lastRun = Time.realtimeSinceStartup;
    lastReal = Time.realtimeSinceStartup;
    }

    void Update () {
    var time = Stopwatch.GetTimestamp();
    if (time - lastRun < 1 * 1000 * 10000)
    var time = Time.realtimeSinceStartup;
    if (time - lastRun < 1)
    return;
    lastRun = time;

    var currTicks = Environment.TickCount;
    var currStop = Stopwatch.GetTimestamp();
    var currReal = Time.realtimeSinceStartup;

    UnityEngine.Debug.Log(string.Format("{0} {1:0.0} {2:0.0} {3:0.0}",
    UnityEngine.Debug.Log(string.Format("{0} TICKS={1:0.0} WATCH={2:0.0} REAL={3:0.0}",
    DateTime.UtcNow,
    (currTicks - lastTicks) / 1000f,
    (currStop - lastStop) / 1000f / 10000f,
  4. webcoyote revised this gist Jun 10, 2015. 1 changed file with 6 additions and 13 deletions.
    19 changes: 6 additions & 13 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -8,19 +8,13 @@ public class Main : MonoBehaviour {
    int lastTicks;
    long lastStop;
    long lastRun;
    long lastQpc;

    [DllImport("KERNEL32")]
    private static extern bool QueryPerformanceCounter (out long lpPerformanceCount);

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency (out long lpFrequency);
    float lastReal;

    void Start () {
    lastTicks = Environment.TickCount;
    lastStop = Stopwatch.GetTimestamp();
    lastRun = Stopwatch.GetTimestamp();
    QueryPerformanceCounter(out lastQpc);
    lastReal = Time.realtimeSinceStartup;
    }

    void Update () {
    @@ -31,17 +25,16 @@ void Update () {

    var currTicks = Environment.TickCount;
    var currStop = Stopwatch.GetTimestamp();
    long currQpc; QueryPerformanceCounter(out currQpc);
    var currReal = Time.realtimeSinceStartup;

    UnityEngine.Debug.Log(string.Format("{0} {1:0.0} {2:0.0} {3:0.0} {4}",
    UnityEngine.Debug.Log(string.Format("{0} {1:0.0} {2:0.0} {3:0.0}",
    DateTime.UtcNow,
    (currTicks - lastTicks) / 1000f,
    (currStop - lastStop) / 1000f / 10000f,
    (currQpc - lastQpc) / 1000f / 10000f,
    Stopwatch.IsHighResolution
    (currReal - lastReal)
    ));
    lastTicks = currTicks;
    lastStop = currStop;
    lastQpc = currQpc;
    lastReal = currReal;
    }
    }
  5. webcoyote revised this gist Jun 10, 2015. 1 changed file with 40 additions and 18 deletions.
    58 changes: 40 additions & 18 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,47 @@
    using UnityEngine;
    using System;
    using System.Net;
    using System.Threading;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    public static class Program {
    public static void Main (string[] args) {
    var lastTicks = Environment.TickCount;
    var lastStop = Stopwatch.GetTimestamp();
    while (true) {
    System.Threading.Thread.Sleep(5000);
    var currTicks = Environment.TickCount;
    var currStop = Stopwatch.GetTimestamp();
    public class Main : MonoBehaviour {
    int lastTicks;
    long lastStop;
    long lastRun;
    long lastQpc;

    System.Console.WriteLine(
    "{0} {1:0.0} {2:0.0}",
    DateTime.UtcNow,
    (currTicks - lastTicks) / 1000f,
    (currStop - lastStop) / 1000f / 10000f
    );
    lastTicks = currTicks;
    lastStop = currStop;
    }
    [DllImport("KERNEL32")]
    private static extern bool QueryPerformanceCounter (out long lpPerformanceCount);

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency (out long lpFrequency);

    void Start () {
    lastTicks = Environment.TickCount;
    lastStop = Stopwatch.GetTimestamp();
    lastRun = Stopwatch.GetTimestamp();
    QueryPerformanceCounter(out lastQpc);
    }

    void Update () {
    var time = Stopwatch.GetTimestamp();
    if (time - lastRun < 1 * 1000 * 10000)
    return;
    lastRun = time;

    var currTicks = Environment.TickCount;
    var currStop = Stopwatch.GetTimestamp();
    long currQpc; QueryPerformanceCounter(out currQpc);

    UnityEngine.Debug.Log(string.Format("{0} {1:0.0} {2:0.0} {3:0.0} {4}",
    DateTime.UtcNow,
    (currTicks - lastTicks) / 1000f,
    (currStop - lastStop) / 1000f / 10000f,
    (currQpc - lastQpc) / 1000f / 10000f,
    Stopwatch.IsHighResolution
    ));
    lastTicks = currTicks;
    lastStop = currStop;
    lastQpc = currQpc;
    }
    }
  6. webcoyote created this gist Jun 10, 2015.
    25 changes: 25 additions & 0 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    using System;
    using System.Net;
    using System.Threading;
    using System.Diagnostics;

    public static class Program {
    public static void Main (string[] args) {
    var lastTicks = Environment.TickCount;
    var lastStop = Stopwatch.GetTimestamp();
    while (true) {
    System.Threading.Thread.Sleep(5000);
    var currTicks = Environment.TickCount;
    var currStop = Stopwatch.GetTimestamp();

    System.Console.WriteLine(
    "{0} {1:0.0} {2:0.0}",
    DateTime.UtcNow,
    (currTicks - lastTicks) / 1000f,
    (currStop - lastStop) / 1000f / 10000f
    );
    lastTicks = currTicks;
    lastStop = currStop;
    }
    }
    }
    7 changes: 7 additions & 0 deletions build
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/bin/bash
    set -e # crash on errors
    set -u # crash on undefined variables
    set -o pipefail # crash when intermediate program in pipe fails
    SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

    mcs "$SCRIPT_DIR/timer.cs" "$@"