Click here to Skip to main content
13,800,885 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


38 bookmarked
Posted 2 Apr 2013
Licenced CPOL

Obtaining Microsecond Precision in .NET

, 11 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Obtaining microsecond precision using .NET without Platform Invoke.


It is has been widely toted that Microsecond Precision (μs) scale time is not possible on .NET or Mono due to many issues which I will not endeavor into explaining.

Based on some of this I originally had setup a task for myself to write a good portable μs scale timer which performed the necessary platform invocation.

After I was done I realized that there is nothing "scientifically" stopping .NET from having this precision based on the fact that the caller executes the invocation and obtains the result and the GC cannot interrupt platform invocation calls so long as you do not pass a managed type.

E.g., if I pass a plain old pointer to a un-managed function there is nothing for the GC to interrupt or stop unless the Kernel itself interrupts the call for something.

I originally though about using unsafe code but then I realized that I was just going closer to using platform invocation.

I thought about trying to obtain precise clock cycles using a static constructor which forced a GC and then ran to determine things like call overhead and whatnot but I felt that there was more time being spent on trying to obtain information then actually sleeping for the user which was the goal.

I then realized something even more bold and interesting... Sockets have a microsecond precision due to signaling and they are usable from the .NET Framework and there is a Poll method which actually accepts the amount of time in Microseconds (μs).

After some quick tests I realized I had something which was a lightweight sealed class with all static members with no more resources than a single socket.

I tricked the socket into always being busy and then I used the Poll method to obtain the desired sleep time in Microsecond Precision (μs).

I want to know what everyone thinks about this and if anyone sees anything glaring out at me which I did not also take into account.

Here is the class code and testing code complete with platform invocation methods (found here on Stack Overflow @ usleep is obsolte...) for comparison and testing.

#region Cross Platform μTimer

    /// <summary>
    /// A Cross platform implementation which can delay time on the microsecond(μs) scale.
    /// It operates at a frequencies which are faster then most Platform Invoke results can provide due to the use of Kernel Calls under the hood.
    /// Requires on Mono and QueryPerformanceCounter on Windows for uSleep static
    /// </summary>
    /// <notes>A Tcp Socket will be created on port 7777 by default to help keep track of time. No connections will be recieved from this socket.</notes>
    public sealed class μTimer : IDisposable
        #region Not Applicable for the MicroFramework

        #region Uncesessary Interop (Left for Comparison)
