HighPerformanceTimer.cs 940 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OptiTrack
  8. {
  9. public class HighPerformanceTimer
  10. {
  11. [DllImport("Kernel32.dll")]
  12. private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
  13. [DllImport("Kernel32.dll")]
  14. private static extern bool QueryPerformanceFrequency(out long lpFrequency);
  15. private long start;
  16. private long freq;
  17. public HighPerformanceTimer()
  18. {
  19. start = 0;
  20. QueryPerformanceFrequency(out freq);
  21. }
  22. public void Start()
  23. {
  24. QueryPerformanceCounter(out start);
  25. }
  26. public double Stop()
  27. {
  28. long stop;
  29. QueryPerformanceCounter(out stop);
  30. return (double)(stop - start) / (double)freq;
  31. }
  32. }
  33. }