/* $Id$ * * Copyright 2007-2008 Cisco Systems Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ using System; using System.Runtime.InteropServices; namespace Etch.Util { public abstract class HPTimer { [DllImport("kernel32.dll")] private static extern bool QueryPerformanceCounter(out long x); [DllImport("kernel32.dll")] private static extern bool QueryPerformanceFrequency(out long x); static HPTimer() { long freq = 0; if (!QueryPerformanceFrequency(out freq)) throw new Exception( "QueryPerformanceFrequency not supported" ); // freq is ticks / second. // we wanna compute seconds from ticks. // the easy computation is seconds = ticks / freq // to Get ns, we compute ns = ticks * 1,000,000,000 / freq // the problem is ticks * 1,000,000,000 will likely overflow. // what we want to do is precompute nsPerTick = 1,000,000,000 / freq // and then compute ns = ticks * nsPerTick. //Console.WriteLine( "HPTimer: freq = "+freq ); nsPerTick = ((double) NS_PER_SECOND)/freq; //Console.WriteLine( "HPTimer: nsPerTick = "+nsPerTick ); } public const long NS_PER_MICROSECOND = 1000; public const long NS_PER_MILLISECOND = 1000 * NS_PER_MICROSECOND; public const double NS_PER_SECOND = 1000.0 * NS_PER_MILLISECOND; /// /// Returns the current high precision timer value in nanos. /// /// public static long Now() { long x = 0; if (!QueryPerformanceCounter(out x)) throw new Exception( "QueryPerformanceCounter not supported" ); return (long) (x * nsPerTick); } /// /// Returns the difference in nanos between the current timer value and /// a timer value previously returned by Now. /// /// /// public static long NsSince( long y ) { return Now() - y; } /// /// Returns the difference in millis between the current timer value and /// a timer value previously returned by Now. /// /// /// public static long MillisSince( long y ) { return NsSince(y) / NS_PER_MILLISECOND; } /// /// Returns the difference in seconds between the current timer value and /// a timer value previously returned by Now. /// /// /// public static double SecondsSince( long y ) { return NsSince(y) / NS_PER_SECOND; } /// /// Returns the number of high precision clock ticks per nano. /// /// public static double NsPerTick() { return nsPerTick; } private static double nsPerTick; } }