Features of the mql5 language, subtleties and tricks - page 94

 
Konstantin:

and how do you see the practical application of GetMicrosecondCount that it spoils the whole work of the program in the current version? describe the practical application

for example, neither in C++, nor here, i do not see variants other than those described by Renat, i.e. measuring code execution time with accuracy in mcs

I don't understand your insistence, frankly speaking

The scope of this function is quite extensive, if you have a flight of fancy.
I already mentioned above about multitimer, which allows you to run several timers with different periods at the same time.
This is also what fxsaber already wrote about.
The microsecond function compared to millisecond function is useful not only for speed tests, but also for taking various telemetry information during runtime of Expert Advisors.
If the accuracy of such telemetry is 16 ms (more precisely 1/64 s = 15625 microseconds) - this is a very big error.

 
Aleksey Vyazmikin:

It is configured, but it does not help - I do not understand the reason. True, my server is ntp2.stratum2.ru

If you use GetTickCount for such long intervals, you shouldn't have any problems.
There will be problems if you use GetMicrosecondCount.
If using the microsecond function is a matter of principle, you'd better use this variant of the function.

Information for taking into account:

Approximate execution time of the functions:

- GetTickCount - ~ 2 ns

- GetMicrosecondCount - ~ 30 ns

-RealMicrosecondCount - ~ 40 ns

 
Nikolai Semko:

If you use GetTickCount for such long intervals, there shouldn't be any problems.
There will be problems if you use GetMicrosecondCount.
If using microsecond function is a matter of principle, you'd better use this variant of the function.

Information to be taken note of:

Approximate execution time of the functions:

- GetTickCount - ~ 2 ns

- GetMicrosecondCount - ~ 30 ns

-RealMicrosecondCount - ~ 40 ns

I'm using someone else's code, there is no such functions, but the effect of desynchronization occurs.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

input color ValuesPositiveColor = MediumSeaGreen; // Color for positive timer values
input color ValuesNegativeColor = PaleVioletRed;  // Color for negative timer values
input int   TimeFontSize        = 10;             // Font size for timer
input int   TimerShift          = 7;              // Timer shift

#define  clockName "CandleTimer"
int  atrHandle;
int ObjComplite=0;
int  OnInit()                   { atrHandle = iATR(NULL,0,30); EventSetTimer(1);  return(0); }
void OnDeinit(const int reason) { EventKillTimer(); }
void OnTimer( )                 { refreshClock();  }
int  OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
{
   //refreshClock();
   return(rates_total);
}
void refreshClock()
{
   static bool inRefresh = false;
           if (inRefresh) return;
               inRefresh = true;
                              ShowClock(); ChartRedraw();
               inRefresh=false;
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
void ShowClock()
{
   int periodMinutes = periodToMinutes(Period());
   int shift         = periodMinutes*TimerShift*60;
   int currentTime   = (int)TimeCurrent();
   int localTime     = (int)TimeLocal();
   int barTime       = (int)iTime(Symbol(),PERIOD_CURRENT,0);//iTime();
   int diff          = (int)MathMax(round((currentTime-localTime)/3600.0)*3600,-24*3600);

      color  theColor;
      string time = getTime(barTime+periodMinutes*60-localTime-diff,theColor);
             time = (TerminalInfoInteger(TERMINAL_CONNECTED)) ? time : time+" x";

      if(ObjComplite==0)if(ObjectFind(0,clockName) < 0)
      {            
         ObjectCreate(0,clockName,OBJ_TEXT,0,barTime+shift,0);
         ObjComplite=1;
      }   
         ObjectSetString(0,clockName,OBJPROP_TEXT,time);
         ObjectSetString(0,clockName,OBJPROP_FONT,"Arial");
         ObjectSetInteger(0,clockName,OBJPROP_FONTSIZE,TimeFontSize);
         ObjectSetInteger(0,clockName,OBJPROP_COLOR,theColor);

      
         if (ChartGetInteger(0,CHART_SHIFT,0)==0 && (shift >=0))
               ObjectSetInteger(0,clockName,OBJPROP_TIME,barTime-shift*3);
         else  ObjectSetInteger(0,clockName,OBJPROP_TIME,barTime+shift);

      double price[]; if (CopyClose(Symbol(),0,0,1,price)<=0) return;
      double atr[];   if (CopyBuffer(atrHandle,0,0,1,atr)<=0) return;
             price[0] += 3.0*atr[0]/4.0;
             
      bool visible = ((ChartGetInteger(0,CHART_VISIBLE_BARS,0)-ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0)) > 0);
      if ( visible && price[0]>=ChartGetDouble(0,CHART_PRICE_MAX,0))
            ObjectSetDouble(0,clockName,OBJPROP_PRICE,price[0]-1.5*atr[0]);
      else  ObjectSetDouble(0,clockName,OBJPROP_PRICE,price[0]);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
string getTime(int times, color& theColor)
{
   string stime = "";
   int    seconds;
   int    minutes;
   int    hours;
   
   if (times < 0) {
         theColor = ValuesNegativeColor; times = (int)fabs(times); }
   else  theColor = ValuesPositiveColor;
   seconds = (times%60);
   hours   = (times-times%3600)/3600;
   minutes = (times-seconds)/60-hours*60;
   
   if (hours>0)
   if (minutes < 10)
         stime = stime+(string)hours+":0";
   else  stime = stime+(string)hours+":";
         stime = stime+(string)minutes;
   if (seconds < 10)
         stime = stime+":0"+(string)seconds;
   else  stime = stime+":" +(string)seconds;
   return(stime);
}


int periodToMinutes(int period)
{
   int i;
   static int _per[]={1,2,3,4,5,6,10,12,15,20,30,0x4001,0x4002,0x4003,0x4004,0x4006,0x4008,0x400c,0x4018,0x8001,0xc001};
   static int _min[]={1,2,3,4,5,6,10,12,15,20,30,60,120,180,240,360,480,720,1440,10080,43200};

   if (period==PERIOD_CURRENT) 
       period = Period();   
            for(i=0;i<20;i++) if(period==_per[i]) break;
   return(_min[i]);   
}
 
Slava:
What is the probability of changing the local computer time between two calls to GetMicrosecondsCount used to measure the time in microseconds?

This depends, first of all, on the specified period of synchronization of the system time with the Internet time. For example, I set synchronization once a day, and during this time there is more than 1 second of divergence. Someone synchronizes once an hour, or even more often. So, calculate the probability.

 
Nikolai Semko:

...because I already know about such feature of GetMicrosecondCount() and that this function is more than an order of magnitude slower than GetTickCount.

If we were to compare PerfomanceCount and GetTickCount directly, the difference would be much smaller.

Although, frankly speaking, I don't really understand all this talk about execution speed when we're talking about 2-20 nanoseconds. This difference can only be really felt by running a nearly empty loop (with this function) for a hundred million iterations. This is in itself a wrongly designed solution.

 
Renat Fatkhullin:

And yes, you'll be just as blown away by a pure WinAPI function (whether GetTickCount or QueryPerformanceCounter) when you slip a scrap into the chainsaw by changing the date even by seconds. There's no protection at all that you're talking about supposedly having. Sucked out of your hand as a problem and alleged solution.

So everything is correct - this is how WinAPI is and this is reality.

You're wrong. I specifically cited the code here using WinApi. Run it, change the clock in the process, and look at the result.

It's not very clear, how to conduct any dialogue with you, if your arguments are based on speculations. And you don't even consider it necessary to get acquainted with the course of topic discussion.

 
Nikolai Semko:

Scope of this function is very broad, if there is a flight of fancy.
I already mentioned above about multitimer, which allows you to run several timers with different periods at the same time.
This is also what fxsaber already wrote about.
The microsecond function compared to millisecond function is useful not only for speed tests, but also for taking various telemetry information during runtime of Expert Advisors.
If such telemetry is accurate at 16 ms (more precisely 1/64 s = 15625 microseconds), it is quite a big error.

I constantly measure the speed of execution, as Renat wrote, I have never seen any problems, you seem to wind up from nothing, or do not want to change what was written before, it happens when as the saying goes on ... shi and then you realize that all was written in vain, no offense, but the same multitimer you mentioned easy to implement without any errors, but for this you have to pay, Renat above also gave a description in response to my question

 
Konstantin:
The mouth of the Truth is dumb to the uninitiated.
 
Nikolai Semko:
The mouth of the Truth is mute to the uninitiated.

well, yes))

 
Alexey Navoykov:

It depends, first of all, on a set period of synchronization of system time with internet time. For example, I set synchronization once a day, and during this time there is more than 1 second of divergence. Someone is synchronized once an hour, or even more often. Here you can estimate the probability.

Are you sure you read the whole question?

...between two calls to GetMicrosecondsCount...