Download MetaTrader 5

Timer function for debugging

To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
Metal10k
31
Metal10k 2010.08.16 18:17 

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
Rashid Umarov
Admin
11836
Rashid Umarov 2010.08.16 18:34  

May be function GetTickCount will help you.
Metal10k
31
Metal10k 2010.08.16 20:07  

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);
      }
};
To add comments, please log in or register