Re: [aus-dotnet] TotalMilliseconds


    [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
    • From: Alex Hoffman
    • Subject: Re: [aus-dotnet] TotalMilliseconds
    • Date: Mon, 06 Dec 2004 09:49:55 +1100

    David,
    
    
    I'm no hardware guy, but i'm sure the count per second is processor dependent, and much higher than the figure you mention.
    
    
    For example, on my current machine (P4 1.6Mhz), the resolution using a high-resolution performance counter is 3,579,545 counts per second.
    
    
    You can display your own by making the Frequency property public in the Counter class I included before, then display the result e.g. Console.WriteLine(Counter.Frequency).
    
    
    Alternatively save the following code as say "frequency.csx", install Alintex Script (http://alintex.com/download.aspx) and then double click on the "frequency.csx" file.
    
    ///////////////////////////////////////////////////////
    // frequency.csx
    
    using System;
    
    #region Script
    	Console.WriteLine ("Counts per second = " + Counter.Frequency);
    #endregion
    
    
    public class Counter
    {
        [System.Runtime.InteropServices.DllImport("KERNEL32")]
    
    private static extern bool QueryPerformanceCounter( ref long lpPerformanceCount);
    
        [System.Runtime.InteropServices.DllImport("KERNEL32")]
    
    private static extern bool QueryPerformanceFrequency( ref long lpFrequency);
    
        long elapsedCount = 0;
        long startCount = 0;
    
        public void Start()
        {
            startCount = 0;
            QueryPerformanceCounter(ref startCount);
        }
    
        public void Stop()
        {
            long stopCount = 0;
            QueryPerformanceCounter(ref stopCount);
    
            elapsedCount += (stopCount - startCount);
        }
    
        public void Clear()
        {
            elapsedCount = 0;
        }
    
        public float Seconds
        {
            get
            {
                long freq = 0;
                QueryPerformanceFrequency(ref freq);
                return((float) elapsedCount / (float) freq);
            }
        }
    
        public override string ToString()
        {
            return String.Format("{0} seconds", Seconds);
        }
    
        public static long Frequency
        {
            get
            {
                long freq = 0;
                QueryPerformanceFrequency(ref freq);
                return freq;
            }
        }
    
        static long Value
        {
            get
            {
                long count = 0;
                QueryPerformanceCounter(ref count);
                return count;
            }
        }
    }
    ///////////////////////////////////////////////////////
    
    Alex Hoffman
    http://weblogs.asp.net/ahoffman
    
    David Richards said the following:
    
    I vaguely recall reading that the clock is updated 18 times a second.  This would make the accuracy 1/18th of a second or about 56ms.  Is this correct?
    
    I'm assuming this is a hardware limitation.  I would be curious to know if this is/was the case.
    
    
    
    Regards
    David Richards 	
    
    Software Support Technician Product Development Australian Arrow P/L Phone: 03 8770 2255 Fax: 03 8770 2325 "If we can hit that bullseye, the rest of the dominoes
    will fall like a house of cards... checkmate!"
    	
    -Zapp Brannigan, Futurama 	
    
    _________________
    You are a part of the Australian "dotnet" mailing list.
    To unsubscribe send "unsubscribe dotnet" or to re-subscribe send "subscribe dotnet Your Name" in the body of the email to: imailsrv@xxxxxxxxxxx
    List managed by: http://www.stanski.com
    
    _________________
    You are a part of the Australian "dotnet" mailing list.
    To unsubscribe send "unsubscribe dotnet" or to re-subscribe send "subscribe dotnet Your Name" in the body of the email to: imailsrv@xxxxxxxxxxx
    List managed by: http://www.stanski.com
    
    



    (Click here for more information on the aus-dotnet mailling list)