#if MONO
        using System.Runtime.InteropServices;
        [System.Runtime.InteropServices.DllImport("")] //.a , Not Portable
        static extern int usleep (uint amount);

        ///<notes>The type useconds_t is an unsigned integer type capable of holding integers in the range [0,1000000]. Programs will be more portable if they never mention this type explicitly. </notes>
        void uSleep(int waitTime) { usleep(waitTime); }
        static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

        static extern bool QueryPerformanceFrequency(out long lpFrequency);

        /// <summary>
        /// Performs a sleep using a plaform dependent but proven method
        /// </summary>
        /// <param name="amount">The amount of time to sleep in microseconds(μs)</param>
        public static void uSleep(TimeSpan amount) { μTimer.uSleep(((int)(amount.TotalMilliseconds * 1000))); }

        /// <summary>
        /// Performs uSleep by convention of waiting on performance couters
        /// </summary>
        /// <param name="waitTime">The amount of time to wait</param>
        public static void uSleep(int waitTime)
            long time1 = 0, time2 = 0, freq = 0;

            QueryPerformanceCounter(out time1);
            QueryPerformanceFrequency(out freq);

                QueryPerformanceCounter(out time2);
            } while ((time2 - time1) < waitTime);

        #region Statics

        //Who but me
        const ushort Port = 7777;

        //Since System.Timespan.TickerPerMicrosecond is constantly 10,000
        public const long TicksPerMicrosecond = 10;

        /// <summary>
        /// A divider used to scale time for waiting
        /// </summary>
        public const long Divider = TimeSpan.TicksPerMillisecond / TicksPerMicrosecond;

        static bool m_Disposed;

        /// <summary>
        /// The socket we use to keep track of time
        /// </summary>
        static Socket m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        /// <summary>
        /// The memory we give to the socket for events which should not occur
        /// </summary>
        static SocketAsyncEventArgs m_SocketMemory = new SocketAsyncEventArgs();

        public static DateTime LocalTime { get { return new DateTime(Environment.TickCount * TimeSpan.TicksPerMillisecond); } }

        public static DateTime UniversalTime { get { return LocalTime.ToUniversalTime(); } }

        /// <summary>
        /// Handles the creation of resources used to provide the μSleep method.
        /// </summary>
        static μTimer()
                //Listen on the Loopback adapter on the specified port
                m_Socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, Port));

                //Only for 1 client

                //Assign an event now because in Begin process we will not call it if the even will not raise
                m_SocketMemory.Completed += BeginProcess;


                //If the SocketAsyncEventArgs will not raise it's own event we will call it now
                if (!m_Socket.AcceptAsync(m_SocketMemory))
                    BeginProcess(typeof(μTimer), m_SocketMemory);
                new Thread(()=> BeginProcess(this, null)).Start();

        /// <summary>
        /// Handles processing on the master time socket.
        /// This should never occcur.
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The SocketAsyncEventArgs from the event</param>
        /// <remarks>
        /// No one should connect... Ever.. (This is not a signaling implementation)
        /// </remarks>
        static void BeginProcess(object sender, SocketAsyncEventArgs e)
        static void BeginProcess(object sender, object args e)
                    Socket dontCare = m_Socket.Accept(); dontCare.Dispose(); 
                    throw new System.InvalidProgramException("A Connection to the system was made by a unauthorized means."); 
                catch { throw; } 
            if (!m_Disposed && e.LastOperation == SocketAsyncOperation.Connect)
                    throw new System.InvalidProgramException("A Connection to the system was made by a unauthorized means.");
                    if (e.AcceptSocket != null)e.AcceptSocket.Dispose();

        /// <summary>
        /// Performs a sleep using a method engineered by Julius Friedman (
        /// </summary>
        /// <param name="amount">The amount of time to Sleep</param>
        public static void μSleep(TimeSpan amount)
            //Sample the system clock            
            DateTime now = μTimer.UniversalTime, then = μTimer.UniversalTime;
            TimeSpan waited = now - then;
            //If cpu time is not fast enough to accomadate then you are in bigger trouble then you know
            if (waited > amount) return;
            else System.Threading.Thread.Sleep(amount - waited); //A normal sleep with an amount less that 1 but greater than 0 Millisecond will not switch
            waited = now - then;//Waste cycles and calculate time waited in ticks again
            if (waited > amount) return;
            else unchecked
                    //Scale time, basis of theory is we shouldn't be able to read from a socket in Accept mode 
                    //and it should take more time than a 1000th of the time we need
                    if (m_Socket.WaitRead(((int)((amount.Ticks - waited.Ticks / TicksPerMicrosecond) / Divider))))
                        //We didn't sleep
                        //Sample the system clock
                        then = μTimer.UniversalTime;
                        //Calculate waited
                        //Subtract time already waited from amount
                        amount -= waited;
                        //Waited set to now - then to determine wait
                        waited = now - then;
                        //return or utilize rest of slice sleeping
                        if (waited > amount) return;
                        else System.Threading.Thread.Sleep(amount - waited);

        /// <summary>
        /// Performs a sleep using a method engineered by Julius Friedman (
        /// </summary>
        /// <param name="amount">The amount of time to Sleep in microseconds(μs) </param>
        public static void μSleep(int amount) { μTimer.μSleep(TimeSpan.FromMilliseconds(amount * TimeSpan.TicksPerMillisecond)); }


        void IDisposable.Dispose()
            m_Disposed = true;
            if (m_Socket != null)
                m_Socket = null;


Here is the testing code:

I even updated it to show that the StopWatch verifies that my method sleeps for under 1 μs.

  • Test Failed! (A Result of the writenotice function, take only note of the color which will be "DarkGreen" 
  • Exception.Message: StopWatch Elapsed during µSleep = 00:00:00.0000043
  • µTimer Took: 00:00:00
  • PerformanceCounter Took: 00:00:00
  • StopWatch Took 00:00:00.0000006
  • Test Passed! (Because my method was faster) 
  • Test Passed! (Because my method was faster) 
  • Press (W) to run again, (D) to debug or any other key to continue. (Press D or Q) 
  • 0 Failures, 7778 Successes (Because the CPU Has to warm up the first executions I believe)  
  • ......  
  • Sleep slept 0 µs. Sleep slept 43 Ticks. 1 Ops (Proof) 
  • 46533553 00:00:00.0150008
  • Sleep slept 0 µs. Sleep slept 46 Ticks. 1 Ops  (Proof) 
  • 999 Environment.TickCount: 46533569, delta: 16, 181 * (average TimeSlice Information) 
  • Min Delta: 15, Max Delta: 32.    
  • Sum Delta: 15803 / Num Delta: 1000 = Average Delta = 15.803000000 (Average TimeSlice time)  
  • (This proves the Envrionment.TickCount is too SLOW!!!)    

I have also included test code to measure the tick count!

The explanation for these results in short is as follows:

Just as when you offload work to the GPU this method offloads work to your NIC processor.

The network stack in your OS sets up an event in to perform this action by setting up an interrupt.

Then when the interrupter make the interrupt and the OS Completes the operation the interrupter is resumed back at the position where he interrupted the code giving you the precision you desire!

The nest steps would be WaitHandle derived implementation which overloads the defaults and gives Microsecond Precision which also add the ability to notify other threads as well!

Until that time you can find the code below!

static void RunTest(Action test, int count = 1)
    Console.BackgroundColor = ConsoleColor.Blue;
    Console.WriteLine("About to run test: " + test.Method.Name);
    Console.WriteLine("Press Q to skip or any other key to continue.");
    Console.BackgroundColor = ConsoleColor.Black;
    if (Console.ReadKey().Key == ConsoleKey.Q) return;
        Dictionary<int, Exception> log = null;

        int run = count, failures = 0, successes = 0; bool multipleTests = count > 0;

        if (multipleTests) log = new Dictionary<int, Exception>();

            System.Threading.Interlocked.Decrement(ref run);
            System.Threading.Interlocked.Increment(ref successes);
        catch (Exception ex)
            System.Threading.Interlocked.Increment(ref failures);
            if (multipleTests)
                log.Add(run, ex);
        if (run >= 0) goto Test;
        else if (multipleTests)
            if (failures > successes) writeNotice(new Exception("More Failures then Successes"));
            else writeSuccess(false, failures + " Failures, " + successes + " Successes");

        ConsoleKey input = Console.ReadKey().Key;

        if (input == ConsoleKey.W) goto Test;
        else if (input == ConsoleKey.D) System.Diagnostics.Debugger.Break();

static void writeNotice(Exception ex, ConsoleColor color = ConsoleColor.Red, bool pressStatement = true)
    ConsoleColor swap = Console.BackgroundColor;
    Console.BackgroundColor = color;
    Console.WriteLine("Test Failed!");
    Console.WriteLine("Exception.Message: " + ex.Message);
    if(pressStatement) Console.WriteLine("Press (W) to try again or any other key to continue.");
    Console.BackgroundColor = swap;

static void writeSuccess(bool auto = true, string message = null, ConsoleColor? color = null)
    ConsoleColor swap = Console.BackgroundColor;
    if (color.HasValue) Console.BackgroundColor = color.Value;
    else Console.BackgroundColor = ConsoleColor.Green;
    Console.WriteLine("Test Passed!");
    if (!auto) Console.WriteLine("Press (W) to run again, (D) to debug or any other key to continue.");
    if (!string.IsNullOrWhiteSpace(message)) Console.WriteLine(message);
    Console.BackgroundColor = swap;

static void TestEnvironmentTickCount()
            int mindelta = int.MaxValue;
            int maxdelta = int.MinValue;
            long sumdelta = 0;
            long numdelta = 0;
            System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
            for (int i = 0; i < 1000; i++)
                int d1 = Environment.TickCount;
                int d2 = d1;
                int sameval = 0;
                DateTime now = DateTime.UtcNow;
                uint ops = 0;
                while ((d2 = Environment.TickCount) == d1)
                    ops = 0;
                    DateTime then = DateTime.UtcNow;
                    Media.Common.μTimer.μSleep(TimeSpan.FromTicks((TimeSpan.TicksPerMillisecond / Media.Common.μTimer.TicksPerMicrosecond / Media.Common.μTimer.Divider)));
                    Console.WriteLine(Environment.TickCount + " " + (then - now));
                    Console.WriteLine("Sleep slept {0} µs. Sleep slept {1} Ticks. {2} Ops", (w.Elapsed.Ticks / ( TimeSpan.TicksPerMillisecond / Media.Common.μTimer.TicksPerMicrosecond)), w.Elapsed.Ticks, ops);
                int delta = d2 - d1;
                mindelta = Math.Min(delta, mindelta);
                maxdelta = Math.Max(delta, maxdelta);
                sumdelta += delta;
                Console.WriteLine("{3:D3} Environment.TickCount: {0}, delta: {1}, {2} *", d2, delta, sameval, i);
            double avgdelta = ((double)sumdelta) / ((double)numdelta);
            Console.WriteLine("Min Delta: {0}, Max Delta: {1}.", mindelta, maxdelta);
            Console.WriteLine("Sum Delta: {0} / Num Delta: {1} = Average Delta = {2:F9}", sumdelta, numdelta, avgdelta);

Called like this:

public static void Main(string[] args)
    RunTest(TimerTest, 7777);//Address article post
    tickThread = null;

Even when using just the Performance counters there are some failures as you might come to expect, however in all cases I find my method is faster then the platform invocation and the counters.

Let me know if I am too high on my horse to see the big picture here or if I actually achieved something which others may find useful!

I updated the test code and it shows how to use a StopWatch to wait also.. it seems that is NOT faster because it uses the performance counters mine already beats...

The reason for this in short, is that my code uses IOCompletionPorts under the hood in Windows and on Unix it is using system calls.

See MSDN, tutorialspoint.

I am also confirming this working in the MicroFx as well as other places you may not expect e.g., Java et al and depends on how the underlying implementation provides the Poll method.

Here you will find the Guidelines for providing Multimedia Time Support, you can clearly see that it has a lengthy requirements list! 

On of the key points being: "Chipset vendors should implement an HPET to comply with Intels "IA-PC HPET (High Precision Event Timers) Specification" and this code is strengthened on the reliance that all NIC processors can handle this requirement quite easily!

Some will say this is a hack and a shortcut but in all honesty, is there anything really wrong with this? 

If you need the 'WaitRead' Method code you can check out Manged Media Aggregation which will be releasing this code very shortly along with a bunch of other goodies! 

Until then use Poll with the same value as I do in 'WaitRead' and SelectMode.Read  



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) ASTI Transportation Inc.
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...


