Sleep() Function test

 

I found the Sleep() function does not work exactly as expected.

See the table for test results. The test is to Sleep() from 0 to 99 milliseconds and use GetTickCount() to see how long the sleep lasted.

Results might vary with different PC hardware.

Conclusions:
This is probably OS and/or hardware limitation.
Sleep() occurs on a hit/miss fashion on 16millisecond boundaries
Don't rely on Sleep() to be exact, and don't rely on values less than 16 milliseconds.

Test code to collect data:
The script takes a few minutes to run, monitor progress (0-99) in the terminal

int start()
  {
    string result[100][50];    
    int i, j;
    int startTick, endTick;    
    for( i = 0; i < 100; i++){
        Print(i);
        for(j = 0; j < 50; j++){
            startTick = GetTickCount();
            Sleep(i);
            endTick = GetTickCount();
            result[i][j] = DoubleToStr(endTick - startTick, 0)+" ";
        }
    }
    int FILE;
    FILE = FileOpen("SleepTest.txt", FILE_WRITE|FILE_BIN);    
    string resultLine;
    for(i = 0; i < 100; i++){
        resultLine = "Sleep("+i+") GetTickCount() = ";
        for(j = 0; j < 50; j++){
            resultLine = StringConcatenate(resultLine, result[i][j], " ");
        }
        resultLine = StringConcatenate(resultLine, "\r\n");    
        FileWriteString(FILE, resultLine, StringLen(resultLine));
    }
   return(0);
  }
 

Test Results Spreadsheet

Files:
sleeptest.zip  11 kb
 

Interesting discovery phy... I used the Sleep() function on another EA and double checked against stopwatch timer. To confirm tests on 1-5 minutes seem ok but as pointed out hardware can play a part when < 99 milliseconds is the target.

 

I'm running a detailed test of 0 to 99 milliseconds sleep.
The Averages support your theory that "everything is fine" over one to 5 minutes.

Spreadsheet sample, values in milliseconds as reported by MT4

 

Ops that came out the wrong way phy. Yes the discrepancy is definitely there < 99milliseconds and was just confirming that above this is fine. I have no doubt you are running a smooth rig...

Don't i still owe you that beer?

 
I was at the bar, where were you?
 

Banging my head against a wall with this order control management 'ArrayMinimum' .

Catch ya there later...

 

no strange, Sleep use 1/64 second as base unit ( that maybe is standand for CMOS chip, I forget), through you can set millisecond number as its parameter.

Why you need such precise time ?

even network delay has several hundreds milliseconds.

 

"Sleep use 1/64 second as base unit "

Ok, I figured it was OS or hardware limitation as mentioned. Thank you for the reply.

"Why you need such precise time ?"

Precise time, maybe no, but small time, yes, I need.

Example 1:

TickCollector script for all pairs.

while not stopped
cycle through all pair names
check for new price
write to tick files
Sleep // during Sleep, script gives execution time to other processes. If no sleep, CPU = 100% in this loop and MT4 "locks up"
repeat

Now I know the minumum "repeatable" Sleep is 16milliseconds, and so do other people (if interested)

 

1. Function GetTickCount cannot be used for exact detection of small intervals. You may get 0 ms, 16 ms, 1034 ms, etc. However, you will never get 1-14 ms. This is due to architecture properties.

2. Function Sleep works in a bit different way than many people think. Indeed, the system guarantees that Sleep(1) will take AS MUCH AS 1ms, whereas the upper limit is not specified. (Quoted from the article "То, что вам никто не говорил о многозадачности в Windows" in Russian, translated into English by myself, sorry. This may sound like 'What You Have Never Been Told about Multitasking in Windows')

3. Function Sleep in the client terminal works with granulation of 100 ms in order one not to be able to hang the Expert Advisor.

Reason: