title: QueryPerformanceCounter and weird results
body: <p>I want to use PerformanceCounter to measure how much time I need for some operation.</p>
  
  <p>I don't know much about PerformanceCounter and C++ in general. I found some code here: </p>
  
  <p><a href="https://stackoverflow.com/a/1739265/1215291">How to use QueryPerformanceCounter?</a></p>
  
  <p>I'm getting weird results with this. Here is my try:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  
  // ...
  
  double PCFreq = 0.0;
  __int64 CounterStart = 0;
  
  void StartCounter()
  {
      LARGE_INTEGER li;
      if (!QueryPerformanceFrequency(&amp;li))
          printf("QueryPerformanceFrequency failed!\n");
  
      PCFreq = double(li.QuadPart) / 1000.0;
  
      //printf("Performance counter resolution: %f", PCFreq);
  
      QueryPerformanceCounter(&amp;li);
      CounterStart = li.QuadPart;
  }
  
  double GetCounter()
  {
      LARGE_INTEGER li;
      QueryPerformanceCounter(&amp;li);
      return double(li.QuadPart - CounterStart) / PCFreq;
  }
  
  int main(int argc, const char** argv) {
      while (true) {
          StartCounter();
          Sleep(1000); // just a test
          printf("Query frame: %d\n", GetCounter());
  
          // ...
      }
  }
  </code></pre>
  
  <p>And here is my weird result with negative numbers:</p>
  
  <p><img src="https://i.stack.imgur.com/HwSE8.png" alt="enter image description here"></p>
  
  <p>What is wrong with my code?</p>