Timer function for debugging

 

I am trying to create a quick class for timing particular sequences of code (loops and nested functions) to determine where all my cpu cycles are going. So far I have come up with this:

 

class DebugTimer
{
   datetime StartTime;
   datetime EndTime;
   
   bool running;
   
   public:
      bool Start()
      {
      if(!running)
         {
         StartTime = TimeLocal();
         running = true;
         return(true);
         }
      return(false);
      }
      datetime Stop()
      {
      if(running)
         {
         EndTime = TimeLocal();
         running = false;
         return(EndTime-StartTime);
         }
      return(0);
      }
};

 The problem however is that the datetime variable along with any Timexxx() functions, their maximum resolution returned is seconds. For checking code timing you really need to be working in milliseconds. Is there any way anyone can think of doing this? Using the sleep function is of little use as it is running in the same thread (will pause everything and thus not achieve the desired results), and as far as I am aware there is no way to program multithreaded applications directly in mql5.

Documentation on MQL5: Common Functions / Sleep
  • www.mql5.com
Common Functions / Sleep - Documentation on MQL5
 

May be function GetTickCount will help you.
 

Perfect, thats exactly what I am after, thanks.

 

For anyone who can make use of it, here is the ammended code: 

 

class DebugTimer
{
   uint StartTime;
   uint EndTime;
   
   bool running;
   
   public:
      bool Start()
      {
      if(!running)
         {
         StartTime = GetTickCount();
         running = true;
         return(true);
         }
      return(false);
      }
      uint Stop()
      {
      if(running)
         {
         EndTime = GetTickCount();
         running = false;
         return(EndTime-StartTime);
         }
      return(0);
      }
};