Задача: Счетчик времени с точностью до микросекунд
Исходник: Timer.cs (через QueryPerformance), язык: C# [code #63, hits: 10839]
автор: this [добавлен: 21.02.2006]
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4.  
  5.  
  6.  
  7. public class Timer
  8. {
  9. [DllImport("KERNEL32")]
  10. private static extern bool QueryPerformanceCounter(
  11. out long lpPerformanceCount);
  12.  
  13. [DllImport("Kernel32.dll")]
  14. private static extern bool QueryPerformanceFrequency(out long lpFrequency);
  15.  
  16. private long start;
  17. private long stop;
  18. private long frequency;
  19. private long[] counters;
  20. private int N;
  21. //Decimal multiplier = new Decimal(1.0e9);
  22.  
  23. public Timer(int N)
  24. {
  25. this.N = N;
  26. this.counters = new long[this.N];
  27. if (QueryPerformanceFrequency(out this.frequency) == false)
  28. {
  29. // Frequency not supported
  30. throw new Win32Exception();
  31. }
  32. }
  33.  
  34. public void Start(int num)
  35. {
  36. QueryPerformanceCounter(out this.start);
  37. this.counters[num] = this.start;
  38. }
  39.  
  40. public double Get(int num)
  41. {
  42. QueryPerformanceCounter(out this.stop);
  43. return ((((double)(this.stop - this.counters[num])) / (double)this.frequency));
  44. }
  45. }
Timer.cs :: Счетки времени выполнения

Использует механизм QueryPerformance.
Оценивает время с точностью до наносекунд.
Тестировалось на: MS Visual Studio 2005, .NET Framework 2.0

+добавить реализацию