mql5语言的特点、微妙之处以及技巧 - 页 94

 
康斯坦丁

你如何看待GetMicrosecondCount的实际应用,它破坏了当前版本中程序的整个工作? 描述实际应用

例如,我在C++和这里都没有看到任何变体,除了Renat描述的那些,即用MCS精确测量代码执行时间

坦率地说,我不理解你的执着。

这个功能的范围相当广泛,如果你有一个飞行的想法。
我在上面 已经提到了多定时器,它允许你在同一时间运行几个不同周期的定时器。
这也是fxsaber已经写过的 内容。
与毫秒函数相比,微秒函数不仅适用于速度测试,而且还适用于在专家顾问系统运行期间获得各种遥测信息。
如果这种遥测的精度是16毫秒(更精确的是1/64秒=15625微秒)--这是一个非常大的误差。

 
阿列克谢-维亚兹米 金。

配置了,但它没有帮助--我不明白其中的原因。是的,我的服务器是ntp2.stratum2.ru

如果你使用GetTickCount的时间间隔这么长,你应该不会有任何问题。
如果你使用GetMicrosecondCount,会出现问题。
如果使用微秒函数是一个原则问题,你最好使用该函数的这个变体

需要注意的信息。

函数的大致执行时间。

- GetTickCount - ~ 2 ns

- GetMicrosecondCount - ~ 30 ns

-RealMicrosecondCount - ~ 40 ns

 
尼古拉-森科

如果你使用GetTickCount的时间间隔这么长,你应该不会有任何问题。
如果你使用GetMicrosecondCount,会出现问题。
如果使用微秒函数是一个原则问题,你最好使用该函数的这个变体

需要注意的信息。

函数的近似执行时间。

- GetTickCount - ~ 2 ns

- GetMicrosecondCount - ~ 30 ns

-RealMicrosecondCount - ~ 40 ns

我使用的是别人的代码,那里根本就没有这样的功能,但却出现了这种不同步的效果。

#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]);   
}
 
斯拉瓦
在两次调用用于测量微秒时间的GetMicrosecondsCount之间,改变本地计算机时间的概率是多少?

这首先取决于系统时间与互联网时间同步的设定周期。例如,我设置每天同步一次,在这段时间内有超过1秒的差异。 有人每小时同步一次,甚至更频繁。 那么,计算一下概率。

 
Nikolai Semko:

...因为我已经知道GetMicrosecondCount()的这种特性,而且这个函数比GetTickCount要慢。

我想这种缓慢是由于除了获得原生PerfomanceCount之外,它还会额外干扰本地时间,所以我们必须为这种熊市服务付费。 如果我们直接比较PerfomanceCount和GetTickCount的速度,差异会小很多。

虽然,坦率地说,当我们在谈论2-20纳秒时,我并不真正理解这些关于执行速度的话题。 只有在执行一个几乎是空的循环(用这个函数)进行一亿次迭代时,才能感受到这种差别。 这本身就是一个设计不正确的解决方案。

 
雷纳特-法特库林

是的,当你通过改变日期甚至以秒为单位的方式将废品滑入电锯时,你也会被一个纯WinAPI函数(GetTickCount或QueryPerformanceCounter)所震撼。你所说的所谓的保护,根本就没有。从你的手指中吸出的是问题和所谓的解决方案。

所以一切都是真的--这就是WinAPI的情况,这就是现实。

你错了,我在这里 特别引用了使用WinApi的代码。 运行它,在过程中改变时钟,看看结果。

如果你的论点是基于猜测,那么如何与你进行对话就不太清楚了。 而且你甚至认为没有必要熟悉这个问题的讨论。

 
尼古拉-森科

这个功能的应用领域相当广泛,如果你有幻想的话。
我在上面 已经提到了多定时器,它允许你在同一时间运行几个不同周期的定时器。
这也是fxsaber已经写过的 内容。
与毫秒函数相比,微秒函数不仅适用于速度测试,而且还适用于在专家顾问系统运行期间获得各种遥测信息。
如果这样的遥测数据精确到16毫秒(更准确地说是1/64秒=15625微秒),那就是相当大的误差。

我不断地测量执行速度,就像Renat写的那样,我从未见过任何问题,你似乎从无到有,或者不想改变以前写的东西,当你徒劳无功时就会发生,没有冒犯的意思,但你提到的同样的多聚器可以很容易地实现,没有任何错误,但你必须为此付出代价,Renat在回答我上述问题时也作了说明

 
康斯坦丁
真理之口对不了解的人来说是哑巴。
 
尼古拉-森科
真理之口对不了解的人来说是哑巴。

嗯,是的))。

 
阿列克谢-纳沃伊科夫

这首先取决于系统时间与互联网时间同步的设定时间。例如,我每天设置一次同步,在此期间有超过1秒的分歧。 有人一小时一次,甚至更频繁。 这里你可以估计一下概率。

你确定你读了整个问题吗?

...在两次调用GetMicrosecondsCount之间...