You may also be interested in...

Comments and Discussions

GeneralRe: Tried it, but it doesn't seem accurate Pin
_groo_6-May-14 2:51
member_groo_6-May-14 2:51 
GeneralRe: Tried it, but it doesn't seem accurate Pin
jfriedman6-May-14 2:58
professionaljfriedman6-May-14 2:58 
GeneralRe: Tried it, but it doesn't seem accurate Pin
_groo_6-May-14 6:35
member_groo_6-May-14 6:35 
GeneralRe: Tried it, but it doesn't seem accurate Pin
jfriedman6-May-14 6:53
professionaljfriedman6-May-14 6:53 
QuestionHow to calculate the sleep time? Pin
BlackDogSpark12-Jun-13 14:41
memberBlackDogSpark12-Jun-13 14:41 
AnswerRe: How to calculate the sleep time? Pin
jfriedman5-Jul-13 7:43
professionaljfriedman5-Jul-13 7:43 
QuestionI'm confused. How to use this to replace System.Timers.Timer loop? Pin
BlackDogSpark10-Jun-13 9:13
memberBlackDogSpark10-Jun-13 9:13 
AnswerRe: I'm confused. How to use this to replace System.Timers.Timer loop? Pin
jfriedman10-Jun-13 10:33
professionaljfriedman10-Jun-13 10:33 
GeneralRe: I'm confused. How to use this to replace System.Timers.Timer loop? Pin
BlackDogSpark10-Jun-13 11:04
memberBlackDogSpark10-Jun-13 11:04 
GeneralRe: I'm confused. How to use this to replace System.Timers.Timer loop? Pin
jfriedman5-Jul-13 7:45
professionaljfriedman5-Jul-13 7:45 
QuestionWhere to find Socket.WaitRead()? Pin
Nils-Erik Thorén22-Apr-13 4:00
memberNils-Erik Thorén22-Apr-13 4:00 
AnswerRe: Where to find Socket.WaitRead()? Pin
jfriedman16-Jan-14 19:15
professionaljfriedman16-Jan-14 19:15 
GeneralMy vote of 5 Pin
dusty_dex11-Apr-13 11:32
memberdusty_dex11-Apr-13 11:32 
GeneralRe: My vote of 5 Pin
jfriedman11-Apr-13 11:39
professionaljfriedman11-Apr-13 11:39 
QuestionMaybe I read through this a bit fast, but Pin
Espen Harlinn11-Apr-13 2:45
mentorEspen Harlinn11-Apr-13 2:45 
AnswerRe: Maybe I read through this a bit fast, but Pin
jfriedman11-Apr-13 6:13
professionaljfriedman11-Apr-13 6:13 
GeneralRe: Maybe I read through this a bit fast, but Pin
millka11-Apr-13 8:12
membermillka11-Apr-13 8:12 
GeneralRe: Maybe I read through this a bit fast, but Pin
millka11-Apr-13 8:56
membermillka11-Apr-13 8:56 
GeneralRe: Maybe I read through this a bit fast, but Pin
jfriedman11-Apr-13 9:00
professionaljfriedman11-Apr-13 9:00 
GeneralRe: Maybe I read through this a bit fast, but Pin
Espen Harlinn11-Apr-13 11:16
mentorEspen Harlinn11-Apr-13 11:16 
GeneralRe: Maybe I read through this a bit fast, but Pin
jfriedman11-Apr-13 11:40
professionaljfriedman11-Apr-13 11:40 
QuestionThere is a problem.... Pin
Daniel Rozsar11-Apr-13 1:22
professionalDaniel Rozsar11-Apr-13 1:22 
AnswerRe: There is a problem.... Pin
jfriedman11-Apr-13 6:55
professionaljfriedman11-Apr-13 6:55 
QuestionNice idea, but it doesn't work Pin
millka5-Apr-13 14:22
membermillka5-Apr-13 14:22 
AnswerRe: Nice idea, but it doesn't work Pin
jfriedman6-Apr-13 9:46
professionaljfriedman6-Apr-13 9:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181215.1 | Last Updated 12 Apr 2013
Article Copyright 2013 by jfriedman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid