Download MetaTrader 5

Sleep(100) takes 94 ms or 110 ms

To add comments, please log in or register
Documentation is available in CHM and PDF. Download it in any convenient form!
Jan Tarnogrocki
6335
Jan Tarnogrocki 2016.06.10 12:56 

Hi everyone,

using Sleep(100) in a loop does not take 100 milliseconds.

  while(!IsStopped())
  {
  int GlobalTicker = GetTickCount();
  Sleep(100);
  Print("loop duration after sleep: ",GetTickCount()-GlobalTicker);
  

I get values above or below:

The problem gets even bigger using shorter sleep durations. Using Sleep(1) leeds to a sleep time of 0 ms or 16 ms.

Has someone an idea how to solve this?

Kind regards,

Jan.

Jan Tarnogrocki
6335
Jan Tarnogrocki 2016.06.10 13:16  
int OnInit()
{
  EventSetMillisecondTimer(25);
  return(INIT_SUCCEEDED);
}

int prevGTC= GetTickCount();
void OnTimer()
{
  Print("loop duration after sleep: ",GetTickCount()-prevGTC);
  prevGTC= GetTickCount();
} 

Using OnTimer() brings a better result (more stable) but still not very stable:


Ex Ovo Omnia
3145
Ex Ovo Omnia 2016.06.10 13:57  
Jan Tarnogrocki:

Using OnTimer() brings a better result (more stable) but still not very stable:


The Sleep works fine, but you are using GetTickCount for your metrics. It has resolution of 15-16 ms.
Jan Tarnogrocki
6335
Jan Tarnogrocki 2016.06.10 14:04  
Ovo Cz:
The Sleep works fine, but you are using GetTickCount for your metrics. It has resolution of 15-16 ms.
How to measure it another way than using GetTickCount()?
Jan Tarnogrocki
6335
Jan Tarnogrocki 2016.06.10 14:12  
int loopcnt;

int OnInit()
{
   uint prevGTC=0;
   while(!IsStopped())
   {
      loopcnt++;
      if(loopcnt>=100)
      {
       double var = (GetTickCount()-prevGTC)/loopcnt;
       Print("loop duration after sleep: ",var);
       prevGTC= GetTickCount();
       loopcnt=0;
      }
    Sleep(1);      
   }
   
 return(INIT_SUCCEEDED);
}
OK, this shows an average loop duration of 1 ms. But this is no evidence that every single loop takes 1 ms.
Jan Tarnogrocki
6335
Jan Tarnogrocki 2016.06.10 14:42  
#import "Winmm.dll"
 int timeGetTime();
#import

int OnInit()
{
   uint prevGTC=0;
   while(!IsStopped())
   {
      int a  = timeGetTime();
      Sleep(5);        
      Print(timeGetTime()-a);
   }
 return(INIT_SUCCEEDED);
}

At least accurate to 1ms with my PC.

Stanislav Korotky
17883
Stanislav Korotky 2016.06.10 15:18  
You may use microseconds.
Jan Tarnogrocki
6335
Jan Tarnogrocki 2016.06.10 18:50  
Stanislav Korotky:
You may use microseconds.

Thanks, that´s great!

Although it seems that it returns nanoseconds and not microseconds. Need to divide the result by 1000 to get microseconds.

int OnInit()
{
   while(!IsStopped())
   {
      int prevGMSC  = GetMicrosecondCount();
      Sleep(10);        
      Print(DoubleToStr((GetMicrosecondCount()-prevGMSC)/1000,0));
   }
 return(INIT_SUCCEEDED);
}
Alain Verleyen
Moderator
30721
Alain Verleyen 2016.06.10 22:07  
Jan Tarnogrocki:

Thanks, that´s great!

Although it seems that it returns nanoseconds and not microseconds. Need to divide the result by 1000 to get microseconds.

You divide by 1000 to get milliseconds. There is no problem.
To add comments, please log in or register